From 8ae80dd6d2e607f9dc5c7d97b9858303ea862da5 Mon Sep 17 00:00:00 2001 From: Lisa Smith Date: Sun, 25 Dec 2022 13:17:36 +0000 Subject: [PATCH] 10th & 11th December --- 2022_10_addx_1.py | 17 +++++ 2022_10_addx_2.py | 26 +++++++ 2022_10_addx_input.txt | 138 ++++++++++++++++++++++++++++++++++++++ 2022_11_monkeys.py | 78 +++++++++++++++++++++ 2022_11_monkeys_input.txt | 55 +++++++++++++++ 5 files changed, 314 insertions(+) create mode 100644 2022_10_addx_1.py create mode 100644 2022_10_addx_2.py create mode 100644 2022_10_addx_input.txt create mode 100644 2022_11_monkeys.py create mode 100644 2022_11_monkeys_input.txt diff --git a/2022_10_addx_1.py b/2022_10_addx_1.py new file mode 100644 index 0000000..c1ed000 --- /dev/null +++ b/2022_10_addx_1.py @@ -0,0 +1,17 @@ +from itertools import islice + + +def get_x(): + x = 1 + with open("2022_10_addx_input.txt") as f: + for line in f: + line = line.strip() + if line == 'noop': + yield x + else: + yield x + yield x + x += int(line.split()[1]) + + +print(sum(cycle * x for cycle, x in islice(enumerate(get_x(), 1), 19, None, 40))) diff --git a/2022_10_addx_2.py b/2022_10_addx_2.py new file mode 100644 index 0000000..84fa9f2 --- /dev/null +++ b/2022_10_addx_2.py @@ -0,0 +1,26 @@ +from itertools import islice + + +def get_sprite_pixels(): + x = 1 + with open("2022_10_addx_input.txt") as f: + for line in f: + line = line.strip() + if line == 'noop': + yield (x-1, x, x+1) + else: + yield (x-1, x, x+1) + yield (x-1, x, x+1) + x += int(line.split()[1]) + + +sprite_pixels_iterator = get_sprite_pixels() + +for _ in range(6): + output_line = '' + for crt_pos, sprite_pixels in zip(range(40), sprite_pixels_iterator): + if crt_pos in sprite_pixels: + output_line += '#' + else: + output_line += '.' + print(output_line) diff --git a/2022_10_addx_input.txt b/2022_10_addx_input.txt new file mode 100644 index 0000000..3e49574 --- /dev/null +++ b/2022_10_addx_input.txt @@ -0,0 +1,138 @@ +addx 1 +noop +addx 4 +noop +noop +addx 7 +noop +noop +noop +addx 3 +noop +noop +addx 5 +addx -1 +addx 1 +addx 5 +addx 3 +noop +addx 3 +noop +addx -1 +noop +addx 3 +addx 5 +addx -38 +addx 7 +addx 10 +addx -14 +addx 5 +addx 30 +addx -25 +noop +addx 2 +addx 3 +addx -2 +addx 2 +addx 5 +addx 2 +addx 2 +addx -21 +addx 22 +addx 5 +addx 2 +addx 3 +noop +addx -39 +addx 1 +noop +noop +addx 3 +addx 5 +addx 4 +addx -5 +addx 4 +addx 4 +noop +addx -9 +addx 12 +addx 5 +addx 2 +addx -1 +addx 6 +addx -2 +noop +addx 3 +addx 3 +addx 2 +addx -37 +addx 39 +addx -33 +addx -1 +addx 1 +addx 8 +noop +noop +noop +addx 2 +addx 20 +addx -19 +addx 4 +noop +noop +noop +addx 3 +addx 2 +addx 5 +noop +addx 1 +addx 4 +addx -21 +addx 22 +addx -38 +noop +noop +addx 7 +addx 32 +addx -27 +noop +addx 3 +addx -2 +addx 2 +addx 5 +addx 2 +addx 2 +addx 3 +addx -2 +addx 2 +noop +addx 3 +addx 5 +addx 2 +addx 3 +noop +addx -39 +addx 2 +noop +addx 4 +addx 8 +addx -8 +addx 6 +addx -1 +noop +addx 5 +noop +noop +noop +addx 3 +addx 5 +addx 2 +addx -11 +addx 12 +addx 2 +noop +addx 3 +addx 2 +addx 5 +addx -6 +noop diff --git a/2022_11_monkeys.py b/2022_11_monkeys.py new file mode 100644 index 0000000..f2fc0b7 --- /dev/null +++ b/2022_11_monkeys.py @@ -0,0 +1,78 @@ +import operator +import heapq +import math + + +class Monkey: + def __init__(self, starting_items, operation, test_value, true_monkey, false_monkey): + self.items = starting_items + self.operation = operation + self.test_value = test_value + self.test = make_test(test_value) + self.true_monkey = true_monkey + self.false_monkey = false_monkey + self.inspection_count = 0 + + def take_turn(self): + for item in self.items: + self.inspection_count += 1 + item = self.operation(item) + item %= mod + if self.test(item): + monkeys[self.true_monkey].items.append(item) + else: + monkeys[self.false_monkey].items.append(item) + self.items.clear() + + +operators = { + '+': operator.add, + '-': operator.sub, + '*': operator.mul, +} + + +def make_operation(left, symbol, right): + operator_function = operators[symbol] + left = None if left == 'old' else int(left) + right = None if right == 'old' else int(right) + + def operation(old): + return operator_function(old if left is None else left, old if right is None else right) + return operation + + +def make_test(test_value): + def test(value): + return not value % test_value + return test + + +monkeys = [] + +lines = (line.strip() for line in open("2022_11_monkeys_input.txt")) + +for line in lines: + if not line: + continue + line = next(lines) + starting_items = [int(item) + for item in line.split("Starting items: ", 1)[1].split(', ')] + line = next(lines) + operation = make_operation(*line.split("Operation: new = ", 1)[1].split()) + line = next(lines) + test_value = int(line.split("Test: divisible by ", 1)[1]) + line = next(lines) + true_monkey = int(line.split("If true: throw to monkey ", 1)[1]) + line = next(lines) + false_monkey = int(line.split("If false: throw to monkey ", 1)[1]) + monkeys.append(Monkey(starting_items, operation, + test_value, true_monkey, false_monkey)) + +mod = math.prod(monkey.test_value for monkey in monkeys) + +for _ in range(10000): + for monkey in monkeys: + monkey.take_turn() + +print(operator.mul(*heapq.nlargest(2, (monkey.inspection_count for monkey in monkeys)))) diff --git a/2022_11_monkeys_input.txt b/2022_11_monkeys_input.txt new file mode 100644 index 0000000..f505c2e --- /dev/null +++ b/2022_11_monkeys_input.txt @@ -0,0 +1,55 @@ +Monkey 0: + Starting items: 83, 97, 95, 67 + Operation: new = old * 19 + Test: divisible by 17 + If true: throw to monkey 2 + If false: throw to monkey 7 + +Monkey 1: + Starting items: 71, 70, 79, 88, 56, 70 + Operation: new = old + 2 + Test: divisible by 19 + If true: throw to monkey 7 + If false: throw to monkey 0 + +Monkey 2: + Starting items: 98, 51, 51, 63, 80, 85, 84, 95 + Operation: new = old + 7 + Test: divisible by 7 + If true: throw to monkey 4 + If false: throw to monkey 3 + +Monkey 3: + Starting items: 77, 90, 82, 80, 79 + Operation: new = old + 1 + Test: divisible by 11 + If true: throw to monkey 6 + If false: throw to monkey 4 + +Monkey 4: + Starting items: 68 + Operation: new = old * 5 + Test: divisible by 13 + If true: throw to monkey 6 + If false: throw to monkey 5 + +Monkey 5: + Starting items: 60, 94 + Operation: new = old + 5 + Test: divisible by 3 + If true: throw to monkey 1 + If false: throw to monkey 0 + +Monkey 6: + Starting items: 81, 51, 85 + Operation: new = old * old + Test: divisible by 5 + If true: throw to monkey 5 + If false: throw to monkey 1 + +Monkey 7: + Starting items: 98, 81, 63, 65, 84, 71, 84 + Operation: new = old + 3 + Test: divisible by 2 + If true: throw to monkey 2 + If false: throw to monkey 3