42 lines
1019 B
Python
42 lines
1019 B
Python
|
import fileinput
|
||
|
import numpy
|
||
|
from matplotlib import pyplot
|
||
|
from pprint import pprint
|
||
|
|
||
|
PADDING = (16, 256)
|
||
|
SIM_GENS = 150
|
||
|
EXTRAPOLATE_GENS = -SIM_GENS + 50000000000
|
||
|
|
||
|
def convert(line):
|
||
|
return numpy.array([True if x == "#" else False for x in line])
|
||
|
|
||
|
inp = fileinput.input()
|
||
|
|
||
|
# Read the initial state
|
||
|
pots = convert(next(inp).split()[-1])
|
||
|
next(inp)
|
||
|
|
||
|
rules = {}
|
||
|
for line in inp:
|
||
|
x, y = [convert(s) for s in line.split(" => ")]
|
||
|
rules[tuple(x)] = y[0]
|
||
|
|
||
|
output = numpy.zeros((SIM_GENS + 1, len(pots) + PADDING[0] + PADDING[1]), dtype=bool)
|
||
|
output[0] = numpy.pad(pots, (PADDING[0], PADDING[1]), "constant")
|
||
|
|
||
|
for y in range(SIM_GENS):
|
||
|
for i in range(output.shape[1] - 2):
|
||
|
key = tuple(output[y, i:i+5])
|
||
|
output[y + 1][i + 2] = rules.get(key, 0)
|
||
|
|
||
|
numbers = numpy.arange(-PADDING[0], pots.shape[0] + PADDING[1])
|
||
|
|
||
|
sum_a = numbers[output[-2]].sum()
|
||
|
sum_b = numbers[output[-1]].sum()
|
||
|
delta = sum_b - sum_a
|
||
|
print(EXTRAPOLATE_GENS * delta + sum_b)
|
||
|
|
||
|
pyplot.gray()
|
||
|
pyplot.imshow(1 - output)
|
||
|
pyplot.show()
|