While I wait for it to get a little later into Day 13, before posting spoilers for those that haven't solved it yet, let's talk about Day 2.
Part 1 described three commands. Two modify one value (the depth of a submarine), and the third modifies a second value (the submarine's distance across the ocean floor, a.k.a. "X"). My solution is a very typical Erlang list iteration:
After breaking the input into a list of lines, I recurse through the lines, updating the depth and x values of my state record, as directed. This could have been written in the "fold" syntax I waxed lyrical about yesterday, but this time I wrote it out longhand. Sometimes that's the way it works out.
Part 2 requires exactly the same parsing, but different interpretation. This time, one command (forward) modifies both x and depth, while the other two commands update a completely different bit of state (aim). The twist is that forward's modification also depends on the latest value of that other bit of state.
Surprise! I thought it would be fun to have a semi-direct comparison of manual recursion versus fold. If this comparison isn't direct enough, you can also view the git diff where I changed solveB just now to demonstrate. Which do you like better? I think each has its own noise and clutter, but feel free to send me your opinion on Twitter (@hobbyist) or Github (beerriot).
The ending state shows what we expected: x is the same as last time, and aim took the same value that depth had previously, but now our submarine has ventured much deeper.
Good luck to those solving Day 13 right now! Come back later to read my solution.
Post Copyright © 2021 Bryan Fink