aoc/2020/17/17.py

36 lines
994 B
Python
Raw Normal View History

2020-12-17 09:26:28 +00:00
def main(d):
# create a set of tuples
seed = open('input.txt').read().split('\n')
2020-12-17 19:07:48 +00:00
active = set(
2020-12-17 09:26:28 +00:00
(d - 2) * (0,) + (i, j) # zero-pad coords
for i, row in enumerate(seed)
for j, cell in enumerate(row)
if cell == '#'
)
# generate all tuples -1..1 in all dimensions
2020-12-17 19:09:26 +00:00
neighboring = [()] # list contains empty tuple which will be iterated over
2020-12-17 09:26:28 +00:00
for _ in range(d):
2020-12-17 19:09:26 +00:00
neighboring = [
2020-12-17 19:12:54 +00:00
_tuple + (offset,)
for offset in [-1, 0, 1]
for _tuple in neighboring
2020-12-17 09:26:28 +00:00
]
2020-12-17 19:09:26 +00:00
neighboring.remove(d * (0,)) # this is the center cell
2020-12-17 09:26:28 +00:00
# do 6 iterations according to rules
from collections import Counter
for _ in range(6):
2020-12-17 19:07:48 +00:00
active = set(
2020-12-17 19:12:54 +00:00
_tuple
for _tuple, count in Counter( # unpack the tuple and its count of neighboring active cells
tuple(map(sum, zip(_tuple, offset))) # add neighbors kernel to each active cell
for _tuple in active
for offset in neighboring
2020-12-17 09:26:28 +00:00
).items()
2020-12-17 19:12:54 +00:00
if count == 3 or _tuple in active and count == 2
2020-12-17 09:26:28 +00:00
)
2020-12-17 19:07:48 +00:00
return len(active)
2020-12-17 09:26:28 +00:00
print(main(3), main(4))