1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
instructions = ARGF.read.strip.scan(/(\w+) (dec|inc) (-?\d+) if (\w+) ([!<=>]+) (-?\d+)/m)
max = 0
registers = Hash.new(0)
instructions.each do |instruction|
  condition_value = registers[instruction[-3]]
  if eval("#{condition_value} #{instruction[-2]} #{instruction[-1]}")
    value = registers[instruction[0]]
    case instruction[1]
    when "inc"
      value += instruction[2].to_i
    when "dec"
      value -= instruction[2].to_i
    else
      raise instruction
    end
    registers[instruction[0]] = value
    max = [value, max].max
  end
end
p max