Created
June 5, 2014 10:51
-
-
Save jhrr/2b5a60e9efcb573c9cc4 to your computer and use it in GitHub Desktop.
Jack Diederich's implementation of Conway's "Game of Life"
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import itertools | |
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