Day 8: Matchsticks
A simple counting puzzle.
This one went okay, but I found Picat's pattern matching a bit limited when it came to partial string matching. I wanted to match hex code literals a function head like this:
solve([("\\x" ++ [_,_] ++ X)|XS],C,L) = solve([X|XS],C+4,L+1).
Picat didn't like this, and I had to go with nested list constructors instead, making things uglier:
solve([['\\'|['x'|[_|[_|X]]]] | XS], C, L) = solve([X|XS],C+4,L+1).
Maybe there's a nicer way to express this that I'll discover later.
Easier than part 1.
- Picat could do with nicer string utilities (e.g. a
starts_with
orcontains
function, like Haskell'sisPrefixOf
andisInfixOf
, rather than having to either futz withfind
or awkward pattern matching.
*** error until line 11
'|X]]|XS], C, L) = solve(X,C+2,L+1).
(Syntax error, but what is the message trying to say? I think this one came down to my failing to escape a quote in a character literal.)