Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for Life Counter by tokiojapan55
def life_counter(state, tick_n):
AROUND = [(-1,-1),(-1,0),(-1,1),(0,-1),(0,1),(1,-1),(1,0),(1,1)]
lives = {(r,c) for r in range(len(state)) for c in range(len(state[0])) if state[r][c]}
for _ in range(tick_n):
death,born = set(),dict()
for r,c in lives:
neighbors = 0
for rc in [(r+dr,c+dc) for dr,dc in AROUND]:
if rc in lives:
neighbors += 1
else:
born[rc] = 1 if rc not in born else born[rc]+1
if neighbors<2 or neighbors>3:
death.add((r,c))
lives -= death
lives |= {rc for rc in born if born[rc]==3}
return len(lives)
Sept. 2, 2020
Comments: