From 8829205e086b721c3046c684994eaa50a6aba883 Mon Sep 17 00:00:00 2001 From: kleph Date: Thu, 26 Jan 2023 00:59:43 +0100 Subject: [PATCH] 2022 - Day 11 part 1 --- 2022/10/10_2.py | 2 - 2022/11/11.py | 113 ++++++++++++++++++++++++++++++++++++++ 2022/11/input.txt | 55 +++++++++++++++++++ 2022/11/input_example.txt | 27 +++++++++ 4 files changed, 195 insertions(+), 2 deletions(-) create mode 100644 2022/11/11.py create mode 100644 2022/11/input.txt create mode 100644 2022/11/input_example.txt diff --git a/2022/10/10_2.py b/2022/10/10_2.py index 6dcc5a1..36b4624 100644 --- a/2022/10/10_2.py +++ b/2022/10/10_2.py @@ -32,5 +32,3 @@ with open(file, encoding="utf-8") as f: regX += value check_cycle(cycle, regX) cycle += 1 - - diff --git a/2022/11/11.py b/2022/11/11.py new file mode 100644 index 0000000..2574824 --- /dev/null +++ b/2022/11/11.py @@ -0,0 +1,113 @@ +#!/usr/bin/env python +# 2022 - Advent Of Code 11 part 1 + +# Monkey 0: +# Starting items: 79, 98 +# Operation: new = old * 19 +# Test: divisible by 23 +# If true: throw to monkey 2 +# If false: throw to monkey 3 + +class Monkey: + def __init__(self, sitems, op, op2, divisible, ta, tf): + self.items = sitems + 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'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, w): + if self.operation == '+': + return w + self.operand + if self.operation == '*': + if self.operand == 'old': + return w * w + return w * self.operand + + def turn(self, other_monkeys): + for item in self.items: + self.activity += 1 + w = item + w = self.operate(w) + w = int(w / 3) # round + if w % self.divisible == 0: + # print(f'throwing worry level {w} to monkey {self.action_true}') + other_monkeys[self.action_true].throw_item(w) + else: + # print(f'throwing worry level {w} to monkey {self.action_false}') + other_monkeys[self.action_false].throw_item(w) + self.items = [] + + def throw_item(self, item): + self.items.append(item) + + +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 + + +# inputfile = 'input_example.txt' +inputfile = 'input.txt' + +monkeys = [] +monkeys = parse_file(inputfile) + +# 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]) diff --git a/2022/11/input.txt b/2022/11/input.txt new file mode 100644 index 0000000..54ae24d --- /dev/null +++ b/2022/11/input.txt @@ -0,0 +1,55 @@ +Monkey 0: + Starting items: 97, 81, 57, 57, 91, 61 + Operation: new = old * 7 + Test: divisible by 11 + If true: throw to monkey 5 + If false: throw to monkey 6 + +Monkey 1: + Starting items: 88, 62, 68, 90 + Operation: new = old * 17 + Test: divisible by 19 + If true: throw to monkey 4 + If false: throw to monkey 2 + +Monkey 2: + Starting items: 74, 87 + Operation: new = old + 2 + Test: divisible by 5 + If true: throw to monkey 7 + If false: throw to monkey 4 + +Monkey 3: + Starting items: 53, 81, 60, 87, 90, 99, 75 + Operation: new = old + 1 + Test: divisible by 2 + If true: throw to monkey 2 + If false: throw to monkey 1 + +Monkey 4: + Starting items: 57 + Operation: new = old + 6 + Test: divisible by 13 + If true: throw to monkey 7 + If false: throw to monkey 0 + +Monkey 5: + Starting items: 54, 84, 91, 55, 59, 72, 75, 70 + Operation: new = old * old + Test: divisible by 7 + If true: throw to monkey 6 + If false: throw to monkey 3 + +Monkey 6: + Starting items: 95, 79, 79, 68, 78 + Operation: new = old + 3 + Test: divisible by 3 + If true: throw to monkey 1 + If false: throw to monkey 3 + +Monkey 7: + Starting items: 61, 97, 67 + Operation: new = old + 4 + Test: divisible by 17 + If true: throw to monkey 0 + If false: throw to monkey 5 diff --git a/2022/11/input_example.txt b/2022/11/input_example.txt new file mode 100644 index 0000000..30e09e5 --- /dev/null +++ b/2022/11/input_example.txt @@ -0,0 +1,27 @@ +Monkey 0: + Starting items: 79, 98 + Operation: new = old * 19 + Test: divisible by 23 + If true: throw to monkey 2 + If false: throw to monkey 3 + +Monkey 1: + Starting items: 54, 65, 75, 74 + Operation: new = old + 6 + Test: divisible by 19 + If true: throw to monkey 2 + If false: throw to monkey 0 + +Monkey 2: + Starting items: 79, 60, 97 + Operation: new = old * old + Test: divisible by 13 + If true: throw to monkey 1 + If false: throw to monkey 3 + +Monkey 3: + Starting items: 74 + Operation: new = old + 3 + Test: divisible by 17 + If true: throw to monkey 0 + If false: throw to monkey 1