aboutsummaryrefslogtreecommitdiff
path: root/day21/part1.rb
diff options
context:
space:
mode:
authorNat Lasseter <user@4574.co.uk>2022-12-21 15:19:10 +0000
committerNat Lasseter <user@4574.co.uk>2022-12-21 15:19:10 +0000
commit6734e8dee0ff45229100bfbed9935ce080639981 (patch)
tree32373e1eb73eaaef7040c8751cd7ad067bec85d4 /day21/part1.rb
parent05d62a081d1cc9ea50a0ec5b1bb7ce3f6c726545 (diff)
Day 21
Diffstat (limited to 'day21/part1.rb')
-rw-r--r--day21/part1.rb34
1 files changed, 34 insertions, 0 deletions
diff --git a/day21/part1.rb b/day21/part1.rb
new file mode 100644
index 0000000..4895237
--- /dev/null
+++ b/day21/part1.rb
@@ -0,0 +1,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"]