Skip to content

Commit 7d92fd1

Browse files
committed
2022: Day 11
1 parent d0846e3 commit 7d92fd1

File tree

3 files changed

+144
-0
lines changed

3 files changed

+144
-0
lines changed

2022/11/input.txt

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

2022/11/main.py

+88
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
from math import prod, ceil
2+
3+
4+
class Monkey:
5+
def __init__(self) -> None:
6+
self.id = 0
7+
self.items = []
8+
self.operation = ""
9+
self.test = {}
10+
self.inspected = 0
11+
12+
def get_first_item(self) -> int:
13+
self.inspected += 1
14+
return self.items.pop(0)
15+
16+
17+
class Day11:
18+
19+
def part_one(self) -> int:
20+
puzzle_input, _ = self.load_input()
21+
return self.get_most_active(monkeys=puzzle_input, rounds=20)
22+
23+
def part_two(self) -> int:
24+
puzzle_input, LCM = self.load_input()
25+
return self.get_most_active(monkeys=puzzle_input, rounds=10000, least_common_multiple=LCM)
26+
27+
def get_most_active(self, monkeys: list, rounds: int, least_common_multiple: int = None) -> int:
28+
for _ in range(rounds):
29+
for monkey in monkeys:
30+
for _ in range(len(monkey.items)):
31+
item = monkey.get_first_item()
32+
33+
right = monkey.operation.split()[-1]
34+
left = monkey.operation.split()[-3]
35+
operator = monkey.operation.split()[-2]
36+
37+
operators = {'+': lambda x, y: x + y,
38+
'-': lambda x, y: x - y,
39+
'*': lambda x, y: x * y}
40+
41+
right = item if right == 'old' else int(right)
42+
left = item if left == 'old' else int(left)
43+
44+
item = operators[operator](right, left)
45+
46+
if least_common_multiple:
47+
item = item % least_common_multiple
48+
else:
49+
item //= 3
50+
51+
throw_to = item % monkey.test['divisible'] == 0
52+
monkeys[monkey.test[throw_to]].items.append(item)
53+
54+
inspected = sorted([monkey.inspected for monkey in monkeys])
55+
56+
return inspected[-1] * inspected[-2]
57+
58+
@staticmethod
59+
def load_input() -> tuple[list, int]:
60+
monkeys, divisibles = [], []
61+
with open("input.txt") as file:
62+
lines = file.read().splitlines()
63+
num_monkeys = ceil(len(lines)/7)
64+
for i in range(0, num_monkeys):
65+
row_start = i * 7
66+
rows = lines[row_start:row_start + 7]
67+
68+
monkey = Monkey()
69+
monkey.id = rows[0].replace(":", "").split()[-1]
70+
71+
starting_items = map(int, rows[1].replace(",", "").split(":")[-1].split())
72+
monkey.items.extend(starting_items)
73+
74+
monkey.operation = rows[2].split(":")[-1].strip()
75+
monkey.test = {
76+
'divisible': int(rows[3].split()[-1]),
77+
True: int(rows[4].split()[-1]),
78+
False: int(rows[5].split()[-1])
79+
}
80+
divisibles.append(monkey.test['divisible'])
81+
monkeys.append(monkey)
82+
83+
return monkeys, prod(divisibles)
84+
85+
86+
day = Day11()
87+
print(f'Result part 1: {day.part_one()}')
88+
print(f'Result part 2: {day.part_two()}')

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ My Python Solutions to [Advent of Code](https://adventofcode.com/)
1414
|8 |Treetop Tree House||| [🔗](2022/08/main.py) |
1515
|9 |Rope Bridge||| [🔗](2022/09/main.py) |
1616
|10 |Cathode-Ray Tube||| [🔗](2022/10/main.py) |
17+
|11 |Monkey in the Middle||| [🔗](2022/11/main.py) |
1718

1819
## [2021](https://adventofcode.com/2021)
1920
| Day | Name | Part 1 | Part 2 | Code |

0 commit comments

Comments
 (0)