1
0
Fork 0

11: Solve in Python

This commit is contained in:
Mia Herkt 2022-12-11 17:09:55 +01:00
parent e2ba0de6f8
commit 7b51b5820b
Signed by: mia
GPG key ID: 72E154B8622EC191
2 changed files with 103 additions and 0 deletions

55
11/input Normal file
View file

@ -0,0 +1,55 @@
Monkey 0:
Starting items: 61
Operation: new = old * 11
Test: divisible by 5
If true: throw to monkey 7
If false: throw to monkey 4
Monkey 1:
Starting items: 76, 92, 53, 93, 79, 86, 81
Operation: new = old + 4
Test: divisible by 2
If true: throw to monkey 2
If false: throw to monkey 6
Monkey 2:
Starting items: 91, 99
Operation: new = old * 19
Test: divisible by 13
If true: throw to monkey 5
If false: throw to monkey 0
Monkey 3:
Starting items: 58, 67, 66
Operation: new = old * old
Test: divisible by 7
If true: throw to monkey 6
If false: throw to monkey 1
Monkey 4:
Starting items: 94, 54, 62, 73
Operation: new = old + 1
Test: divisible by 19
If true: throw to monkey 3
If false: throw to monkey 7
Monkey 5:
Starting items: 59, 95, 51, 58, 58
Operation: new = old + 3
Test: divisible by 11
If true: throw to monkey 0
If false: throw to monkey 4
Monkey 6:
Starting items: 87, 69, 92, 56, 91, 93, 88, 73
Operation: new = old + 8
Test: divisible by 3
If true: throw to monkey 5
If false: throw to monkey 2
Monkey 7:
Starting items: 71, 57, 86, 67, 96, 95
Operation: new = old + 7
Test: divisible by 17
If true: throw to monkey 3
If false: throw to monkey 1

48
11/solution.py Executable file
View file

@ -0,0 +1,48 @@
#!/usr/bin/env python3
import sys
from collections import deque
from copy import deepcopy
from math import prod
monkeys = []
for l in sys.stdin:
l = l.strip()
if l.startswith("M"):
monkeys.append({})
monkeys[-1]["count"] = 0
elif l.startswith("S"):
monkeys[-1]["items"] = deque(int(x) for x in l[16:].split(", "))
elif l.startswith("O"):
monkeys[-1]["op"] = l[17:]
elif l.startswith("T"):
monkeys[-1]["test"] = int(l[19:])
elif l.startswith("If t"):
monkeys[-1][True] = int(l[25:])
elif l.startswith("If f"):
monkeys[-1][False] = int(l[26:])
monkeysG = deepcopy(monkeys)
for it in range(20):
for m in monkeys:
while len(m["items"]):
m["count"] += 1
old = m["items"].popleft()
new = eval(m["op"]) // 3
monkeys[m[not (new % m["test"])]]["items"].append(new)
ohmygodihateyoueric = prod(m["test"] for m in monkeys)
for it in range(10000):
for m in monkeysG:
while len(m["items"]):
m["count"] += 1
old = m["items"].popleft()
new = eval(m["op"]) % ohmygodihateyoueric
monkeysG[m[not (new % m["test"])]]["items"].append(new)
resS = sorted(m["count"] for m in monkeys)[-2:]
resG = sorted(m["count"] for m in monkeysG)[-2:]
print(f"Silver: {prod(resS)}\nGold: {prod(resG)}")