From 06a09afccdd981afdcb7f5d1b2d032e2d9a8caa0 Mon Sep 17 00:00:00 2001 From: "matthias@arch" Date: Mon, 12 Dec 2022 13:49:44 +0100 Subject: [PATCH] day 11 task 2 --- 11/README.md | 8 ++++++++ 11/day11.py | 33 ++++++++++++++++++++++++--------- README.md | 2 +- 3 files changed, 33 insertions(+), 10 deletions(-) create mode 100644 11/README.md diff --git a/11/README.md b/11/README.md new file mode 100644 index 0000000..2cac3eb --- /dev/null +++ b/11/README.md @@ -0,0 +1,8 @@ +# [Day 11](https://adventofcode.com/2022/day/11) +:gift::gift::gift::gift::gift::gift::gift::gift::gift::gift::gift: + +Today's language: **Python** + +```shell +python3 day11.py monkey-stuff.txt +``` diff --git a/11/day11.py b/11/day11.py index bd1cecb..a3fa985 100644 --- a/11/day11.py +++ b/11/day11.py @@ -1,10 +1,11 @@ from sys import argv from re import finditer - -ROUND_COUNT = 10000 +from copy import deepcopy class Monkey: monkeys = [] + common_divisor = 1 + worry_level_decrease = 1 def __init__(self, start_items:list[int], divisible_by:int, monkey_on_true: int, monkey_on_false: int, operation: str ): self.items = start_items self.div_by = divisible_by @@ -27,7 +28,7 @@ class Monkey: Monkey.monkeys.append(self) def do_business(self): for _ in range(len(self.items)): - self.items[0] = self.operation(self.items[0])# // 3 + self.items[0] = (self.operation(self.items[0]) % Monkey.common_divisor) // Monkey.worry_level_decrease if self.items[0] % self.div_by == 0: Monkey.monkeys[self.monkey_on_true].recv_item(self.items.pop(0)) else: @@ -46,19 +47,33 @@ def do_monkey_business(filename): content = file.read() for match in finditer(re_monkey, content): g = match.groups() - print(g) Monkey([int(x) for x in g[1].replace(" ", "").split(",")], int(g[3]), int(g[4]), int(g[5]), g[2]) + monkey_copy = deepcopy(Monkey.monkeys) - for r in range(ROUND_COUNT): - print(f"Round {r}") + for i in range(len(Monkey.monkeys)): + Monkey.common_divisor *= Monkey.monkeys[i].div_by + + # Task 1 + Monkey.worry_level_decrease = 3 + for _ in range(20): for i in range(len(Monkey.monkeys)): Monkey.monkeys[i].do_business() - ranking = [m.get_inspections() for m in Monkey.monkeys] ranking.sort() result = ranking[-2] * ranking[-1] - print(ranking) - print(f"Monkey business level at {result}") + print(f"Task 1: Monkey business level at {result}") + + # Task 2 + Monkey.monkeys = monkey_copy + Monkey.worry_level_decrease = 1 + for _ in range(10000): + for i in range(len(Monkey.monkeys)): + Monkey.monkeys[i].do_business() + ranking = [m.get_inspections() for m in Monkey.monkeys] + ranking.sort() + result = ranking[-2] * ranking[-1] + # print(ranking) + print(f"Task 2: Monkey business level at {result}") if __name__ == '__main__': if not len(argv) == 2: diff --git a/README.md b/README.md index 42848cd..a69f6fc 100644 --- a/README.md +++ b/README.md @@ -26,5 +26,5 @@ Also, check out the repositories of my friends who do stuff in go, Visual Basic, - **Java**: day 10 - **Javascript**: day 6 - **php**: day 9 -- **Python**: day 5 +- **Python**: day 5, 11 - **VIM-Script**: day 3