Files
InverseOfLife.NeuralSolver/Board/__init__.py
2025-01-26 16:13:49 +00:00

38 lines
1.2 KiB
Python

import itertools
class Board:
def __init__(self, width, height, quotient_x, quotient_y):
self.width = width
self.height = height
self.quotient_x = quotient_x
self.quotient_y = quotient_y
self.lives = set()
def toggle(self, x, y):
if (x, y) in self.lives:
self.lives.remove((x, y))
else:
self.lives.add((x, y))
def evaluate(self):
new_lives = set()
for (x, y) in itertools.product(range(self.width), range(self.height)):
neighbor_count = 0
for (dx, dy) in itertools.product([-1, 0, 1], [-1, 0, 1]):
if dx == 0 and dy == 0:
continue
nx = x + dx
ny = y + dy
if self.quotient_x:
nx %= self.width
if self.quotient_y:
ny %= self.height
if (nx, ny) in self.lives:
neighbor_count += 1
if (x, y) in self.lives and neighbor_count in [2, 3]:
new_lives.add((x, y))
if (x, y) not in self.lives and neighbor_count == 3:
new_lives.add((x, y))
self.lives = new_lives