2020-12-07 21:11:05 -08:00
|
|
|
require "advent"
|
2020-12-11 11:32:01 -08:00
|
|
|
require "./console.cr"
|
2020-12-07 21:11:05 -08:00
|
|
|
|
2020-12-11 11:32:01 -08:00
|
|
|
INPUT = input(2020, 8)
|
2020-12-07 21:11:05 -08:00
|
|
|
|
2020-12-07 23:38:26 -08:00
|
|
|
def part1
|
2020-12-11 11:32:01 -08:00
|
|
|
run(parse(INPUT))[1].receive[1]
|
2020-12-07 23:38:26 -08:00
|
|
|
end
|
2020-12-07 21:11:05 -08:00
|
|
|
|
2020-12-07 23:38:26 -08:00
|
|
|
def part2
|
2020-12-11 11:32:01 -08:00
|
|
|
input = parse(INPUT)
|
|
|
|
jnp = input.find_indices { |e| e[0] == "jmp" || e[0] == "nop" }
|
2020-12-07 21:11:05 -08:00
|
|
|
jnp.each do |i|
|
2020-12-11 11:32:01 -08:00
|
|
|
prog = input.clone
|
2020-12-07 23:38:26 -08:00
|
|
|
op, int = prog[i]
|
|
|
|
prog[i] = {op.tr("jmpnop", "nopjmp"), int}
|
2020-12-07 21:11:05 -08:00
|
|
|
|
2020-12-11 11:32:01 -08:00
|
|
|
code, acc = run(prog)[1].receive
|
2020-12-07 23:38:26 -08:00
|
|
|
return acc if code == :term
|
2020-12-07 21:11:05 -08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
puts part1
|
|
|
|
puts part2
|