From 7b51b5820b86f4f93431745ce9a6df7e28332d85 Mon Sep 17 00:00:00 2001 From: Mia Herkt Date: Sun, 11 Dec 2022 17:09:55 +0100 Subject: [PATCH] 11: Solve in Python --- 11/input | 55 ++++++++++++++++++++++++++++++++++++++++++++++++++ 11/solution.py | 48 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 103 insertions(+) create mode 100644 11/input create mode 100755 11/solution.py diff --git a/11/input b/11/input new file mode 100644 index 0000000..dd12745 --- /dev/null +++ b/11/input @@ -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 diff --git a/11/solution.py b/11/solution.py new file mode 100755 index 0000000..37586ce --- /dev/null +++ b/11/solution.py @@ -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)}")