Advent of Code - Day 12


Note: This topic is to talk about Day 12 of the Advent of Code.

For general discussion about the Advent of Code 2018 and links to topics of the other days, see this topic.



My solution is here.

Part 1 was pretty straightforward. I built a map of rules, and then used a bit of binary pattern matching to move to the next state.

In part 2 it really helped me that I did AoC last year. As soon as I saw it, I knew that there had to be a cycle. I did a quick experiment, and found out that the plants converge to the stable state pretty soon. In that state, the plants relative positioning doesn’t change, but they gradually move in one direction.

I briefly thought about solving this special case, but I wasn’t quite happy with that, because I thought there might exist a more general situation with larger cycles. Therefore, I wrote a more complex recursion which caches pattern transitions, and can handle the cycle of any length.



My solution is here - cleaning of the generation head and tail allows me to discover the cycle.



Here’s my solution to Day 12:

I also found the cycle and quit there.

Here’s a video of the creation of this code (good for 14 days):

1 Like


Too much appending to lists in this solution. Need to optimize still for part 2



Thanks for sharing your solutions, I am learning tons by reading your code! On line 10 of the solution here, it looks like a declaration of the function without any body. Why is this separate and why not put this in the definition at line 14 itself? Thanks in advance :slight_smile:

1 Like


That function has multiple clauses (in this case two - in lines 10 and 12 respectively, and it also has a default third argument. In this case, a separate bodyless function clause is needed to declare defaults. This is explained in the def documentation.

1 Like


Thanks !