1
0
Fork 0
aoc2022/11/solution.py

48 lines
1.3 KiB
Python
Executable File

#!/usr/bin/env python3
import sys
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"] = [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:
for old in m["items"]:
m["count"] += 1
new = eval(m["op"]) // 3
monkeys[m[not (new % m["test"])]]["items"].append(new)
m["items"].clear()
ohmygodihateyoueric = prod(m["test"] for m in monkeys)
for it in range(10000):
for m in monkeysG:
for old in m["items"]:
m["count"] += 1
new = eval(m["op"]) % ohmygodihateyoueric
monkeysG[m[not (new % m["test"])]]["items"].append(new)
m["items"].clear()
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)}")