blob: 4895237a59d7098d3ab70710e0d7da4a177f5a86 (
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
|
monkeys = $stdin.readlines
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["root"].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["root"]
|