commit 6d129d2f5129e367ec3fa6c38be9435284a8b46a
parent 7539928b528e483ce9f1fa715e095d2b550d3736
Author: Eamon Caddigan <eamon.caddigan@gmail.com>
Date: Sat, 10 Dec 2022 14:30:33 -0800
Solution to day 10, part 1.
Diffstat:
A | src/day_10.jl | | | 232 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
1 file changed, 232 insertions(+), 0 deletions(-)
diff --git a/src/day_10.jl b/src/day_10.jl
@@ -0,0 +1,232 @@
+#!/usr/bin/env julia
+# https://adventofcode.com/2022/day/10
+using AdventOfCode
+using DataFrames
+
+# Using data frames again, not because they're strictly necessary, but because
+# I've only touched them once so far and any actual work I do with Julia some
+# day will certainly require them. I did this a lot last year, leaning into
+# pandas (and Series objects in particular) just to get the hang of them. Let's
+# just ignore the fact that I've barely touched python since then, for now. :)
+
+example = readlines(IOBuffer("""
+ 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"""))
+input = readlines("data/day_10.txt")
+
+"""
+Calculate cumulative sum, skipping `nothing` values
+"""
+function cumsomething(x)
+ cumsum([isnothing(v) ? 0 : v for v in x])
+end
+
+"""
+Calculate signal strength
+"""
+function signal_strength(X, total_cycles)
+ cat(1, X[1:end-1], dims = 1) .* total_cycles
+end
+
+"""
+Run the instructions and return a data frame tracing the machine state.
+
+Note that the X value is the value of the X register at the end of the cycle
+specified by total_cycles. So if you want the X value during a given cycle
+number, look one row earlier.
+"""
+function run_instructions(input)
+ instructioncycles = Dict(
+ "addx" => 2,
+ "noop" => 1
+ )
+
+ cpu = DataFrame(
+ instruction = AbstractString[],
+ arg = Vector{Union{Int, Nothing}}(),
+ cycles = Int[]
+ )
+
+ # First, read in the instructions
+ for instruction = input
+ instparts = split(instruction, " ")
+ push!(cpu, (
+ instruction = instparts[1],
+ arg = length(instparts) > 1 ? parse(Int, instparts[2]) : nothing,
+ cycles = instructioncycles[instparts[1]]
+ ))
+ end
+
+ # Next, find the value of X and the total number of cycles
+ cpu = transform(cpu, :arg => (v -> 1 .+ cumsomething(v)) => :X)
+ cpu = transform(cpu, :cycles => cumsum => :total_cycles)
+
+ cpu
+end
+
+"""
+Find the signal strength during a cycle.
+
+To find the signal strength DURING cycle n, we need the X value AFTER cycle
+n-1.
+"""
+function strength_during(cpu, cycle_number)
+ X = cpu.X[findlast(cpu.total_cycles .< cycle_number)]
+ X * cycle_number
+end
+
+function part_1(input)
+ cpu = run_instructions(input)
+ sum(map(Base.Fix1(strength_during, cpu), 20:40:maximum(cpu.total_cycles)))
+end
+@assert part_1(example) == 13140
+@info part_1(input)
+
+function part_2(input)
+ nothing
+end
+@info part_2(input)