From bb8be4b0a61d04048001d1e7905085accb532419 Mon Sep 17 00:00:00 2001 From: Luna Lailatova Date: Tue, 13 Dec 2022 00:12:43 +0100 Subject: [PATCH] day 11, hmm i think i understand the math... maybe --- day11-1.py | 79 ++++++++++++++++++++++++ day11-2.py | 85 ++++++++++++++++++++++++++ in.txt | 173 ++++++++-------------------------------------------- input11.txt | 55 +++++++++++++++++ 4 files changed, 246 insertions(+), 146 deletions(-) create mode 100644 day11-1.py create mode 100644 day11-2.py create mode 100644 input11.txt diff --git a/day11-1.py b/day11-1.py new file mode 100644 index 0000000..87492d2 --- /dev/null +++ b/day11-1.py @@ -0,0 +1,79 @@ +import math + +class Monkey: + def __init__(self, items, inspect, divider, _true, _false): + self.inspected = 0 + self.items = [] + for x in items: + self.items.append(int(x)) + self.divider = int(divider) + self._true = int(_true) + self._false = int(_false) + if '+' in inspect: + self.inspect_kind = 1 + tempa, tempb = inspect.split('+ ') + self.inspect_amount = int(tempb) + elif inspect.endswith('old'): + self.inspect_kind = 3 + else: + self.inspect_kind = 2 + tempa, tempb = inspect.split('* ') + self.inspect_amount = int(tempb) + + def inspect(self): + if self.items == []: + return + for x in range(len(self.items)): + self.inspected += 1 + if self.inspect_kind == 1: + self.items[x] += self.inspect_amount + if self.inspect_kind == 2: + self.items[x] = self.items[x] * self.inspect_amount + if self.inspect_kind == 3: + self.items[x] = self.items[x] * self.items[x] + self.items[x] = int(math.floor(self.items[x]/3)) + if self.items[x] % self.divider == 0: + monkeys[self._true].addItem(self.items[x]) + else: + monkeys[self._false].addItem(self.items[x]) + self.items = [] + + def addItem(self, item): + self.items.append(item) + + def retInspect(self): + return self.inspected + + +with open('input11.txt','r') as f: + inp = f.read().splitlines(keepends=False) + +monkeys = [] +tempMain = [] +monkeyBuisness = [] + +for i in range(len(inp)): + tempMain.append(inp[i]) + if inp[i] == '' or i == (len(inp) - 1): + print(tempMain[1]) + tempa, tempb = tempMain[1].split(': ') + items = tempb.split(', ') + tempa, divider = tempMain[3].split('by ') + tempa, inspect = tempMain[2].split('= ') + tempa, _true = tempMain[4].split('monkey ') + tempa, _false = tempMain[5].split('monkey ') + newMonkey = Monkey(items, inspect, divider, _true, _false) + monkeys.append(newMonkey) + tempMain = [] + print('end') + +for _ in range(20): + for i in monkeys: + i.inspect() + +for i in monkeys: + monkeyBuisness.append(i.retInspect()) + +monkeyBuisness.sort() + +print(monkeyBuisness[-1]*monkeyBuisness[-2]) \ No newline at end of file diff --git a/day11-2.py b/day11-2.py new file mode 100644 index 0000000..33dc762 --- /dev/null +++ b/day11-2.py @@ -0,0 +1,85 @@ +import math + +class Monkey: + def __init__(self, items, inspect, divider, _true, _false): + self.inspected = 0 + self.items = [] + for x in items: + self.items.append(int(x)) + self.divider = int(divider) + self._true = int(_true) + self._false = int(_false) + if '+' in inspect: + self.inspect_kind = 1 + tempa, tempb = inspect.split('+ ') + self.inspect_amount = int(tempb) + elif inspect.endswith('old'): + self.inspect_kind = 3 + else: + self.inspect_kind = 2 + tempa, tempb = inspect.split('* ') + self.inspect_amount = int(tempb) + + def inspect(self): + if self.items == []: + return + for x in range(len(self.items)): + self.inspected += 1 + if self.inspect_kind == 1: + self.items[x] += self.inspect_amount + if self.inspect_kind == 2: + self.items[x] = self.items[x] * self.inspect_amount + if self.inspect_kind == 3: + self.items[x] = self.items[x] * self.items[x] + self.items[x] %= modulo + if self.items[x] % self.divider == 0: + monkeys[self._true].addItem(self.items[x]) + else: + monkeys[self._false].addItem(self.items[x]) + self.items = [] + + def addItem(self, item): + self.items.append(item) + + def retInspect(self): + return self.inspected + + + +with open('input11.txt','r') as f: + inp = f.read().splitlines(keepends=False) + +monkeys = [] +tempMain = [] +monkeyBuisness = [] +dividers = [] +modulo = 1 + +for i in range(len(inp)): + tempMain.append(inp[i]) + if inp[i] == '' or i == (len(inp) - 1): + tempa, tempb = tempMain[1].split(': ') + items = tempb.split(', ') + tempa, divider = tempMain[3].split('by ') + dividers.append(int(divider)) + tempa, inspect = tempMain[2].split('= ') + tempa, _true = tempMain[4].split('monkey ') + tempa, _false = tempMain[5].split('monkey ') + newMonkey = Monkey(items, inspect, divider, _true, _false) + monkeys.append(newMonkey) + tempMain = [] + + +for i in dividers: + modulo = modulo * i + +for _ in range(10000): + for i in monkeys: + i.inspect() + +for i in monkeys: + monkeyBuisness.append(i.retInspect()) + +monkeyBuisness.sort() + +print(monkeyBuisness[-1]*monkeyBuisness[-2]) \ No newline at end of file diff --git a/in.txt b/in.txt index 37ee8ee..30e09e5 100644 --- a/in.txt +++ b/in.txt @@ -1,146 +1,27 @@ -addx 15 -addx -11 -addx 6 -addx -3 -addx 5 -addx -1 -addx -8 -addx 13 -addx 4 -noop -addx -1 -addx 5 -addx -1 -addx 5 -addx -1 -addx 5 -addx -1 -addx 5 -addx -1 -addx -35 -addx 1 -addx 24 -addx -19 -addx 1 -addx 16 -addx -11 -noop -noop -addx 21 -addx -15 -noop -noop -addx -3 -addx 9 -addx 1 -addx -3 -addx 8 -addx 1 -addx 5 -noop -noop -noop -noop -noop -addx -36 -noop -addx 1 -addx 7 -noop -noop -noop -addx 2 -addx 6 -noop -noop -noop -noop -noop -addx 1 -noop -noop -addx 7 -addx 1 -noop -addx -13 -addx 13 -addx 7 -noop -addx 1 -addx -33 -noop -noop -noop -addx 2 -noop -noop -noop -addx 8 -noop -addx -1 -addx 2 -addx 1 -noop -addx 17 -addx -9 -addx 1 -addx 1 -addx -3 -addx 11 -noop -noop -addx 1 -noop -addx 1 -noop -noop -addx -13 -addx -19 -addx 1 -addx 3 -addx 26 -addx -30 -addx 12 -addx -1 -addx 3 -addx 1 -noop -noop -noop -addx -9 -addx 18 -addx 1 -addx 2 -noop -noop -addx 9 -noop -noop -noop -addx -1 -addx 2 -addx -37 -addx 1 -addx 3 -noop -addx 15 -addx -21 -addx 22 -addx -6 -addx 1 -noop -addx 2 -addx 1 -noop -addx -10 -noop -noop -addx 20 -addx 1 -addx 2 -addx 2 -addx -6 -addx -11 -noop -noop -noop +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 diff --git a/input11.txt b/input11.txt new file mode 100644 index 0000000..66663b4 --- /dev/null +++ b/input11.txt @@ -0,0 +1,55 @@ +Monkey 0: + Starting items: 59, 65, 86, 56, 74, 57, 56 + Operation: new = old * 17 + Test: divisible by 3 + If true: throw to monkey 3 + If false: throw to monkey 6 + +Monkey 1: + Starting items: 63, 83, 50, 63, 56 + Operation: new = old + 2 + Test: divisible by 13 + If true: throw to monkey 3 + If false: throw to monkey 0 + +Monkey 2: + Starting items: 93, 79, 74, 55 + Operation: new = old + 1 + Test: divisible by 2 + If true: throw to monkey 0 + If false: throw to monkey 1 + +Monkey 3: + Starting items: 86, 61, 67, 88, 94, 69, 56, 91 + Operation: new = old + 7 + Test: divisible by 11 + If true: throw to monkey 6 + If false: throw to monkey 7 + +Monkey 4: + Starting items: 76, 50, 51 + Operation: new = old * old + Test: divisible by 19 + If true: throw to monkey 2 + If false: throw to monkey 5 + +Monkey 5: + Starting items: 77, 76 + Operation: new = old + 8 + Test: divisible by 17 + If true: throw to monkey 2 + If false: throw to monkey 1 + +Monkey 6: + Starting items: 74 + Operation: new = old * 2 + Test: divisible by 5 + If true: throw to monkey 4 + If false: throw to monkey 7 + +Monkey 7: + Starting items: 86, 85, 52, 86, 91, 95 + Operation: new = old + 6 + Test: divisible by 7 + If true: throw to monkey 4 + If false: throw to monkey 5