|
|
|
@ -2,7 +2,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
using Test
|
|
|
|
using Test
|
|
|
|
|
|
|
|
|
|
|
|
struct Instruction
|
|
|
|
mutable struct Instruction
|
|
|
|
cycles :: UInt
|
|
|
|
cycles :: UInt
|
|
|
|
add :: Int
|
|
|
|
add :: Int
|
|
|
|
end
|
|
|
|
end
|
|
|
|
@ -18,7 +18,7 @@ function parse_instruction_line(line)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
function execute_program(io; end_cycle=220)
|
|
|
|
function signal_strength_sum(io; end_cycle=220)
|
|
|
|
X :: Int = 1
|
|
|
|
X :: Int = 1
|
|
|
|
cycle :: Int = 1
|
|
|
|
cycle :: Int = 1
|
|
|
|
signal_strength_sum :: Int = 0
|
|
|
|
signal_strength_sum :: Int = 0
|
|
|
|
@ -43,10 +43,48 @@ function execute_program(io; end_cycle=220)
|
|
|
|
return signal_strength_sum
|
|
|
|
return signal_strength_sum
|
|
|
|
end
|
|
|
|
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()
|
|
|
|
function test()
|
|
|
|
@testset "signal strength sum" verbose=true begin
|
|
|
|
@testset "signal strength sum" verbose=true begin
|
|
|
|
@test execute_program("example.txt") == 13140
|
|
|
|
@test signal_strength_sum("example.txt") == 13140
|
|
|
|
@test execute_program("input.txt") == 13220
|
|
|
|
@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
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
@ -56,7 +94,10 @@ function main()
|
|
|
|
else
|
|
|
|
else
|
|
|
|
infile = ARGS[1]
|
|
|
|
infile = ARGS[1]
|
|
|
|
println("infile = ", infile)
|
|
|
|
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
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
|