-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathday_11.py
101 lines (77 loc) · 2.47 KB
/
day_11.py
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
from helper import get_input
from pprint import pprint
import copy
import math
inp = get_input(day=11)
MONKEYS = {}
for i in range(0, len(inp), 7):
monkey = int(inp[i].split(" ")[-1][:-1])
items = [int(x) for x in inp[i + 1].split(":")[-1].split(",")]
operation = inp[i + 2].split("=")[-1].split()
test = [
int(inp[i + 3].split()[-1]),
int(inp[i + 4].split()[-1]),
int(inp[i + 5].split()[-1]),
]
MONKEYS[monkey] = {
"items": items,
"operation": operation,
"test": test,
"inspections": 0,
}
# pprint(monkeys)
def do_operation(item, operation):
operation = [item if x == "old" else x for x in operation]
operation[-1] = int(operation[-1])
v1, op, v2 = operation
res = 0
match op:
case "+":
res = v1 + v2
case "-":
res = v1 - v2
case "*":
res = v1 * v2
case "/":
res = v1 / v2
# print(operation, res)
return res
def do_test(worry, test):
"""
Return which monkey to pass the item
"""
div, t, f = test
if worry % div == 0:
return t
return f
def part_1():
monkeys = copy.deepcopy(MONKEYS)
for _ in range(20):
for monkey, ops in monkeys.items():
for item in ops["items"]:
new_worry = do_operation(item, ops["operation"]) // 3
new_monkey = do_test(new_worry, ops["test"])
monkeys[new_monkey]["items"].append(new_worry)
ops["inspections"] += 1
ops["items"] = []
# for monkey, ops in monkeys.items():
# print("Monkey", monkey, ops["items"])
x, y, *_ = sorted([x["inspections"] for x in monkeys.values()], reverse=True)
return x * y
def part_2():
monkeys = copy.deepcopy(MONKEYS)
mod_lcm = math.lcm(*[x["test"][0] for x in monkeys.values()])
for _ in range(10000):
for monkey, ops in monkeys.items():
for item in ops["items"]:
new_worry = do_operation(item, ops["operation"]) % mod_lcm
new_monkey = do_test(new_worry, ops["test"])
monkeys[new_monkey]["items"].append(new_worry)
ops["inspections"] += 1
ops["items"] = []
# for monkey, ops in monkeys.items():
# print("Monkey", monkey, ops["items"])
x, y, *_ = sorted([x["inspections"] for x in monkeys.values()], reverse=True)
return x * y
print(f"D11P1: {part_1()}")
print(f"D11P2: {part_2()}")