aboutsummaryrefslogtreecommitdiff
path: root/day08/part1
blob: 167e761d8d886f473037f3c4cda8889846823289 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#!/usr/bin/env ruby

input = $stdin.readlines.map(&:chomp)
h = {}

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
end

puts h.to_a.map(&:last).max