aboutsummaryrefslogtreecommitdiff
path: root/day21/part2.rb
blob: ab8869990c19bc18995300514bd786b2d3b1f744 (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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
def mutate(eqn, tgt)
  if eqn[6..9] == tgt
    op = case eqn[11]
    when ?+; ?-
    when ?-; ?+
    when ?*; ?/
    when ?/; ?*
    end
    "#{eqn[6..9]}: #{eqn[0..3]} #{op} #{eqn[13..16]}"
  else
    case eqn[11]
    when ?+
      "#{eqn[13..16]}: #{eqn[0..3]} - #{eqn[6..9]}"
    when ?-
      "#{eqn[13..16]}: #{eqn[6..9]} - #{eqn[0..3]}"
    when ?*
      "#{eqn[13..16]}: #{eqn[0..3]} / #{eqn[6..9]}"
    when ?/
      "#{eqn[13..16]}: #{eqn[6..9]} / #{eqn[0..3]}"
    end
  end
end

monkeys = $stdin.readlines.reject { |l| l[0...4] == "humn" }

prev = nil
need = "humn"
loop do
  cand = monkeys.
          select { |m| m.include?(need) }.
          reject { |m| m[0...4] == prev }[0]
  cix = monkeys.find_index(cand)
  break if cand[0...4] == "root"
  monkeys[cix] = mutate(cand, need)
  prev = need
  need = cand[0...4]
end

root = monkeys.select { |m| m[0...4] == "root" }[0]
candname = (root[6..9] == need ? root[13..16] : root[6..9])
monkeys.delete(root)

cand = monkeys.select { |m| m[0...4] == candname }[0]
cix = monkeys.index(cand)
monkeys[cix] = "#{need}#{cand[4..16]}"

nums = {}

monkeys.count.times do |mix|
  if monkeys[mix] =~ /(\d+)/
    val = $1.to_i
    nums[monkeys[mix][0...4]] = $1.to_i
    monkeys[mix] = nil
  end
end
monkeys.compact!

while nums["humn"].nil?
  monkeys.count.times do |mix|
    f, a, b = monkeys[mix].scan(/[a-z]{4}/)
    unless nums[a].nil? || nums[b].nil?
      case monkeys[mix][11]
      when ?+
        nums[f] = nums[a] + nums [b]
      when ?-
        nums[f] = nums[a] - nums [b]
      when ?*
        nums[f] = nums[a] * nums [b]
      when ?/
        nums[f] = nums[a] / nums [b]
      end
      monkeys[mix] = nil
    end
  end
  monkeys.compact!
end

puts nums["humn"]