aboutsummaryrefslogtreecommitdiff
path: root/day08/part2
diff options
context:
space:
mode:
authorNat Lasseter <Nat Lasseter nathan@bytemark.co.uk>2017-12-08 17:18:19 +0000
committerNat Lasseter <Nat Lasseter nathan@bytemark.co.uk>2017-12-08 17:18:19 +0000
commit4a22c4e119c6cad69bdf7687d509f69e391f33c0 (patch)
tree2576e85959ad7c95ea34c49e101ea06f14508181 /day08/part2
parent546c50a3d17551aeee259c122b02bfd68673277a (diff)
Day 08
Diffstat (limited to 'day08/part2')
-rwxr-xr-xday08/part236
1 files changed, 36 insertions, 0 deletions
diff --git a/day08/part2 b/day08/part2
new file mode 100755
index 0000000..a19d462
--- /dev/null
+++ b/day08/part2
@@ -0,0 +1,36 @@
+#!/usr/bin/env ruby
+
+input = $stdin.readlines.map(&:chomp)
+h = {}
+max = 0
+
+input.each do |line|
+ cmd, cond = line.split(' if ')
+
+ cmda = cmd.split
+ reg = cmda[0]
+ h[reg] = 0 if h[reg].nil?
+ chg = (cmda[1] == 'inc' ? cmda[2].to_i : -cmda[2].to_i)
+
+ conda = cond.split
+ h[conda[0]] = 0 if h[conda[0]].nil?
+ go = case conda[1]
+ when '<'
+ h[conda[0]] < conda[2].to_i
+ when '<='
+ h[conda[0]] <= conda[2].to_i
+ when '=='
+ h[conda[0]] == conda[2].to_i
+ when '>='
+ h[conda[0]] >= conda[2].to_i
+ when '>'
+ h[conda[0]] > conda[2].to_i
+ when '!='
+ h[conda[0]] != conda[2].to_i
+ end
+ h[reg] += chg if go
+ nmax = h.to_a.map(&:last).max
+ max = nmax if nmax > max
+end
+
+puts max