53 lines
1.2 KiB
Python
53 lines
1.2 KiB
Python
|
def to_int(bitlist):
|
||
|
out = 0
|
||
|
for bit in bitlist:
|
||
|
out = out * 2 + bit
|
||
|
return out
|
||
|
|
||
|
|
||
|
def get_slice(index, pots):
|
||
|
for i in range(index - 2, index + 3):
|
||
|
if i < 0:
|
||
|
yield 0
|
||
|
yield pots[i]
|
||
|
|
||
|
|
||
|
def grow(init, num_gens):
|
||
|
current_gen = init
|
||
|
for g in range(num_gens):
|
||
|
while current_gen[:2] != [0, 0]:
|
||
|
current_gen.insert(0, 0)
|
||
|
while current_gen[-2:] != [0, 0]:
|
||
|
current_gen.append(0)
|
||
|
|
||
|
next_gen = []
|
||
|
for i, b in enumerate(current_gen):
|
||
|
key = to_int(get_slice(i, current_gen))
|
||
|
if key in positives:
|
||
|
next_gen.append(1)
|
||
|
else:
|
||
|
next_gen.append(0)
|
||
|
current_gen = next_gen
|
||
|
|
||
|
return current_gen
|
||
|
|
||
|
|
||
|
with open('data/12.txt') as f:
|
||
|
initial = list(next(f)[15:-1])
|
||
|
initial = list(map(lambda p: int(p == '#'), initial))
|
||
|
next(f)
|
||
|
positives, negatives = set(), set()
|
||
|
for line in f:
|
||
|
state, result = line.strip().split(' => ')
|
||
|
s = to_int(int(c == '#') for c in state)
|
||
|
if result == '#':
|
||
|
positives.add(s)
|
||
|
else:
|
||
|
negatives.add(s)
|
||
|
|
||
|
|
||
|
def test(n):
|
||
|
result = grow(initial, n)
|
||
|
print(''.join('#' if p else '.' for p in result))
|
||
|
|