· · · ·

Well, December's here at last, and with it Day 1 of Advent of Code.

… It goes on to explain that you may only leave by solving a captcha to prove you're not a human. Apparently, you only get one millisecond to solve the captcha: too fast for a normal human, but it feels like hours to you. …

As well as posting solutions here when I can, I'll be putting them all on https://github.com/jezcope/aoc2017 too.

Commentary

After doing some challenges from last year in Haskell for a warm up, I felt inspired to try out the functional-ish Python dialect, Coconut. Now that I've done it, it feels a bit of an odd language, neither fish nor fowl. It'll look familiar to any Pythonista, but is loaded with features normally associated with functional languages, like pattern matching, destructuring assignment, partial application and function composition.

That makes it quite fun to work with, as it works similarly to Haskell, but because it's restricted by the basic rules of Python syntax everything feels a bit more like hard work than it should.

The accumulator approach feels clunky, but it's necessary to allow tail call elimination, which Coconut will do and I wanted to see in action. Lo and behold, if you take a look at the compiled Python version you'll see that my recursive implementation has been turned into a non-recursive while loop.

Then again, maybe I'm just jealous of Phil Tooley's one-liner solution in Python.

import sys

case reiterable(s):
match (|d, d|) :: rest:
return inverse_captcha_((|d|) :: rest, acc + int(d))
match (|d0, d1|) :: rest:

return acc

case (reiterable(s0), reiterable(s1)):
match ((|d0|) :: rest0, (|d0|) :: rest1):
return inverse_captcha_1_(rest0, rest1, acc + int(d0))
match ((|d0|) :: rest0, (|d1|) :: rest1):

return acc

assert "1111" |> inverse_captcha == 4
assert "1122" |> inverse_captcha == 3
assert "1234" |> inverse_captcha == 0
assert "91212129" |> inverse_captcha == 9