From f0b631085823b710825d65de0357efe51a680d47 Mon Sep 17 00:00:00 2001 From: kleph Date: Mon, 2 Dec 2024 19:31:58 +0100 Subject: [PATCH] 2022 - Day 11 part 2 - unfinished --- 2022/11/11_2.py | 127 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 127 insertions(+) create mode 100644 2022/11/11_2.py diff --git a/2022/11/11_2.py b/2022/11/11_2.py new file mode 100644 index 0000000..ff1e947 --- /dev/null +++ b/2022/11/11_2.py @@ -0,0 +1,127 @@ +#!/usr/bin/env python +# 2022 - Advent Of Code 11 part 2 +# since it's not finished at all, disable pylint +# pylint: skip-file + +class Monkey: + def __init__(self, sitems, op, op2, divisible, ta, tf): + self.sitems = sitems + self.items = [] + self.operation = op + self.operand = op2 + self.divisible = divisible + self.action_true = ta + self.action_false = tf + self.activity = 0 + + def print(self): + print(f'start items: {self.sitems}') + print(f'factored items: {self.items}') + print(f'operation: {self.operation} {self.operand}') + print(f'divisible: {self.divisible}') + print(f'true: {self.action_true}') + print(f'false: {self.action_false}') + # print(f'{}') + print() + + def operate(self, item): + if self.operation == '+': + pass + if self.operation == '*': + if self.operand != 'old': + item.update([self.operand]) # |= + + def turn(self, other_monkeys): + for item in self.items: + self.activity += 1 + self.operate(item) + if self.divisible in item: + print(f'throwing worry level {item} to monkey {self.action_true}') + other_monkeys[self.action_true].throw_item(item) + else: + print(f'throwing worry level {item} to monkey {self.action_false}') + other_monkeys[self.action_false].throw_item(item) + self.items = [] + + def throw_item(self, item): + self.items.append(item) + + +def prime_factors(n): + i = 2 + factors = [] + while i * i <= n: + if n % i: + i += 1 + else: + n //= i + factors.append(i) + if n > 1: + factors.append(n) + return factors + + +def parse_file(file): + m_list = [] + with open(file, encoding="utf-8") as f: + line = f.readline() # monkey + while line: + line = f.readline().strip() + item_list = line.split('Starting items:')[1] + items = [int(i) for i in item_list.split(',')] + # print(items) + line = f.readline().strip() + oper = line.split('Operation: new = old ')[1] + operation = oper[0] + # print(oper) + if oper[1:] == ' old': + operand = 'old' + else: + operand = int(oper[1:]) + + line = f.readline().strip() + nb_div = int(line.split('Test: divisible by ')[1]) + line = f.readline().strip() + v_true = int(line.split('If true: throw to monkey ')[1]) + line = f.readline().strip() + v_false = int(line.split('If false: throw to monkey ')[1]) + line = f.readline().strip() # empty line + + m_list.append(Monkey(items, operation, operand, nb_div, v_true, v_false)) + line = f.readline() # monkey + + return m_list + + +input_file = 'input_example.txt' +# input = 'input.txt' + +monkeys = parse_file(input_file) + + +for n, m in enumerate(monkeys): + print(f'monkey {n}') + for s in m.sitems: + m.items.append(set(prime_factors(s))) + m.print() + +# for n, m in enumerate(monkeys): +# print(f'monkey {n}') +# m.print() +nb_round = 20 +for r in range(0, nb_round): + print(f'round {r+1}') + for n, m in enumerate(monkeys): + # print(f'monkey {n}') + m.turn(monkeys) + for n, m in enumerate(monkeys): + print(f'monkey {n} items: {m.items}') + +# find 2 max +active_list = [m.activity for m in monkeys] +active_dict = {v: k for k, v in enumerate(active_list)} +# print(active_dict) +print(sorted(active_dict)[-2:]) +two_max = sorted(active_dict)[-2:] + +print(two_max[0] * two_max[1])