Skip to content

Commit fadee6a

Browse files
committed
Add day 11
1 parent 4867767 commit fadee6a

File tree

5 files changed

+103
-1
lines changed

5 files changed

+103
-1
lines changed

2022/11/11.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import sys, math, operator
2+
3+
monkeys = []
4+
5+
class Monkey:
6+
modulo = 1
7+
def __init__(self, monkey: str):
8+
_, items, op, test, if_t, if_f = monkey.split("\n")
9+
self.items = [int(a) for a in items.split(":")[1].split(',')]
10+
ops = {"*": operator.mul, "+": operator.add}
11+
*_, op, arg = op.split(":")[1].split()
12+
self.op = lambda x: ops[op](x, x if arg == "old" else int(arg))
13+
self.divisible_by = int(test.split()[-1])
14+
Monkey.modulo *= self.divisible_by
15+
self.if_t = int(if_t.split()[-1])
16+
self.if_f = int(if_f.split()[-1])
17+
self.passes = 0
18+
19+
def round(self, div_by_3: bool):
20+
for item in self.items:
21+
new_worry = self.op(item) // (3 if div_by_3 else 1) % Monkey.modulo
22+
self.passes += 1
23+
new_index = self.if_t if new_worry % self.divisible_by == 0 else self.if_f
24+
monkeys[new_index].items.append(new_worry)
25+
self.items.clear()
26+
27+
28+
monkeys2 = []
29+
for i, monkey in enumerate(sys.stdin.read().strip().split("\n\n")):
30+
monkeys.append(Monkey(monkey))
31+
monkeys2.append(Monkey(monkey))
32+
33+
for i in range(20):
34+
for monkey in monkeys:
35+
monkey.round(True)
36+
print(math.prod(sorted([a.passes for a in monkeys])[-2:]))
37+
38+
monkeys = monkeys2
39+
for i in range(10000):
40+
for monkey in monkeys:
41+
monkey.round(False)
42+
print(math.prod(sorted([a.passes for a in monkeys])[-2:]))

2022/11/input.ans

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
108240
2+
25712998901

2022/11/input.in

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
Monkey 0:
2+
Starting items: 83, 97, 95, 67
3+
Operation: new = old * 19
4+
Test: divisible by 17
5+
If true: throw to monkey 2
6+
If false: throw to monkey 7
7+
8+
Monkey 1:
9+
Starting items: 71, 70, 79, 88, 56, 70
10+
Operation: new = old + 2
11+
Test: divisible by 19
12+
If true: throw to monkey 7
13+
If false: throw to monkey 0
14+
15+
Monkey 2:
16+
Starting items: 98, 51, 51, 63, 80, 85, 84, 95
17+
Operation: new = old + 7
18+
Test: divisible by 7
19+
If true: throw to monkey 4
20+
If false: throw to monkey 3
21+
22+
Monkey 3:
23+
Starting items: 77, 90, 82, 80, 79
24+
Operation: new = old + 1
25+
Test: divisible by 11
26+
If true: throw to monkey 6
27+
If false: throw to monkey 4
28+
29+
Monkey 4:
30+
Starting items: 68
31+
Operation: new = old * 5
32+
Test: divisible by 13
33+
If true: throw to monkey 6
34+
If false: throw to monkey 5
35+
36+
Monkey 5:
37+
Starting items: 60, 94
38+
Operation: new = old + 5
39+
Test: divisible by 3
40+
If true: throw to monkey 1
41+
If false: throw to monkey 0
42+
43+
Monkey 6:
44+
Starting items: 81, 51, 85
45+
Operation: new = old * old
46+
Test: divisible by 5
47+
If true: throw to monkey 5
48+
If false: throw to monkey 1
49+
50+
Monkey 7:
51+
Starting items: 98, 81, 63, 65, 84, 71, 84
52+
Operation: new = old + 3
53+
Test: divisible by 2
54+
If true: throw to monkey 2
55+
If false: throw to monkey 3

Media/2022/11.png

7.24 KB
Loading

README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
<!-- AOC TILES BEGIN -->
66
<h1 align="center">
7-
2022 - 20
7+
2022 - 22
88
</h1>
99
<a href="2022/01/01.kt">
1010
<img src="Media/2022/01.png" width="161px">
@@ -36,6 +36,9 @@
3636
<a href="2022/10/10.kt">
3737
<img src="Media/2022/10.png" width="161px">
3838
</a>
39+
<a href="2022/11/11.py">
40+
<img src="Media/2022/11.png" width="161px">
41+
</a>
3942
<h1 align="center">
4043
2021 - 50 ⭐
4144
</h1>

0 commit comments

Comments
 (0)