day 12
subterranean sustainability
approach
the row of pots is a set of the indices that hold a plant, so it can grow in both directions, and the rules are a dict from a five-character pattern to its result. next_gen checks every index from two below the leftmost plant to two above the rightmost, builds the pattern around it and applies the matching rule.
part 1 runs 20 generations and sums the indices. fifty billion generations can't be simulated, so part 2 runs 1000, takes the change in the sum over the last generation, and assumes the pattern has settled into growing by that amount every generation from then on.
the trick
once the pattern stabilises the sum grows by a constant, so simulate long enough to see that step and extrapolate linearly.
python
def part2():
prev=0
summ=0
state=initial_state
for i in range(1000):
prev=summ
state=next_gen(state)
summ=sum(state)
print(summ+((summ-prev)*(50000000000-(i+1))))