AdventOfCode-2020/day8.cr

25 lines
426 B
Crystal
Raw Normal View History

2020-12-07 21:11:05 -08:00
require "advent"
require "./console.cr"
2020-12-07 21:11:05 -08:00
INPUT = input(2020, 8)
2020-12-07 21:11:05 -08:00
2020-12-07 23:38:26 -08:00
def part1
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
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|
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
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