From e19f3e486548e96d2ee4d7e2d4fb3a80347bf9c2 Mon Sep 17 00:00:00 2001 From: Alexander Munch-Hansen Date: Sat, 9 Dec 2017 21:29:20 +0100 Subject: [PATCH] Day 8 --- i_like_registers.rb | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 i_like_registers.rb 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 +