Compare commits
4 Commits
28f7fcb605
...
aeaf4f524c
Author | SHA1 | Date | |
---|---|---|---|
aeaf4f524c | |||
648ce83517 | |||
4fc545ac39 | |||
eefab8eabf |
55
day11/input
Normal file
55
day11/input
Normal 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
Normal file
27
day11/sample
Normal 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
|
70
day11/sol.py
Normal file
70
day11/sol.py
Normal file
@ -0,0 +1,70 @@
|
|||||||
|
from collections import namedtuple
|
||||||
|
from copy import deepcopy
|
||||||
|
import math
|
||||||
|
|
||||||
|
Monkey = namedtuple('Monkey', 'index, op, divisor, target')
|
||||||
|
def parse(input):
|
||||||
|
monkeys = []
|
||||||
|
items = {}
|
||||||
|
for i, chunk in enumerate(input.split("\n\n")):
|
||||||
|
lines = chunk.strip().split("\n")
|
||||||
|
assert 'Starting items:' in lines[1]
|
||||||
|
assert 'Operation: new =' in lines[2]
|
||||||
|
assert 'Test: divisible by' in lines[3]
|
||||||
|
assert 'If true: throw to' in lines[4]
|
||||||
|
assert 'If false: throw to' in lines[5]
|
||||||
|
|
||||||
|
items[i] = [int(x.strip()) for x in lines[1].split(":")[1].split(",")]
|
||||||
|
op = eval("lambda old: " + lines[2].split("=")[1])
|
||||||
|
divisor = int(lines[3].split()[-1])
|
||||||
|
target = [
|
||||||
|
int(lines[5].split()[-1]),
|
||||||
|
int(lines[4].split()[-1]),
|
||||||
|
]
|
||||||
|
monkeys.append(Monkey(i, op, divisor, target))
|
||||||
|
return monkeys, items
|
||||||
|
|
||||||
|
def play(monkeys, items, rounds=1, N=None):
|
||||||
|
throws = {m.index: 0 for m in monkeys}
|
||||||
|
for _ in range(rounds):
|
||||||
|
for m in monkeys:
|
||||||
|
for x in items[m.index]:
|
||||||
|
if N:
|
||||||
|
nx = m.op(x) % N
|
||||||
|
else:
|
||||||
|
nx = m.op(x) // 3
|
||||||
|
trg = m.target[nx % m.divisor == 0]
|
||||||
|
#print(f'{i}: {x} -> {nx}. throwing to {trg}')
|
||||||
|
items[trg].append(nx)
|
||||||
|
throws[m.index] += len(items[m.index])
|
||||||
|
items[m.index] = []
|
||||||
|
show(items)
|
||||||
|
return throws
|
||||||
|
|
||||||
|
def show(items):
|
||||||
|
for k in items:
|
||||||
|
print(k, items[k])
|
||||||
|
|
||||||
|
def monkeybusiness(throws):
|
||||||
|
a, b = sorted(throws.values())[-2:]
|
||||||
|
return a*b
|
||||||
|
|
||||||
|
def lcm(ints):
|
||||||
|
n = 1
|
||||||
|
for x in ints:
|
||||||
|
n *= x // math.gcd(n, x)
|
||||||
|
return n
|
||||||
|
|
||||||
|
def main():
|
||||||
|
monkeys, items = parse(open("input").read())
|
||||||
|
#print(monkeys)
|
||||||
|
show(items)
|
||||||
|
|
||||||
|
throws = play(monkeys, deepcopy(items), rounds=20)
|
||||||
|
print(monkeybusiness(throws))
|
||||||
|
|
||||||
|
N = lcm(m.divisor for m in monkeys)
|
||||||
|
throws = play(monkeys, deepcopy(items), rounds=10000, N=N)
|
||||||
|
print(monkeybusiness(throws))
|
||||||
|
|
||||||
|
main()
|
Loading…
x
Reference in New Issue
Block a user