day 11 task 2

This commit is contained in:
matthias@arch 2022-12-12 13:49:44 +01:00
parent d0627e2ae0
commit 06a09afccd
3 changed files with 33 additions and 10 deletions

8
11/README.md Normal file
View File

@ -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
```

View File

@ -1,10 +1,11 @@
from sys import argv from sys import argv
from re import finditer from re import finditer
from copy import deepcopy
ROUND_COUNT = 10000
class Monkey: class Monkey:
monkeys = [] 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 ): 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.items = start_items
self.div_by = divisible_by self.div_by = divisible_by
@ -27,7 +28,7 @@ class Monkey:
Monkey.monkeys.append(self) Monkey.monkeys.append(self)
def do_business(self): def do_business(self):
for _ in range(len(self.items)): 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: if self.items[0] % self.div_by == 0:
Monkey.monkeys[self.monkey_on_true].recv_item(self.items.pop(0)) Monkey.monkeys[self.monkey_on_true].recv_item(self.items.pop(0))
else: else:
@ -46,19 +47,33 @@ def do_monkey_business(filename):
content = file.read() content = file.read()
for match in finditer(re_monkey, content): for match in finditer(re_monkey, content):
g = match.groups() 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([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): for i in range(len(Monkey.monkeys)):
print(f"Round {r}") 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)): for i in range(len(Monkey.monkeys)):
Monkey.monkeys[i].do_business() Monkey.monkeys[i].do_business()
ranking = [m.get_inspections() for m in Monkey.monkeys] ranking = [m.get_inspections() for m in Monkey.monkeys]
ranking.sort() ranking.sort()
result = ranking[-2] * ranking[-1] result = ranking[-2] * ranking[-1]
print(ranking) print(f"Task 1: Monkey business level at {result}")
print(f"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 __name__ == '__main__':
if not len(argv) == 2: if not len(argv) == 2:

View File

@ -26,5 +26,5 @@ Also, check out the repositories of my friends who do stuff in go, Visual Basic,
- **Java**: day 10 - **Java**: day 10
- **Javascript**: day 6 - **Javascript**: day 6
- **php**: day 9 - **php**: day 9
- **Python**: day 5 - **Python**: day 5, 11
- **VIM-Script**: day 3 - **VIM-Script**: day 3