From c49022d11f83692b0b80782aaa0cc43864d2be4a Mon Sep 17 00:00:00 2001 From: Jason Turner Date: Thu, 12 Jan 2023 10:32:06 -0700 Subject: [PATCH] Fix life-codon to move out the neighbors struct --- python/conway_game_of_life/life-codon.py | 27 ++++++++++++------------ 1 file changed, 14 insertions(+), 13 deletions(-) diff --git a/python/conway_game_of_life/life-codon.py b/python/conway_game_of_life/life-codon.py index 63b6cfe..ff481bc 100644 --- a/python/conway_game_of_life/life-codon.py +++ b/python/conway_game_of_life/life-codon.py @@ -22,6 +22,18 @@ class Automata: survives: tuple[bool, bool, bool, bool, bool, bool, bool, bool, bool] data: list[bool] + neighbors = ( + Point(-1, -1), + Point(0, -1), + Point(1, -1), + Point(-1, 0), + Point(1, 0), + Point(-1, 1), + Point(0, 1), + Point(1, 1), + ) + + def __init__(self, width, height, born, survives): self.width = width self.height = height @@ -39,18 +51,7 @@ def set(self, p: Point): self.data[self.index(p)] = True def count_neighbors(self, p: Point): - neighbors = ( - Point(-1, -1), - Point(0, -1), - Point(1, -1), - Point(-1, 0), - Point(1, 0), - Point(-1, 1), - Point(0, 1), - Point(1, 1), - ) - - return sum(1 for loc in neighbors if self.get(loc + p)) + return sum(1 for loc in self.neighbors if self.get(loc + p)) def next(self): result = Automata(self.width, self.height, self.born, self.survives) @@ -85,7 +86,7 @@ def add_glider(self, p: Point): obj.add_glider(Point(0, 18)) -for i in range(0, 1000): +for i in range(0, 10000): obj = obj.next() for y in range(0, obj.height):