April 25, 2025

Solution for Advent of Code 2018 - Day 14: Chocolate Charts

Link to the puzzle text

Part 1

In this puzzle we are given instructions on how to build a sequence of digits from 0 to 9. We have two position markers starting at the values 3 and 7. In each step, we add the values at the current position markers to the end of the sequence. We then advance the position markers by 1 + the value at their position. We are asked to find the next ten values after a number of elements.

We can implement the sequence building algorithm straight forward with the sequence stored in a string:

scoreboard = "37"
pos_1 = 0
pos_2 = 1
for i in itertools.count(1):
scoreboard += str(int(scoreboard[pos_1]) + int(scoreboard[pos_2]))
pos_1 = (1 + pos_1 + int(scoreboard[pos_1])) % len(scoreboard)
pos_2 = (1 + pos_2 + int(scoreboard[pos_2])) % len(scoreboard)

The answer is given by checking if the current length of the sequence and the next ten values are already computed.

Part 2 

In part 2, we should use the same algorithm but count how many elements it takes until a given sequence of digits is produced.

We check the last produced elements of our sequence and output the length of the sequence until now.

There is probably a better solution, since we are storing all the previous results, but the program was fast enough.

Link to my solutions

No comments:

Post a Comment