Solution for Day 08

This commit is contained in:
2020-12-08 15:17:53 +01:00
parent c6c6877778
commit 08d6eb34a4
3 changed files with 720 additions and 0 deletions

41
day_08/second.rb Normal file
View File

@ -0,0 +1,41 @@
# frozen_string_literal: true
input = File.readlines('./input').map(&:split)
def execute(c)
pc = acc = 0
visited = []
while pc < c.size
instr = c[pc][0]
val = Integer(c[pc][1])
break if visited.include? pc
visited << pc
case instr
when 'acc'
acc += val
pc += 1
when 'jmp'
pc += val
when 'nop'
pc += 1
end
end
[pc, acc]
end
c.each_with_index do |instr, i|
next if instr[0] == 'acc'
c[i][0] = instr[0] == 'jmp' ? 'nop' : 'jmp'
pc, acc = execute(c)
c[i][0] = instr[0] == 'jmp' ? 'nop' : 'jmp'
break acc if pc >= c.size
end
puts acc