Module aoc::y22d10

source ·
Expand description

Advent of Code 2022 Day 10: https://adventofcode.com/2022/day/10

Because the number of numbers and total states is so small I decided to simply compute the state of the register at each cycle and then analyze it afterwards for each part of the challenge. We basically start with the register x at 1 and then if we see a noop push it into the cycles vector as-is, otherwise compute the change that’s being made and push it once as-is, and then again with the adjustment (the instructions state that the addx operation takes two cycles). This gives us a vector with the state of the x register at each cycle during the “program”’s execution.

In part one we then need to calculate a sum based on the state of the register at various points (cycle 20 and then every 40 cycles after). And in part two we instead need to draw the output based on the state of the register. It actually took me a while to figure out what the prompt was actually asking for so I’ll try to reproduce it here in a hopefully easier way for me to understand.

The drawing operation works like this: we draw from left to right and from top to bottom one pixel at a time. We know whether to draw a # or a . depending on the state of the register for that pixel (the index on the cycles vector is row * 40 + column. The “sprite” is the value of the register plus or minus one, and so if the current column falls into the range of x - 1 or x + 1 then we draw the # character, otherwise the . character.

Functions§

  • The solution for part one of the day ten challenge.
  • The solution for part two of the day ten challenge.