-
Notifications
You must be signed in to change notification settings - Fork 0
/
a11.py
55 lines (44 loc) · 1.82 KB
/
a11.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
import re
def play(rounds, bleh):
#1. Parse input
lines = iter(open("11.txt").read().splitlines())
tryInt = lambda list: [int(x) if len(x) and x[0].isdigit() else x for x in list]
monkeys = []
multiplier = 1
while True:
monkey = {}
next(lines) #Monkey 0:
monkey["items"] = list(map(int,re.findall('\d+', next(lines)))) # Starting items: 79, 98
monkey["op"] = tryInt(next(lines).split(" "))[-2:] # Operation: new = old * 19
monkey["div"] = tryInt(next(lines).split(" "))[-1] # Test: divisible by 23
monkey["true"] = tryInt(next(lines).split(" "))[-1] # If true: throw to monkey 1
monkey["false"] = tryInt(next(lines).split(" "))[-1] # If false: throw to monkey 4
monkey["inspections"] = 0
multiplier *= monkey["div"]
monkeys.append(monkey)
try: next(lines)
except: break
#2. Play!
for round in range(rounds):
for i, monkey in enumerate(monkeys):
for worry in monkey["items"]:
monkey["inspections"] = monkey["inspections"] + 1
op, val = monkey["op"]
if val == "old": val = worry
if op == '*': worry = worry * val
elif op == "+": worry = worry + val
elif op == "-": worry = worry - val
if bleh:
worry = worry % multiplier
else:
worry = worry // 3
throwto = monkey["false"] if worry % monkey["div"] else monkey["true"]
monkeys[throwto]["items"].append(worry)
monkey["items"] = []
s = sorted([monkey["inspections"] for monkey in monkeys])
return s[-1] * s[-2]
# calculate stuff
p1 = play(20, False)
print("Part 1:", p1)
p2 = play(10000, True)
print("Part 2:", p2)