-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgrid.py
More file actions
56 lines (44 loc) · 1.66 KB
/
grid.py
File metadata and controls
56 lines (44 loc) · 1.66 KB
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import collections
ALIVE = "♥"
DEAD = "‧"
class LifeGrid:
def __init__(self, pattern):
self.pattern = pattern
def evolve(self):
neighbors = (
(-1, -1), # Above left
(-1, 0), # Above
(-1, 1), # Above right
(0, -1), # Left
(0, 1), # Right
(1, -1), # Below left
(1, 0), # Below
(1, 1), # Below right
)
num_neighbors = collections.defaultdict(int)
for row, col in self.pattern.alive_cells:
for drow, dcol in neighbors:
num_neighbors[(row + drow, col + dcol)] += 1
stay_alive = {
cell for cell, num in num_neighbors.items() if num in {2, 3}
} & self.pattern.alive_cells
come_alive = {
cell for cell, num in num_neighbors.items() if num == 3
} - self.pattern.alive_cells
self.pattern.alive_cells = stay_alive | come_alive
return self.pattern.alive_cells
def as_string(self, bbox):
start_col, start_row, end_col, end_row = bbox
display = [self.pattern.name.center(2 * (end_col - start_col))]
for row in range(start_row, end_row):
display_row = [
ALIVE if (row, col) in self.pattern.alive_cells else DEAD
for col in range(start_col, end_col)
]
display.append(" ".join(display_row))
return "\n ".join(display)
def __str__(self):
return (
f"{self.pattern.name}:\n"
f"Alive cells -> {sorted(self.pattern.alive_cells)}"
)