day 11 python

main
magical 2022-12-10 21:32:55 -08:00
parent 28f7fcb605
commit eefab8eabf
3 changed files with 136 additions and 0 deletions

55
day11/input 100644
View File

@ -0,0 +1,55 @@
Monkey 0:
Starting items: 93, 54, 69, 66, 71
Operation: new = old * 3
Test: divisible by 7
If true: throw to monkey 7
If false: throw to monkey 1
Monkey 1:
Starting items: 89, 51, 80, 66
Operation: new = old * 17
Test: divisible by 19
If true: throw to monkey 5
If false: throw to monkey 7
Monkey 2:
Starting items: 90, 92, 63, 91, 96, 63, 64
Operation: new = old + 1
Test: divisible by 13
If true: throw to monkey 4
If false: throw to monkey 3
Monkey 3:
Starting items: 65, 77
Operation: new = old + 2
Test: divisible by 3
If true: throw to monkey 4
If false: throw to monkey 6
Monkey 4:
Starting items: 76, 68, 94
Operation: new = old * old
Test: divisible by 2
If true: throw to monkey 0
If false: throw to monkey 6
Monkey 5:
Starting items: 86, 65, 66, 97, 73, 83
Operation: new = old + 8
Test: divisible by 11
If true: throw to monkey 2
If false: throw to monkey 3
Monkey 6:
Starting items: 78
Operation: new = old + 6
Test: divisible by 17
If true: throw to monkey 0
If false: throw to monkey 1
Monkey 7:
Starting items: 89, 57, 59, 61, 87, 55, 55, 88
Operation: new = old + 7
Test: divisible by 5
If true: throw to monkey 2
If false: throw to monkey 5

27
day11/sample 100644
View File

@ -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

54
day11/sol.py 100644
View File

@ -0,0 +1,54 @@
from collections import Counter
def parse(input):
if hasattr(input, 'read'):
input = input.read()
monkeys = []
for chunk in input.split("\n\n"):
lines = chunk.strip().split("\n")
assert 'Starting' in lines[1]
items = [int(x.strip()) for x in lines[1].split(":")[1].split(",")]
assert 'Operation' in lines[2]
op = eval("lambda old: " + lines[2].split("=")[1])
assert 'divisible by' in lines[3]
divisor = int(lines[3].split()[-1])
assert 'true: throw' in lines[4]
assert 'false: throw' in lines[5]
target = [
int(lines[5].split()[-1]),
int(lines[4].split()[-1]),
]
monkeys.append((items, op, divisor, target))
return monkeys
def product(it):
t = 1
for x in it:
t *= x
return t
throws = Counter()
def play(monkeys):
N = product(m[2] for m in monkeys)
for i, m in enumerate(monkeys):
items, op, divisor, target = m
for j, x in enumerate(items):
throws[i] += 1
old = x
x = op(x)
#x //= 3
x %= N
trg = target[x % divisor == 0]
#print(f'{old} -> {x}. throwing to {trg}')
monkeys[trg][0].append(x)
items[:] = []
return throws
m = parse(open("input"))
print(m)
for _ in range(10000):
play(m)
for i, m in enumerate(m):
print(i, m[0])
a, b = sorted(throws.values())[-2:]
print(a*b)