Advent of Code - Day 7

Note by the Moderators: This topic is to talk about Day 7 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.

Here’s my take on day7. I implemented the first part as a special case of part two (using just one worker). This makes part 1 less optimal, but the running time is still good enough.

The curiosity of my approach is that I made a stream which emits all intermediate states. Part 1 then boils down to taking the last state and fetching the processed items. Part 2 amounts to counting all the states and decrementing it by one.


I’ve finally gotten around to solving this (code is here) and, like @sasajuric, my part two solution replaced me original part one code.

My solution revolves around having two maps; one for the steps, their dependencies and their durations and the other for workers, which step they are processing and when they will be done processing (in seconds).

I’m using recursion to step through time and at each second I check to see if any steps have been completed (and log their identifier if they have) and then assign any pending steps to any available workers.

When a step is assigned to a worker it is removed from the map of sets and any references to it are removed from the dependencies list in other steps. The actual progress of a step (really just monitoring when it is complete) is done via the workers.

I’m not entirely happy with my code and will be going back to do some tidying (and documenting) but that’s tending to be my overall trend with these challenges. They’re proving to be a fantastic way to find my way around the Elixir standard libraries but they’re also taking me far longer to complete then they probably should.

Ah well… I’ll improve with practice!