AdventOfCode2022/11/day11.py

83 lines
3.0 KiB
Python
Raw Permalink Normal View History

2022-12-11 22:39:37 +01:00
from sys import argv
from re import finditer
2022-12-12 13:49:44 +01:00
from copy import deepcopy
2022-12-11 22:39:37 +01:00
class Monkey:
monkeys = []
2022-12-12 13:49:44 +01:00
common_divisor = 1
worry_level_decrease = 1
2022-12-11 22:39:37 +01:00
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
self.monkey_on_true = monkey_on_true
self.monkey_on_false = monkey_on_false
self.operation = lambda x : x
self.inspections = 0
if operation.startswith('*'):
try:
i = int(operation[2:])
self.operation = lambda x : x * i
except:
self.operation = lambda x : x * x
else:
try:
i = int(operation[2:])
self.operation = lambda x : x + i
except:
self.operation = lambda x : x + x
Monkey.monkeys.append(self)
def do_business(self):
for _ in range(len(self.items)):
2022-12-12 13:49:44 +01:00
self.items[0] = (self.operation(self.items[0]) % Monkey.common_divisor) // Monkey.worry_level_decrease
2022-12-11 22:39:37 +01:00
if self.items[0] % self.div_by == 0:
Monkey.monkeys[self.monkey_on_true].recv_item(self.items.pop(0))
else:
Monkey.monkeys[self.monkey_on_false].recv_item(self.items.pop(0))
self.inspections += 1
def recv_item(self, item: int):
self.items.append(item)
def get_inspections(self):
return self.inspections
re_monkey = r"Monkey (\d+):\n *Starting items: (.*)\n *Operation: new = old (.*)\n *Test: divisible by (\d+)\n *If true: throw to monkey (\d+)\n *If false: throw to monkey (\d+)"
def do_monkey_business(filename):
with open(filename, "r") as file:
content = file.read()
for match in finditer(re_monkey, content):
g = match.groups()
Monkey([int(x) for x in g[1].replace(" ", "").split(",")], int(g[3]), int(g[4]), int(g[5]), g[2])
2022-12-12 13:49:44 +01:00
monkey_copy = deepcopy(Monkey.monkeys)
for i in range(len(Monkey.monkeys)):
Monkey.common_divisor *= Monkey.monkeys[i].div_by
2022-12-11 22:39:37 +01:00
2022-12-12 13:49:44 +01:00
# Task 1
Monkey.worry_level_decrease = 3
for _ in range(20):
2022-12-11 22:39:37 +01:00
for i in range(len(Monkey.monkeys)):
Monkey.monkeys[i].do_business()
2022-12-12 13:49:44 +01:00
ranking = [m.get_inspections() for m in Monkey.monkeys]
ranking.sort()
result = ranking[-2] * ranking[-1]
print(f"Task 1: Monkey business level at {result}")
2022-12-11 22:39:37 +01:00
2022-12-12 13:49:44 +01:00
# 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()
2022-12-11 22:39:37 +01:00
ranking = [m.get_inspections() for m in Monkey.monkeys]
ranking.sort()
result = ranking[-2] * ranking[-1]
2022-12-12 13:49:44 +01:00
# print(ranking)
print(f"Task 2: Monkey business level at {result}")
2022-12-11 22:39:37 +01:00
if __name__ == '__main__':
if not len(argv) == 2:
print(f"Expected exactly one argument (filename), got {len(argv)-1}")
exit(1)
do_monkey_business(argv[1])