Created
January 29, 2019 15:58
-
-
Save mlen/1dc2a6ad92c2239d9dc256c0412fc324 to your computer and use it in GitHub Desktop.
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 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