-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrules.py
34 lines (22 loc) · 892 Bytes
/
rules.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
from dataclasses import dataclass
from abc import ABC, abstractmethod
class Rules(ABC):
@abstractmethod
def survives(self, live_neighbors: int) -> bool: # pragma: no cover
pass
@abstractmethod
def born(self, live_neighbors: int) -> bool: # pragma: no cover
pass
def set_str(s: set) -> str:
return "".join(str(i) for i in s)
@dataclass(frozen=True)
class StandardRules(Rules):
_liveNeighborsForSurvival: set
_liveNeighborsForBirth: set
def survives(self, live_neighbors: int) -> bool:
return live_neighbors in self._liveNeighborsForSurvival
def born(self, live_neighbors: int) -> bool:
return live_neighbors in self._liveNeighborsForBirth
def __str__(self) -> str:
return f"R {set_str(self._liveNeighborsForSurvival)}/{set_str(self._liveNeighborsForBirth)}"
ConwayRules = StandardRules({2, 3}, {3})