aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNat Lasseter <user@4574.co.uk>2022-12-11 12:53:22 +0000
committerNat Lasseter <user@4574.co.uk>2022-12-11 12:53:22 +0000
commitf1f4d07d5784d0bf2b63bd2206c7d5f5f7e76479 (patch)
tree7823590867edcf16ca9c13f111e642257cae1f3c
parent1128a670513a77fa62cba789d5b4c942e3b46c5d (diff)
Day 11
-rw-r--r--day11/input55
-rw-r--r--day11/part1.rb66
-rw-r--r--day11/part2.rb72
-rw-r--r--day11/test27
4 files changed, 220 insertions, 0 deletions
diff --git a/day11/input b/day11/input
new file mode 100644
index 0000000..3e490d2
--- /dev/null
+++ b/day11/input
@@ -0,0 +1,55 @@
+Monkey 0:
+ Starting items: 91, 54, 70, 61, 64, 64, 60, 85
+ Operation: new = old * 13
+ Test: divisible by 2
+ If true: throw to monkey 5
+ If false: throw to monkey 2
+
+Monkey 1:
+ Starting items: 82
+ Operation: new = old + 7
+ Test: divisible by 13
+ If true: throw to monkey 4
+ If false: throw to monkey 3
+
+Monkey 2:
+ Starting items: 84, 93, 70
+ Operation: new = old + 2
+ Test: divisible by 5
+ If true: throw to monkey 5
+ If false: throw to monkey 1
+
+Monkey 3:
+ Starting items: 78, 56, 85, 93
+ Operation: new = old * 2
+ Test: divisible by 3
+ If true: throw to monkey 6
+ If false: throw to monkey 7
+
+Monkey 4:
+ Starting items: 64, 57, 81, 95, 52, 71, 58
+ Operation: new = old * old
+ Test: divisible by 11
+ If true: throw to monkey 7
+ If false: throw to monkey 3
+
+Monkey 5:
+ Starting items: 58, 71, 96, 58, 68, 90
+ Operation: new = old + 6
+ Test: divisible by 17
+ If true: throw to monkey 4
+ If false: throw to monkey 1
+
+Monkey 6:
+ Starting items: 56, 99, 89, 97, 81
+ Operation: new = old + 1
+ Test: divisible by 7
+ If true: throw to monkey 0
+ If false: throw to monkey 2
+
+Monkey 7:
+ Starting items: 68, 72
+ Operation: new = old + 8
+ Test: divisible by 19
+ If true: throw to monkey 6
+ If false: throw to monkey 0
diff --git a/day11/part1.rb b/day11/part1.rb
new file mode 100644
index 0000000..f4890bc
--- /dev/null
+++ b/day11/part1.rb
@@ -0,0 +1,66 @@
+class Monkey
+ def initialize(items, op, test, iftrue, iffalse)
+ @items = items
+ @op = op
+ @test = test
+ @iftrue = iftrue
+ @iffalse = iffalse
+ @inspections = 0
+ end
+
+ attr_reader :items, :inspections
+
+ def <<(item)
+ @items << item
+ end
+
+ def turn
+ @inspections += @items.count
+ ret = []
+ until @items.empty?
+ old = @items.shift
+ new = eval(@op) / 3
+ ret << [(new % @test == 0 ? @iftrue : @iffalse), new]
+ end
+ ret
+ end
+end
+
+class Game
+ def initialize
+ @monkeys = []
+ end
+
+ def <<(monkey)
+ @monkeys << monkey
+ end
+
+ def round
+ @monkeys.each do |monkey|
+ monkey.turn.each do |tom, val|
+ @monkeys[tom] << val
+ end
+ end
+ end
+
+ def inspections
+ @monkeys.map(&:inspections)
+ end
+end
+
+lines = $stdin.readlines.map(&:strip).reject(&:empty?)
+
+game = Game.new
+lines.each_slice(6) do |mkls|
+ items = mkls[1].scan(/\d+/).map(&:to_i)
+ op = mkls[2].split(" = ")[1]
+ test = mkls[3].scan(/\d+/)[0].to_i
+ ift = mkls[4].scan(/\d+/)[0].to_i
+ iff = mkls[5].scan(/\d+/)[0].to_i
+ game << Monkey.new(items, op, test, ift, iff)
+end
+
+20.times do
+ game.round
+end
+puts game.inspections.max(2).inject(&:*)
diff --git a/day11/part2.rb b/day11/part2.rb
new file mode 100644
index 0000000..0be5a5c
--- /dev/null
+++ b/day11/part2.rb
@@ -0,0 +1,72 @@
+class Monkey
+ def initialize(items, op, test, iftrue, iffalse)
+ @items = items
+ @op = op
+ @test = test
+ @iftrue = iftrue
+ @iffalse = iffalse
+ @inspections = 0
+ end
+
+ attr_reader :items, :inspections
+
+ def <<(item)
+ @items << item
+ end
+
+ def turn(wl)
+ @inspections += @items.count
+ ret = []
+ until @items.empty?
+ old = @items.shift
+ new = eval(@op) % wl
+ ret << [(new % @test == 0 ? @iftrue : @iffalse), new]
+ end
+ ret
+ end
+end
+
+class Game
+ def initialize
+ @monkeys = []
+ @worry_limit = 1
+ end
+
+ attr_accessor :worry_limit
+
+ def <<(monkey)
+ @monkeys << monkey
+ end
+
+ def round
+ @monkeys.each do |monkey|
+ monkey.turn(@worry_limit).each do |tom, val|
+ @monkeys[tom] << val
+ end
+ end
+ end
+
+ def inspections
+ @monkeys.map(&:inspections)
+ end
+end
+
+lines = $stdin.readlines.map(&:strip).reject(&:empty?)
+
+game = Game.new
+wl = 1
+lines.each_slice(6) do |mkls|
+ items = mkls[1].scan(/\d+/).map(&:to_i)
+ op = mkls[2].split(" = ")[1]
+ test = mkls[3].scan(/\d+/)[0].to_i
+ wl *= test
+ ift = mkls[4].scan(/\d+/)[0].to_i
+ iff = mkls[5].scan(/\d+/)[0].to_i
+ game << Monkey.new(items, op, test, ift, iff)
+end
+game.worry_limit = wl
+
+10_000.times do |i|
+ game.round
+end
+puts game.inspections.max(2).inject(&:*)
diff --git a/day11/test b/day11/test
new file mode 100644
index 0000000..30e09e5
--- /dev/null
+++ b/day11/test
@@ -0,0 +1,27 @@
+Monkey 0:
+ Starting items: 79, 98
+ Operation: new = old * 19
+ Test: divisible by 23
+ If true: throw to monkey 2
+ If false: throw to monkey 3
+
+Monkey 1:
+ Starting items: 54, 65, 75, 74
+ Operation: new = old + 6
+ Test: divisible by 19
+ If true: throw to monkey 2
+ If false: throw to monkey 0
+
+Monkey 2:
+ Starting items: 79, 60, 97
+ Operation: new = old * old
+ Test: divisible by 13
+ If true: throw to monkey 1
+ If false: throw to monkey 3
+
+Monkey 3:
+ Starting items: 74
+ Operation: new = old + 3
+ Test: divisible by 17
+ If true: throw to monkey 0
+ If false: throw to monkey 1