From 5ecbe594f77e341bfa5f1e63596170cbb3771761 Mon Sep 17 00:00:00 2001 From: Julia Lange Date: Sat, 10 Dec 2022 21:52:28 -0800 Subject: [PATCH] Day 11 2022 --- Python/2022/11/input | 55 ++++++++++++++++++++++++++++ Python/2022/11/main.py | 81 ++++++++++++++++++++++++++++++++++++++++++ Python/2022/11/sample | 27 ++++++++++++++ 3 files changed, 163 insertions(+) create mode 100644 Python/2022/11/input create mode 100644 Python/2022/11/main.py create mode 100644 Python/2022/11/sample diff --git a/Python/2022/11/input b/Python/2022/11/input new file mode 100644 index 0000000..9945575 --- /dev/null +++ b/Python/2022/11/input @@ -0,0 +1,55 @@ +Monkey 0: + Starting items: 65, 58, 93, 57, 66 + Operation: new = old * 7 + Test: divisible by 19 + If true: throw to monkey 6 + If false: throw to monkey 4 + +Monkey 1: + Starting items: 76, 97, 58, 72, 57, 92, 82 + Operation: new = old + 4 + Test: divisible by 3 + If true: throw to monkey 7 + If false: throw to monkey 5 + +Monkey 2: + Starting items: 90, 89, 96 + Operation: new = old * 5 + Test: divisible by 13 + If true: throw to monkey 5 + If false: throw to monkey 1 + +Monkey 3: + Starting items: 72, 63, 72, 99 + Operation: new = old * old + Test: divisible by 17 + If true: throw to monkey 0 + If false: throw to monkey 4 + +Monkey 4: + Starting items: 65 + Operation: new = old + 1 + Test: divisible by 2 + If true: throw to monkey 6 + If false: throw to monkey 2 + +Monkey 5: + Starting items: 97, 71 + Operation: new = old + 8 + Test: divisible by 11 + If true: throw to monkey 7 + If false: throw to monkey 3 + +Monkey 6: + Starting items: 83, 68, 88, 55, 87, 67 + Operation: new = old + 2 + Test: divisible by 5 + If true: throw to monkey 2 + If false: throw to monkey 1 + +Monkey 7: + Starting items: 64, 81, 50, 96, 82, 53, 62, 92 + Operation: new = old + 5 + Test: divisible by 7 + If true: throw to monkey 3 + If false: throw to monkey 0 diff --git a/Python/2022/11/main.py b/Python/2022/11/main.py new file mode 100644 index 0000000..21749ef --- /dev/null +++ b/Python/2022/11/main.py @@ -0,0 +1,81 @@ +input = open("input", 'r') +# input = open("sample", 'r') +data = [] +total = 0 + +for line in input: + data.append(line.strip()) + +divisor = 1 +monkeys = [] +for line in data: + if len(line) <= 0: + continue + if line[0] == "M": + monkeys.append({}) + else: + if line[0] == "S": + text, items = line.split(": ") + monkeys[-1]["items"] = [int(item) for item in items.split(", ")] + elif line[0] == "O": + formula = line.split("old ")[1] + op = "+" if formula[0] == "+" else "*" + num = formula[2:] + monkeys[-1]["opp"] = (op, num) + elif line[0] == "T": + monkeys[-1]["test"] = int(line.split("divisible by ")[1]) + divisor *= monkeys[-1]["test"] + elif line[3] == "t": + monkeys[-1]["true"] = int(line.split("monkey ")[1]) + elif line[3] == "f": + monkeys[-1]["false"] = int(line.split("monkey ")[1]) + +# inspects = [0 for _ in range(len(monkeys))] +# for r in range(20): +# # round +# for m, monkey in enumerate(monkeys): +# length = len(monkey["items"]) +# inspects[m] += length +# for i in range(length): +# item = monkey["items"][0] +# del monkey["items"][0] +# value = int(monkey["opp"][1]) if monkey["opp"][1] != "old" else item +# if monkey["opp"][0] == "*": +# item = item * value +# elif monkey["opp"][0] == "+": +# item = item + value + +# item = item // 3 + +# if item % monkey["test"] == 0: +# monkeys[monkey["true"]]["items"].append(item) +# else: +# monkeys[monkey["false"]]["items"].append(item) + +# isorted = sorted(inspects, reverse=True) +# print(isorted[0] * isorted[1]) + +inspects = [0 for _ in range(len(monkeys))] +for r in range(10000): + # round + for m, monkey in enumerate(monkeys): + length = len(monkey["items"]) + inspects[m] += length + for i in range(length): + item = monkey["items"][0] + del monkey["items"][0] + value = int(monkey["opp"][1]) if monkey["opp"][1] != "old" else item + if monkey["opp"][0] == "*": + item = item * value + elif monkey["opp"][0] == "+": + item = item + value + + item = item % divisor + + if item % monkey["test"] == 0: + monkeys[monkey["true"]]["items"].append(item) + else: + monkeys[monkey["false"]]["items"].append(item) + +isorted = sorted(inspects, reverse=True) +print(isorted[0] * isorted[1]) diff --git a/Python/2022/11/sample b/Python/2022/11/sample new file mode 100644 index 0000000..30e09e5 --- /dev/null +++ b/Python/2022/11/sample @@ -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