o-o
This commit is contained in:
parent
4697f02e6f
commit
f00cad4c29
36
2020/17/17.py
Normal file
36
2020/17/17.py
Normal file
|
@ -0,0 +1,36 @@
|
||||||
|
def main(d):
|
||||||
|
# create a set of tuples
|
||||||
|
seed = open('input.txt').read().split('\n')
|
||||||
|
state = set(
|
||||||
|
(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
|
||||||
|
neighbors = [()] # list contains empty tuple which will be iterated over
|
||||||
|
for _ in range(d):
|
||||||
|
neighbors = [
|
||||||
|
t + (x,)
|
||||||
|
for x in [-1, 0, 1]
|
||||||
|
for t in neighbors
|
||||||
|
]
|
||||||
|
neighbors.remove(d * (0,)) # this is the center cell
|
||||||
|
|
||||||
|
# do 6 iterations according to rules
|
||||||
|
from collections import Counter
|
||||||
|
for _ in range(6):
|
||||||
|
state = set(
|
||||||
|
t
|
||||||
|
for t, count in Counter( # unpack the tuple and its count of neighboring active cells
|
||||||
|
tuple(map(sum, zip(t, n))) # add neighbors kernel to each active cell
|
||||||
|
for t in state
|
||||||
|
for n in neighbors
|
||||||
|
).items()
|
||||||
|
if count == 3 or t in state and count == 2
|
||||||
|
)
|
||||||
|
|
||||||
|
return len(state)
|
||||||
|
|
||||||
|
print(main(3), main(4))
|
8
2020/17/input.txt
Normal file
8
2020/17/input.txt
Normal file
|
@ -0,0 +1,8 @@
|
||||||
|
......##
|
||||||
|
####.#..
|
||||||
|
.##....#
|
||||||
|
.##.#..#
|
||||||
|
........
|
||||||
|
.#.#.###
|
||||||
|
#.##....
|
||||||
|
####.#..
|
Loading…
Reference in a new issue