-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame_of_life.py
59 lines (49 loc) · 1.33 KB
/
game_of_life.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
import sys
# Constants
ACTIVE_CHAR = '#'
INACTIVE_CHAR = '.'
ACTIVE_NB = [2, 3] # active cells with this many neighbour(s) remain active
INACTIVE_NB = [3] # inactive cells with this many neighbour(s) becomes active
TIME = 6
def num_active(x, y):
ans = 0
for dx in [-1, 0, 1]:
for dy in [-1, 0, 1]:
ans += d.get((x + dx, y + dy), 0)
return ans - d.get((x, y), 0)
def draw(d):
g = [[(INACTIVE_CHAR + ACTIVE_CHAR)[d.get((i, j), 0)] for j in range(len(m[0]))] for i in range(len(m))]
for r in g:
print(''.join(r))
"""
.........
.........
....#....
...###...
...#.#...
....#....
.........
.........
"""
m = []
for line in sys.stdin:
m.append(list(line.strip()))
d = {}
for i in range(len(m)):
for j in range(len(m[0])):
d[(i, j)] = int(m[i][j] == ACTIVE_CHAR)
def tick(d):
new_d = {}
for x, y in d:
for dx in [-1, 0, 1]:
for dy in [-1, 0, 1]:
if d.get((x + dx, y + dy), 0):
new_d[(x + dx, y + dy)] = int(num_active(x + dx, y + dy) in ACTIVE_NB)
else:
new_d[(x + dx, y + dy)] = int(num_active(x + dx, y + dy) in INACTIVE_NB)
return new_d
for t in range(TIME):
d = tick(d)
print('Generation', t+1)
draw(d)
print()