Last active
December 20, 2022 09:26
-
-
Save Shogun89/10934fc2e9d952de6e8cff2f38b6bfb0 to your computer and use it in GitHub Desktop.
Advent of Code - Day 11 part 1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Monkey 0: | |
Starting items: 78, 53, 89, 51, 52, 59, 58, 85 | |
Operation: new = old * 3 | |
Test: divisible by 5 | |
If true: throw to monkey 2 | |
If false: throw to monkey 7 | |
Monkey 1: | |
Starting items: 64 | |
Operation: new = old + 7 | |
Test: divisible by 2 | |
If true: throw to monkey 3 | |
If false: throw to monkey 6 | |
Monkey 2: | |
Starting items: 71, 93, 65, 82 | |
Operation: new = old + 5 | |
Test: divisible by 13 | |
If true: throw to monkey 5 | |
If false: throw to monkey 4 | |
Monkey 3: | |
Starting items: 67, 73, 95, 75, 56, 74 | |
Operation: new = old + 8 | |
Test: divisible by 19 | |
If true: throw to monkey 6 | |
If false: throw to monkey 0 | |
Monkey 4: | |
Starting items: 85, 91, 90 | |
Operation: new = old + 4 | |
Test: divisible by 11 | |
If true: throw to monkey 3 | |
If false: throw to monkey 1 | |
Monkey 5: | |
Starting items: 67, 96, 69, 55, 70, 83, 62 | |
Operation: new = old * 2 | |
Test: divisible by 3 | |
If true: throw to monkey 4 | |
If false: throw to monkey 1 | |
Monkey 6: | |
Starting items: 53, 86, 98, 70, 64 | |
Operation: new = old + 6 | |
Test: divisible by 7 | |
If true: throw to monkey 7 | |
If false: throw to monkey 0 | |
Monkey 7: | |
Starting items: 88, 64 | |
Operation: new = old * old | |
Test: divisible by 17 | |
If true: throw to monkey 2 | |
If false: throw to monkey 5 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from dataclasses import dataclass | |
import re | |
from math import prod | |
@dataclass | |
class Monkey: | |
""" A class for keeping track of an individual monkey """ | |
number : int | |
items : list | |
operation : str | |
operand : int | |
test_condition : int | |
true_monkey : int | |
false_monkey : int | |
inspection_count : int = 0 | |
def inspect_items(self): | |
""" A function that inspects each item """ | |
for i, item in enumerate(self.items): | |
self.inspection_count += 1 | |
if self.operation == '*' and self.operand != -1: | |
self.items[i] = self.items[i] * self.operand | |
if self.operation == '*' and self.operand == -1: | |
self.items[i] = self.items[i] ** 2 | |
if self.operation == '+': | |
self.items[i] += self.operand | |
def reduce_worries(self): | |
""" A function that reduces the worry level of each item """ | |
for i, item in enumerate(self.items): | |
self.items[i] = item//3 | |
def throw_items(self): | |
""" A function that returns a list of tuples with who to throw the item to """ | |
planned_throws = [] | |
for item in self.items: | |
if item % self.test_condition == 0: | |
planned_throws.append((item, self.true_monkey)) | |
else: | |
planned_throws.append((item, self.false_monkey)) | |
self.items = [] | |
return planned_throws | |
def receive_item(self, item): | |
""" A function to add new items """ | |
self.items.append(item) | |
def print_items(self): | |
""" A function to print the current monkey state """ | |
print(f"Monkey {self.number} : " + ', '.join([str(item) for item in self.items])) | |
def get_game_info(file) -> list: | |
""" Gets the game information """ | |
with open(file) as f: | |
lines = f.readlines() | |
# Remove empty rows | |
game = list(filter(lambda x : len(x) > 1, lines)) | |
game = [game[i:i + 6] for i in range(0, len(game), 6)] | |
return game | |
def extract_monkey(info) -> Monkey: | |
""" Generates a monkey from the information """ | |
number = info[0][7] | |
items = list(map(int, info[1].split(':')[1].replace('\n','').split(','))) | |
operation = info[2].split('=')[1] | |
if '*' in operation: | |
operation = '*' | |
if '+' in operation: | |
operation = '+' | |
operand = re.sub("[^0-9]", "",info[2].split('=')[1]) | |
if operand == '': | |
operand = -1 | |
operand = int(operand) | |
test = int(info[3].split('by')[1].replace('\n','')) | |
test_true = int(info[4].split('monkey')[1].replace('\n','')) | |
test_false = int(info[5].split('monkey')[1].replace('\n','')) | |
monkey = Monkey(number, items, operation, operand, test, test_true, test_false) | |
return monkey | |
file = "d11_input.txt" | |
number_rounds = 20 | |
current_round = 0 | |
game_setup = get_game_info(file) | |
monkeys = [extract_monkey(info) for info in game_setup] | |
while current_round < number_rounds: | |
current_round += 1 | |
for monkey in monkeys: | |
monkey.inspect_items() | |
monkey.reduce_worries() | |
throws = monkey.throw_items() | |
for item, target_monkey in throws: | |
monkeys[target_monkey].receive_item(item) | |
print(f"After round {current_round}:") | |
for monkey in monkeys: | |
monkey.print_items() | |
inspections = [monkey.inspection_count for monkey in monkeys] | |
print(prod(sorted(inspections, reverse=True)[:2])) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment