My Advent of Code solutions in Python. kevinyap.ca/2019/12/going-fast-in-advent-of-code/
advent-of-code python
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

Add 2022/11

+55
+55
2022/day11.py
··· 1 + import os, copy, fileinput 2 + from collections import Counter, deque 3 + from utils import parse_nums, mul 4 + 5 + 6 + def simulate(monkeys, num_turns, part_2=False): 7 + monkeys = copy.deepcopy(monkeys) 8 + mod = mul(m[2] for m in monkeys) 9 + counts = Counter() 10 + 11 + for turn in range(num_turns): 12 + for i, (items, operation, div, true, false) in enumerate(monkeys): 13 + for _ in range(len(items)): 14 + counts[i] += 1 15 + item = monkeys[i][0].popleft() 16 + op, val = operation.split()[-2:] 17 + if val == 'old': 18 + val = item 19 + else: 20 + val = int(val) 21 + 22 + if op == "*": 23 + item *= val 24 + elif op == "+": 25 + item += val 26 + 27 + if part_2: 28 + item %= mod 29 + else: 30 + item //= 3 31 + 32 + next_monkey = true if item % div == 0 else false 33 + monkeys[next_monkey][0].append(item) 34 + 35 + inspections = list(sorted(counts.values(), reverse=True)) 36 + return inspections[0] * inspections[1] 37 + 38 + 39 + 40 + # Parse input. 41 + INPUT = "".join(fileinput.input()) 42 + 43 + MONKEYS = [] 44 + for monkey in INPUT.split(os.linesep * 2): 45 + m = monkey.splitlines() 46 + items = deque(parse_nums(m[1])) 47 + operation = m[2].split(': ')[1] 48 + div = parse_nums(m[3])[0] 49 + true = parse_nums(m[4])[0] 50 + false = parse_nums(m[5])[0] 51 + MONKEYS.append([items, operation, div, true, false]) 52 + 53 + # Solve problem. 54 + print("Part 1:", simulate(MONKEYS, 20)) 55 + print("Part 2:", simulate(MONKEYS, 10000, part_2=True))