-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday11-Monkey in the Middle.py
72 lines (62 loc) · 1.83 KB
/
day11-Monkey in the Middle.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
from collections import defaultdict
f = open('input')
lines = f.readlines()
lines = [_.strip() for _ in lines]
lines.append("")
OPS_1 = {
0: lambda x: x * 19,
1: lambda x: x + 6,
2: lambda x: x ** 2,
3: lambda x: x + 3
}
# pls adjust below dict to your test case
OPS_2 = {
0: lambda x: x * 11,
1: lambda x: x + 8,
2: lambda x: x * 3,
3: lambda x: x + 4,
4: lambda x: x ** 2,
5: lambda x: x + 2,
6: lambda x: x + 3,
7: lambda x: x + 5,
}
FN = OPS_2
MONKEYS = defaultdict(list)
DIV = dict()
for line in lines:
if line.startswith('Monkey'):
monkey = int(line[7])
elif line.startswith("Starting items:"):
starting_items = list(map(int, line[16:].split(', ')))
elif line.startswith("Test: divisible by "):
divisible = int(line[19:])
elif line.startswith('If true: throw to monkey '):
trueTo = int(line[25:])
elif line.startswith('If false: throw to monkey '):
falseTo = int(line[26:])
elif line == "":
MONKEYS[monkey].extend(starting_items)
DIV[monkey] = (divisible, trueTo, falseTo)
print(f' Monkey: {monkey}'
f' starting items: {starting_items}'
f' divisible: {divisible}'
f' True: {trueTo}, False: {falseTo}')
MOD = 1
for prime, _, _ in DIV.values():
MOD *= prime
print(MOD)
inspect = [0 for _ in range(len(MONKEYS))]
for i in range(10000):
for monkey in MONKEYS:
inspect[monkey] += len(MONKEYS[monkey])
for item in MONKEYS[monkey]:
worry = FN[monkey](item) % MOD
if worry % DIV[monkey][0] == 0:
nextMonkey = DIV[monkey][1]
else:
nextMonkey = DIV[monkey][2]
MONKEYS[nextMonkey].append(worry)
MONKEYS[monkey].clear()
print(inspect)
inspect.sort()
print(inspect[-1] * inspect[-2])