[2017][ruby][8.x] speedrun
change prvsyunkvmrzunywulolyvqltyuykmlr
commit c847fb1fe4761026518cb2fb7e60a3721d7dfdfa
author Alpha Chen <alpha.chen@gmail.com>
date
parent utoruqvt
diff --git a/2017/ruby/day_08.rb b/2017/ruby/day_08.rb
new file mode 100644
index 0000000..a226c83
--- /dev/null
+++ b/2017/ruby/day_08.rb
@@ -0,0 +1,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