Skip to content

Instantly share code, notes, and snippets.

@mlen
Created January 29, 2019 15:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mlen/1dc2a6ad92c2239d9dc256c0412fc324 to your computer and use it in GitHub Desktop.
Save mlen/1dc2a6ad92c2239d9dc256c0412fc324 to your computer and use it in GitHub Desktop.
import unittest
class Life(object):
def __init__(self, state):
self.state = state
def next(self):
cells_to_consider = set()
for x, y in self.state:
for i in range(-1, 2):
for j in range(-1, 2):
cells_to_consider.add((x+i, y+j))
new_state = set()
for x, y in cells_to_consider:
if self.isAlive((x, y) in self.state, self.countNeighbours(x, y)):
new_state.add((x, y))
return Life(new_state)
def isAlive(self, current, neigh):
if neigh == 2:
return current
elif neigh == 3:
return True
else:
return False
def countNeighbours(self, x, y):
sum = 0
for i in range(-1, 2):
for j in range(-1, 2):
if i == 0 and j == 0:
continue
if (x+i, y+j) in self.state:
sum += 1
return sum
class LifeTest(unittest.TestCase):
# YOLO
pass
if __name__ == '__main__':
unittest.main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment