My Advent of Code solutions in Python.
kevinyap.ca/2019/12/going-fast-in-advent-of-code/
advent-of-code
python
1import fileinput
2
3
4INITIAL = None
5RULES = {}
6GENS = 200 # 200 generations is safe for the automata to converge
7TARGET_GEN = 50000000000
8
9for i, line in enumerate(fileinput.input()):
10 if i == 0:
11 INITIAL = line.split()[-1]
12 elif i >= 2:
13 a, _, b = line.split()
14 RULES[a] = b
15
16
17state = ['.'] * GENS + list(INITIAL) + ['.'] * GENS
18pot_sums = []
19
20for it in range(GENS):
21 amt = sum(i - GENS for i, p in enumerate(state) if p == '#')
22 pot_sums.append(amt)
23
24 next_state = ['.'] * len(state)
25 for i in range(len(state) - 2):
26 next_state[i + 2] = RULES.get(''.join(state[i:i+5]), '.')
27
28 state = next_state
29
30print "Sum of plants after 20 generations:", pot_sums[20]
31
32growth_per_gen = pot_sums[-1] - pot_sums[-2]
33remaining_gens = TARGET_GEN - GENS + 1
34print "Sum after 50000000000 generations:", growth_per_gen * remaining_gens + pot_sums[-1]