AoC 2022 D10: Cathode-Ray Tube
| Problem statement | Source code | Tags: VMManual inspection
← Previous Back to AoC Index Next →
Part 1
I simulate the process of executing the instructions, keeping track of the current cycle and the value of the register X, putting all values of X at each cycle in a list. Then I can just sum the values at the specified cycles.
When parsing the instructions, I represent each instruction as (Int, Int), containing the execution time and the value to add to X (0 for noop).
Then I fold over the list of the instructions. The state tracks three things: current cycle, value of X before the instruction, and the list of X values.
With the list of X values, finding the sum of the specified cycles is straightforward.
Part 2
With the full list of X values, I can just iterate through them and print the appropriate character for each cycle (turns out that rendering a CRT is the same as printing to the console). For each X value, I check if the pixel corresponding to the cycle is within the sprite.
Once again, I appreciate the ASCII art produced: