Skip to content

Instantly share code, notes, and snippets.

@mickeypash
Created July 10, 2016 10:20
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save mickeypash/39c8ddabdec85ea247f736ed9c2c7f41 to your computer and use it in GitHub Desktop.
Save mickeypash/39c8ddabdec85ea247f736ed9c2c7f41 to your computer and use it in GitHub Desktop.
Conway's Really Simple Game of Life based on ("Stop Writing Classes" PyCon 2012)[https://www.youtube.com/watch?v=o9pEzgHorH0]
import itertools
# Conway's Really Simple Game of Life based on "Stop Writing Classes" PyCon 2012
def neighbors(point):
x,y = point
yield x + 1, y
yield x - 1, y
yield x, y + 1
yield x, y - 1
yield x + 1, y + 1
yield x + 1, y - 1
yield x - 1, y + 1
yield x - 1, y - 1
def advance(board):
newstate = set()
recalc = board | set(itertools.chain(*map(neighbors, board)))
for point in recalc:
count = sum((neigh in board)
for neigh in neighbors(point))
if count == 3 or (count == 2 and point in board):
newstate.add(point)
return newstate
glider = set([(0, 0), (1, 0), (2, 0), (0, 1), (1, 2)])
for i in range(1000):
glider = advance(glider)
print glider
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment