Skip to content

Instantly share code, notes, and snippets.

@nsfyn55
Last active December 24, 2015 09:39
Show Gist options
  • Save nsfyn55/6778663 to your computer and use it in GitHub Desktop.
Save nsfyn55/6778663 to your computer and use it in GitHub Desktop.
Conway's game of life
def tick(world):
output_world = world.copy()
if len(world) <= 2:
return set()
for cell in world:
if len(get_live_neighbors(cell, world)) < 2:
output_world.remove(cell)
neighbors = get_neighbors(cell)
dead_neighbors = neighbors.difference(world)
for n in dead_neighbors:
if len(get_live_neighbors(n,world)) == 3:
output_world.add(n)
return output_world
def get_neighbors(cell):
world = set(([(x,y) for x in range(cell[0]-1, cell[0] + 2)
for y in range(cell[1] - 1, cell[1] + 2)]))
world.remove(cell)
return world
def get_live_neighbors(cell, world):
neighbors = get_neighbors(cell)
return world.intersection(neighbors)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment