From 7cdd0fc25b2947f351a4a527fa60efa3753cbf7a Mon Sep 17 00:00:00 2001 From: Vito Minheere Date: Sun, 11 Dec 2022 14:05:24 +0100 Subject: [PATCH] Solution for part 2 --- 2022/d11/solution.py | 65 ++++++++++++++++++++++++++++++++++---------- 1 file changed, 51 insertions(+), 14 deletions(-) diff --git a/2022/d11/solution.py b/2022/d11/solution.py index dd1b9fc..b9b9a3f 100644 --- a/2022/d11/solution.py +++ b/2022/d11/solution.py @@ -1,4 +1,5 @@ import time +from math import prod class Monkey: @@ -27,10 +28,21 @@ def set_test_false(self, monkey): self.test_false = int(monkey) def operation(self, worry_level: int): - return eval(f"{worry_level} {self.arith}") + self.items_inspected += 1 + if "old" in self.arith: + arith = self.arith.replace("old", str(worry_level)) + else: + arith = self.arith + return eval(f"{worry_level} {arith}") + def get_next_monkey(self, worry_level): + res = eval(f"{worry_level} {self.test} == 0") + return (self.test_false, self.test_true)[res] -def p1(data): + + +def p1(data, rounds): + rounds = rounds worry_level = 0 am_monkeys = int(data[-6].split(" ")[-1][:1])+1 @@ -44,10 +56,9 @@ def p1(data): items = [int(x) for x in line.split(":")[-1].split(",")] monkeys[m].append_items(items) elif "Operation" in line: - print(line.split("=")[-1][4:]) - monkeys[m].set_arithmetic("".join(line.split("=")[-1][4:]).replace("old", str(worry_level))) + monkeys[m].set_arithmetic("".join(line.split("=")[-1][4:])) elif "Test" in line: - monkeys[m].set_test(f"/ {line.split(' ')[-1]}") + monkeys[m].set_test(f"% {line.split(' ')[-1]}") elif "true" in line: monkeys[m].set_test_true(line.split(" ")[-1]) elif "false" in line: @@ -56,19 +67,45 @@ def p1(data): start += 7 end += 7 - for monkey in monkeys: - print(monkey.__dict__) - -def p2(data): - pass - -with open("test.txt") as t: + mod = prod([int(x.test.split(" ")[-1]) for x in monkeys]) + + for r in range(1, rounds+1): + for i, m in enumerate(monkeys): + # print("Monkey " + str(i)) + for _, item in enumerate(m.items): + worry_level = item + # print(f"Monkey inspects an item with a worry level of {item}") + worry_level = m.operation(item) + # print(f"Worry level = " + str(worry_level)) + if rounds == 20: + worry_level = worry_level // 3 + else: + worry_level = worry_level % mod + # print(f"Monkey gets bored with item. Worry level is divided by 3 to {worry_level}.") + next = m.get_next_monkey(worry_level) + # print(f"Item with worry level {worry_level} is thrown to monkey {str(next)}.") + monkeys[next].append_items([worry_level]) + m.items = [] + + active = sorted(monkeys, key=lambda x: x.items_inspected, reverse=True) + for i, x in enumerate(active): + print(f"Monkey {str(i)} inspected items {str(x.items_inspected)} times.") + + p1 = 1 + for x in active[:2]: + print(x.items_inspected) + p1 *= x.items_inspected + + print("P1 = " + str(p1)) + + +with open("input.txt") as t: data = t.read().splitlines() start = time.time() - p1(data) + p1(data, 20) p1_time = time.time() - start p2_start = time.time() - p2(data) + p1(data, 10000) p2_time = time.time() - p2_start print("P1 took " + str(round(p1_time * 1000)) + " ms")