From 695b77fef7b61dbed7ac3488c6a1b729541c52d3 Mon Sep 17 00:00:00 2001 From: Tyrel Souza Date: Sun, 11 Dec 2022 00:56:44 -0500 Subject: [PATCH] day11 part2 --- 2022/python/day11.py | 24 +++++++++--------------- 1 file changed, 9 insertions(+), 15 deletions(-) diff --git a/2022/python/day11.py b/2022/python/day11.py index c440418..4e9c064 100644 --- a/2022/python/day11.py +++ b/2022/python/day11.py @@ -1,4 +1,5 @@ import shared +import math from scanf import scanf import matrix from dataclasses import dataclass @@ -18,7 +19,6 @@ class Monkey: inspect_count: int = 0 - def load_monkeys(lines): monkeys = [] count = 0 @@ -52,14 +52,13 @@ def load_monkeys(lines): ) monkeys.append(monkey) count+=1 - return monkeys + lcm = math.lcm(*[m.div for m in monkeys]) + return monkeys, lcm def part1(lines): - monkeys = load_monkeys(lines) - for monkey in monkeys: - print(monkey) + monkeys, _ = load_monkeys(lines) - for current_round in range(20): # TODO: WHILE + for current_round in range(20): for monkey in monkeys: for item in monkey.items: monkey.inspect_count += 1 @@ -68,9 +67,7 @@ def part1(lines): item = monkey.op(item, item) else: item = monkey.op(item, monkey.by) - print(item,monkey.by, monkey.op) item = item // 3 - print(item) if item % monkey.div == 0: to = monkey.t else: @@ -84,16 +81,13 @@ def part1(lines): total = counts[-1] * counts[-2] print(total) - - def part2(lines): - monkeys = load_monkeys(lines) - for monkey in monkeys: - print(monkey) + monkeys, lcm = load_monkeys(lines) - for current_round in range(10000): # TODO: WHILE + for current_round in range(10000): for monkey in monkeys: + print(monkey) for item in monkey.items: monkey.inspect_count += 1 if monkey.by is None: @@ -101,13 +95,13 @@ def part2(lines): item = monkey.op(item, item) else: item = monkey.op(item, monkey.by) + item = item % lcm if item % monkey.div == 0: to = monkey.t else: to = monkey.f monkeys[to].items.append(item) monkey.items = [] - print("-----current round", current_round) counts = list(sorted([monkey.inspect_count for monkey in monkeys])) print(counts)