diff --git a/i_like_registers.rb b/i_like_registers.rb new file mode 100644 index 0000000..3833914 --- /dev/null +++ b/i_like_registers.rb @@ -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 +