Advent of Code: Day 10
Helpers
defmodule Helpers do
@spec read_file_contents(String.t()) :: binary()
def read_file_contents(filename) do
file_path = "/Users/charlie/github.com/charlieroth/advent-of-code/2022/#{filename}"
case File.read(file_path) do
{:ok, contents} -> contents
{:error, _} -> raise("Failed to read file contents")
end
end
end
Part 01: Cathode-Ray Tube
It seems to be some kind of cathode-ray tube screen and simple CPU that are both driven by a precise clock circuit. The clock circuit
ticks at a constant rate; each tick is called a cycle
.
Start by figuring out the signal being sent by the CPU. The CPU has a single register, X
, which starts with the value 1
. It supports only two instructions:
-
addx V
takestwo cycles
to complete. After two cycles, theX
register is increased by the valueV
. (V
can be negative.) -
noop
takesone cycle
to complete. It has no other effect.
The CPU uses these instructions in a program (your puzzle input) to, somehow, tell the screen what to draw.
Consider the following small program:
noop
addx 3
addx -5
Execution of this program proceeds as follows:
-
At the start of the first cycle, the noop instruction begins execution. During the first cycle,
X
is1
. After the first cycle, thenoop
instruction finishes execution, doing nothing. -
At the start of the second cycle, the
addx 3
instruction begins execution. During the second cycle,X
is still1
. -
During the third cycle,
X
is still1
. After the third cycle, theaddx 3
instruction finishes execution, settingX
to4
. -
At the start of the fourth cycle, the
addx -5
instruction begins execution. During the fourth cycle,X
is still4
. -
During the fifth cycle,
X
is still4
. After the fifth cycle, theaddx -5
instruction finishes execution, settingX
to-1
.
Maybe you can learn something by looking at the value of the X
register throughout execution. For now, consider the signal strength
(the cycle number multiplied by the value of the X
register) during the 20th
cycle and every 40
cycles after that (that is, during the 20th
, 60th
, 100th
, 140th
, 180th
, and 220th
cycles).
Example input:
addx 15
addx -11
addx 6
addx -3
addx 5
addx -1
addx -8
addx 13
addx 4
noop
addx -1
addx 5
addx -1
addx 5
addx -1
addx 5
addx -1
addx 5
addx -1
addx -35
addx 1
addx 24
addx -19
addx 1
addx 16
addx -11
noop
noop
addx 21
addx -15
noop
noop
addx -3
addx 9
addx 1
addx -3
addx 8
addx 1
addx 5
noop
noop
noop
noop
noop
addx -36
noop
addx 1
addx 7
noop
noop
noop
addx 2
addx 6
noop
noop
noop
noop
noop
addx 1
noop
noop
addx 7
addx 1
noop
addx -13
addx 13
addx 7
noop
addx 1
addx -33
noop
noop
noop
addx 2
noop
noop
noop
addx 8
noop
addx -1
addx 2
addx 1
noop
addx 17
addx -9
addx 1
addx 1
addx -3
addx 11
noop
noop
addx 1
noop
addx 1
noop
noop
addx -13
addx -19
addx 1
addx 3
addx 26
addx -30
addx 12
addx -1
addx 3
addx 1
noop
noop
noop
addx -9
addx 18
addx 1
addx 2
noop
noop
addx 9
noop
noop
noop
addx -1
addx 2
addx -37
addx 1
addx 3
noop
addx 15
addx -21
addx 22
addx -6
addx 1
noop
addx 2
addx 1
noop
addx -10
noop
noop
addx 20
addx 1
addx 2
addx 2
addx -6
addx -11
noop
noop
noop
The interesting signal strengths can be determined as follows:
-
During the
20th
cycle, registerX
has the value21
, so the signal strength is20 * 21 = 420
. (The20th
cycle occurs in the middle of the secondaddx -1
, so the value of registerX
is the starting value,1
, plus all of the otheraddx
values up to that point:1 + 15 - 11 + 6 - 3 + 5 - 1 - 8 + 13 + 4 = 21
.) -
During the
60th
cycle, registerX
has the value19
, so the signal strength is60 * 19 = 1140
. -
During the
100th
cycle, registerX
has the value18
, so the signal strength is100 * 18 = 1800
. -
During the
140th
cycle, registerX
has the value21
, so the signal strength is140 * 21 = 2940
. -
During the
180th
cycle, registerX
has the value16
, so the signal strength is180 * 16 = 2880
. -
During the
220th
cycle, registerX
has the value18
, so the signal strength is220 * 18 = 3960
.
The sum of these signal strengths is 13140
.
Find the signal strength during the 20th
, 60th
, 100th
, 140th
, 180th
, and 220th
cycles. What is the sum of these six signal strengths?
defmodule PartOne do
def solution(input) do
cycle_map =
input
|> parse()
|> start()
20..220//40
|> Enum.to_list()
|> Enum.reduce(0, fn cycle, signal_strength ->
signal_strength + Map.get(cycle_map, cycle) * cycle
end)
end
defp parse(input) do
input
|> String.split("\n", trim: true)
|> Enum.map(&String.split(&1, " ", trim: true))
|> Enum.map(fn line ->
case length(line) do
1 -> {:noop}
2 -> {:addx, String.to_integer(Enum.at(line, 1))}
end
end)
end
defp start(instructions) do
%{cycles: cycles} =
instructions
|> Enum.reduce(%{cycle: 1, reg: 1, cycles: []}, fn instruction,
%{cycle: cycle, reg: reg, cycles: cycles} =
state ->
case instruction do
{:noop} ->
%{state | cycle: cycle + 1, cycles: [{cycle + 1, reg} | cycles]}
{:addx, value} ->
new_cycles = [{cycle + 1, reg} | cycles]
new_cycles = [{cycle + 2, value + reg} | new_cycles]
%{state | cycle: cycle + 2, reg: value + reg, cycles: new_cycles}
end
end)
Map.new(cycles)
end
end
Helpers.read_file_contents("inputs/Day10.txt")
|> PartOne.solution()