day11
This commit is contained in:
parent
d4f44c3e80
commit
d0627e2ae0
67
11/day11.py
Normal file
67
11/day11.py
Normal file
@ -0,0 +1,67 @@
|
||||
from sys import argv
|
||||
from re import finditer
|
||||
|
||||
ROUND_COUNT = 10000
|
||||
|
||||
class Monkey:
|
||||
monkeys = []
|
||||
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)):
|
||||
self.items[0] = self.operation(self.items[0])# // 3
|
||||
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()
|
||||
print(g)
|
||||
Monkey([int(x) for x in g[1].replace(" ", "").split(",")], int(g[3]), int(g[4]), int(g[5]), g[2])
|
||||
|
||||
for r in range(ROUND_COUNT):
|
||||
print(f"Round {r}")
|
||||
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}")
|
||||
|
||||
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])
|
55
11/monkey-stuff.txt
Normal file
55
11/monkey-stuff.txt
Normal file
@ -0,0 +1,55 @@
|
||||
Monkey 0:
|
||||
Starting items: 62, 92, 50, 63, 62, 93, 73, 50
|
||||
Operation: new = old * 7
|
||||
Test: divisible by 2
|
||||
If true: throw to monkey 7
|
||||
If false: throw to monkey 1
|
||||
|
||||
Monkey 1:
|
||||
Starting items: 51, 97, 74, 84, 99
|
||||
Operation: new = old + 3
|
||||
Test: divisible by 7
|
||||
If true: throw to monkey 2
|
||||
If false: throw to monkey 4
|
||||
|
||||
Monkey 2:
|
||||
Starting items: 98, 86, 62, 76, 51, 81, 95
|
||||
Operation: new = old + 4
|
||||
Test: divisible by 13
|
||||
If true: throw to monkey 5
|
||||
If false: throw to monkey 4
|
||||
|
||||
Monkey 3:
|
||||
Starting items: 53, 95, 50, 85, 83, 72
|
||||
Operation: new = old + 5
|
||||
Test: divisible by 19
|
||||
If true: throw to monkey 6
|
||||
If false: throw to monkey 0
|
||||
|
||||
Monkey 4:
|
||||
Starting items: 59, 60, 63, 71
|
||||
Operation: new = old * 5
|
||||
Test: divisible by 11
|
||||
If true: throw to monkey 5
|
||||
If false: throw to monkey 3
|
||||
|
||||
Monkey 5:
|
||||
Starting items: 92, 65
|
||||
Operation: new = old * old
|
||||
Test: divisible by 5
|
||||
If true: throw to monkey 6
|
||||
If false: throw to monkey 3
|
||||
|
||||
Monkey 6:
|
||||
Starting items: 78
|
||||
Operation: new = old + 8
|
||||
Test: divisible by 3
|
||||
If true: throw to monkey 0
|
||||
If false: throw to monkey 7
|
||||
|
||||
Monkey 7:
|
||||
Starting items: 84, 93, 54
|
||||
Operation: new = old + 1
|
||||
Test: divisible by 17
|
||||
If true: throw to monkey 2
|
||||
If false: throw to monkey 1
|
Loading…
Reference in New Issue
Block a user