-
Notifications
You must be signed in to change notification settings - Fork 39
/
solution_39.py
71 lines (59 loc) · 2.64 KB
/
solution_39.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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
def coding_problem_39(cells):
"""
Conway's Game of Life takes place on an infinite two-dimensional board of square cells. Each cell is either dead or
alive, and at each tick, the following rules apply:
Any live cell with less than two live neighbours dies.
Any live cell with two or three live neighbours remains living.
Any live cell with more than three live neighbours dies.
Any dead cell with exactly three live neighbours becomes a live cell.
A cell neighbours another cell if it is horizontally, vertically, or diagonally adjacent.
Implement Conway's Game of Life. It should be able to be initialized with a starting list of live cell coordinates
and the number of steps it should run for. Once initialized, it should print out the board state at each step.
Since it's an infinite board, print out only the relevant coordinates, i.e. from the top-leftmost live cell to
bottom-rightmost live cell.
You can represent a live cell with an asterisk (*) and a dead cell with a dot (.).
Example. Simulate a glider https://en.wikipedia.org/wiki/Glider_(Conway%27s_Life) advancing five steps:
>>> gol = coding_problem_39(((0, 1), (1, 2), (2, 0), (2, 1), (2, 2)))
>>> for _ in range(5):
... print(gol)
... gol.simulate()
..*
*.*
.**
<BLANKLINE>
*..
.**
**.
<BLANKLINE>
.*.
..*
***
<BLANKLINE>
*.*
.**
.*.
<BLANKLINE>
..*
*.*
.**
<BLANKLINE>
"""
class GameOfLife(object):
def __init__(self, cells):
self.displacements = tuple((xd, yd) for yd in [-1, 0, 1] for xd in [-1, 0, 1] if xd != 0 or yd != 0)
self.cells = set(cells)
def __str__(self):
xmin, xmax, ymin, ymax = [min_max(xy) for xy in zip(*self.cells) for min_max in [min, max]]
return ''.join(('*' if (x, y) in self.cells else '.') + ('\n' if x == xmax else '')
for y in range(ymin, ymax + 1) for x in range(xmin, xmax + 1))
def alive_next_round(self, x, y):
neighbour_count = sum(1 if (x + xd, y + yd) in self.cells else 0 for xd, yd in self.displacements)
return neighbour_count == 3 or ((x, y) in self.cells and neighbour_count == 2)
def simulate(self, steps=1):
for _ in range(steps):
active_cells = set((x + xd, y + yd) for x, y in self.cells for xd in [-1, 0, 1] for yd in [-1, 0, 1])
self.cells = set((x, y) for x, y in active_cells if self.alive_next_round(x, y))
return GameOfLife(cells)
if __name__ == '__main__':
import doctest
doctest.testmod(verbose=True)