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