Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Uncategorized category for Life Counter by Moff
from collections import defaultdict
class InfiniteLife(object):
def __init__(self):
self.area = set()
self.delta = [(-1, -1), (-1, 0), (-1, 1), (0, -1), (0, 1), (1, -1), (1, 0), (1, 1)]
def boundary_cell(self, cell):
return [(cell[0] + i, cell[1] + j) for i, j in self.delta]
def boundary_set(self):
result = set()
for cell in self.area:
for c in self.boundary_cell(cell):
result.add(c)
return result
def boundary_dict(self):
result = defaultdict(int)
for cell in self.area:
for c in self.boundary_cell(cell):
result[c] += 1
return result
def neighbours_cell(self, cell):
return [c for c in self.boundary_cell(cell) if c in self.area]
def iterate(self):
new_area = set()
for c, n in self.boundary_dict().items():
if n == 3:
new_area.add(c)
elif n == 2 and c in self.area:
new_area.add(c)
self.area = new_area
def life_counter(area, n):
game = InfiniteLife()
game.area = set((i, j) for i, row in enumerate(area) for j, cell in enumerate(row) if cell)
for _ in range(n):
game.iterate()
return len(game.area)
Aug. 4, 2015