advent/2017/day06.py
2017-12-13 16:48:23 -08:00

39 lines
691 B
Python

with open('data06.txt') as f:
initial_banks = list(map(int, f.read().split()))
class Memory:
def __init__(self, banks):
self.banks = banks
def reallocate(self):
index = self.banks.index(max(self.banks))
blocks = self.banks[index]
self.banks[index] = 0
for b in range(blocks):
target = (index + b + 1) % len(self.banks)
self.banks[target] += 1
mem = Memory(initial_banks)
seen = {tuple(mem.banks)}
count = 0
while True:
mem.reallocate()
count += 1
snapshot = tuple(mem.banks)
if snapshot in seen:
break
else:
seen.add(snapshot)
count = 0
final = tuple(mem.banks)
while True:
mem.reallocate()
count += 1
if tuple(mem.banks) == final:
break
print(count)