Add day 8 solution.

This commit is contained in:
Danila Fedorin 2020-12-07 21:11:05 -08:00
parent 61713a5faa
commit eafb73cfc1

84
day8.cr Normal file
View File

@ -0,0 +1,84 @@
require "advent"
INPUT = input(2020, 8).lines
def part1
acc = 0
pc = 0
visited = Set(Int32).new
input = INPUT.clone.map do |s|
code, int = s.split(" ")
{code, int.to_i32}
end
loop do
if visited.includes? pc
break
end
visited << pc
code, int = input[pc]
case input[pc][0]
when "acc"
acc += int
pc += 1
when "jmp"
pc += int
when "nop"
pc += 1
end
end
end
def run(prog)
acc = 0
pc = 0
visited = Set(Int32).new
loop do
if visited.includes? pc
return false
end
return acc if pc >= prog.size
visited << pc
code, int = prog[pc]
case prog[pc][0]
when "acc"
acc += int
pc += 1
when "jmp"
pc += int
when "nop"
pc += 1
end
end
end
def part2
input = INPUT.clone.map do |s|
code, int = s.split(" ")
{code, int.to_i32}
end
jnp = [] of Int32
input.each_with_index do |e, i|
op, int = e
jnp << i if op == "jmp" || op == "nop"
end
jnp.each do |i|
prog = input.clone
if prog[i][0] == "jmp"
prog[i] = {"nop", prog[i][1]}
else
prog[i] = {"jmp", prog[i][1]}
end
case i = run(prog)
when Bool
next
when Int32
return i
end
end
end
puts part1
puts part2