This commit is contained in:
Alexander Munch-Hansen 2017-12-09 21:29:20 +01:00
parent 0581fd9c88
commit e19f3e4865

25
i_like_registers.rb Normal file
View File

@ -0,0 +1,25 @@
input = File.read("input_8.txt").split("\n")
regexp = /([a-z]+) (inc|dec) (-?\d+) if ([a-z]+) (.+) (-?\d+)/
res = Hash.new 0
parsed = input.map do |s|
meh = s.match(regexp).captures
[meh[0].to_sym, meh[1].to_sym, meh[2].to_i, meh[3].to_sym, meh[4].to_sym, meh[5].to_i]
end
max = 0
parsed.each do |parsed_i|
if res[parsed_i[3]].send(parsed_i[4], parsed_i[5]) then
case parsed_i[1]
when :inc
res[parsed_i[0]] += parsed_i[2]
when :dec
res[parsed_i[0]] -= parsed_i[2]
end
if res[parsed_i[0]] > max then max = res[parsed_i[0]] end
end
end
p res.values.max
p max