From 94c6ccb87bea6fc05876fd6822a6855b334c6101 Mon Sep 17 00:00:00 2001 From: Bryce Allen Date: Sun, 11 Dec 2022 13:18:24 -0500 Subject: [PATCH] day10 p2 --- day10/day10.jl | 51 ++++++++++++++++++++++++++++++++++++++----- day10/example_crt.txt | 6 +++++ day10/input_crt.txt | 6 +++++ 3 files changed, 58 insertions(+), 5 deletions(-) create mode 100644 day10/example_crt.txt create mode 100644 day10/input_crt.txt diff --git a/day10/day10.jl b/day10/day10.jl index 92a491a..d775064 100644 --- a/day10/day10.jl +++ b/day10/day10.jl @@ -2,7 +2,7 @@ using Test -struct Instruction +mutable struct Instruction cycles :: UInt add :: Int end @@ -18,7 +18,7 @@ function parse_instruction_line(line) end end -function execute_program(io; end_cycle=220) +function signal_strength_sum(io; end_cycle=220) X :: Int = 1 cycle :: Int = 1 signal_strength_sum :: Int = 0 @@ -43,10 +43,48 @@ function execute_program(io; end_cycle=220) return signal_strength_sum end +function get_crt(io; end_cycle=240) + X :: Int = 2 # Note: add 1 to simplify 1-based indexing + pending_instruction :: Union{Instruction, Nothing} = nothing + screen = fill('.', (Int(ceil(end_cycle / 40)), 40)) + nrows, ncols = size(screen) + for i in 1:nrows + for j in 1:ncols + if abs(X - j) <= 1 + screen[i, j] = "#"[1] + else + screen[i, j] = '.' + end + + if pending_instruction === nothing + if eof(io) + continue + end + inst = parse_instruction_line(readline(io)) + pending_instruction = inst + end + pending_instruction.cycles -= 1 + if pending_instruction.cycles == 0 + X += pending_instruction.add + pending_instruction = nothing + end + end + end + return join(reduce(*, screen, dims=2, init=""), "\n") +end + function test() @testset "signal strength sum" verbose=true begin - @test execute_program("example.txt") == 13140 - @test execute_program("input.txt") == 13220 + @test signal_strength_sum("example.txt") == 13140 + @test signal_strength_sum("input.txt") == 13220 + end + @testset "crt test" verbose=true begin + open("example.txt") do io + @test get_crt(io) == chomp(read("example_crt.txt", String)) + end + open("input.txt") do io + @test get_crt(io) == chomp(read("input_crt.txt", String)) + end end end @@ -56,7 +94,10 @@ function main() else infile = ARGS[1] println("infile = ", infile) - println("signal strength sum = ", execute_program(infile)) + println("signal strength sum = ", signal_strength_sum(infile)) + open(infile, "r") do io + println(get_crt(io)) + end end end diff --git a/day10/example_crt.txt b/day10/example_crt.txt new file mode 100644 index 0000000..c973789 --- /dev/null +++ b/day10/example_crt.txt @@ -0,0 +1,6 @@ +##..##..##..##..##..##..##..##..##..##.. +###...###...###...###...###...###...###. +####....####....####....####....####.... +#####.....#####.....#####.....#####..... +######......######......######......#### +#######.......#######.......#######..... diff --git a/day10/input_crt.txt b/day10/input_crt.txt new file mode 100644 index 0000000..5718300 --- /dev/null +++ b/day10/input_crt.txt @@ -0,0 +1,6 @@ +###..#..#..##..#..#.#..#.###..####.#..#. +#..#.#..#.#..#.#.#..#..#.#..#.#....#.#.. +#..#.#..#.#..#.##...####.###..###..##... +###..#..#.####.#.#..#..#.#..#.#....#.#.. +#.#..#..#.#..#.#.#..#..#.#..#.#....#.#.. +#..#..##..#..#.#..#.#..#.###..####.#..#.