diff options
author | Marvin Borner | 2022-12-11 07:01:51 +0100 |
---|---|---|
committer | Marvin Borner | 2022-12-11 07:01:51 +0100 |
commit | 470a942e23c863b86b76e36fc43dce48c25b07c1 (patch) | |
tree | 9d990247a93a5c78f9f36e32ad62eb81ed24da17 | |
parent | bbaa58db1a1de602ac919264eee3d80592178585 (diff) |
hehe
-rw-r--r-- | 2022/11/input | 55 | ||||
-rw-r--r-- | 2022/11/solve.py | 43 |
2 files changed, 98 insertions, 0 deletions
diff --git a/2022/11/input b/2022/11/input new file mode 100644 index 0000000..4013e06 --- /dev/null +++ b/2022/11/input @@ -0,0 +1,55 @@ +Monkey 0: + Starting items: 52, 78, 79, 63, 51, 94 + Operation: new = old * 13 + Test: divisible by 5 + If true: throw to monkey 1 + If false: throw to monkey 6 + +Monkey 1: + Starting items: 77, 94, 70, 83, 53 + Operation: new = old + 3 + Test: divisible by 7 + If true: throw to monkey 5 + If false: throw to monkey 3 + +Monkey 2: + Starting items: 98, 50, 76 + Operation: new = old * old + Test: divisible by 13 + If true: throw to monkey 0 + If false: throw to monkey 6 + +Monkey 3: + Starting items: 92, 91, 61, 75, 99, 63, 84, 69 + Operation: new = old + 5 + Test: divisible by 11 + If true: throw to monkey 5 + If false: throw to monkey 7 + +Monkey 4: + Starting items: 51, 53, 83, 52 + Operation: new = old + 7 + Test: divisible by 3 + If true: throw to monkey 2 + If false: throw to monkey 0 + +Monkey 5: + Starting items: 76, 76 + Operation: new = old + 4 + Test: divisible by 2 + If true: throw to monkey 4 + If false: throw to monkey 7 + +Monkey 6: + Starting items: 75, 59, 93, 69, 76, 96, 65 + Operation: new = old * 19 + Test: divisible by 17 + If true: throw to monkey 1 + If false: throw to monkey 3 + +Monkey 7: + Starting items: 89 + Operation: new = old + 2 + Test: divisible by 19 + If true: throw to monkey 2 + If false: throw to monkey 4 diff --git a/2022/11/solve.py b/2022/11/solve.py new file mode 100644 index 0000000..21b6d4b --- /dev/null +++ b/2022/11/solve.py @@ -0,0 +1,43 @@ +data = [[line.strip() for line in dat.split("\n")] for dat in open("input").read().split("\n\n") if dat != ''] + +def solve(part): + res = 0 + + monkeys = [{"items": [], "op": None, "divisibility": 1, "true": 0, "false": 0, "inspected": 0} for i in range(len(data))] + + # setup monkeys + for i,block in enumerate(data): + monkeys[i]["items"] = [int(item) for item in block[1].split(": ")[1].split(", ")] + monkeys[i]["op"] = block[2].split(": ")[1].replace("new = ", "") + monkeys[i]["divisibility"] = int(block[3].split(" ")[-1]) + monkeys[i]["true"] = int(block[4].split(" ")[-1]) + monkeys[i]["false"] = int(block[5].split(" ")[-1]) + + if part == 2: + # evil muhahahaa + common = eval("*".join(str(monkey["divisibility"]) for monkey in monkeys)) + + for r in range(20 if part == 1 else 10000): + current = 0 + while True: # new round + if current >= len(monkeys): + break + + if len(monkeys[current]["items"]) == 0: + current += 1 + continue + + monkeys[current]["inspected"] += 1 + old = monkeys[current]["items"].pop(0) + lvl = eval(monkeys[current]["op"]) + lvl = int(lvl / 3) if part == 1 else lvl % common + if lvl % monkeys[current]["divisibility"] == 0: + monkeys[monkeys[current]["true"]]["items"].append(lvl) + else: + monkeys[monkeys[current]["false"]]["items"].append(lvl) + + bizz = sorted([monkey["inspected"] for monkey in monkeys]) + return bizz[-1] * bizz[-2] + +print(f"Part 1: {solve(1)}") +print(f"Part 2: {solve(2)}") |