-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconways.py
127 lines (93 loc) · 3.11 KB
/
conways.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
import copy
def dies(x: int, y: int, matrix: list[list[int]]) -> bool:
neighbours = count_neighbours(x, y, matrix)
return neighbours < 2 or neighbours > 3
def lives(x: int, y: int, matrix: list[list[int]]) -> bool:
return count_neighbours(x, y, matrix) in [2, 3]
def new_cell(x: int, y: int, matrix: list[list[int]]) -> bool:
return count_neighbours(x, y, matrix) == 3
def count_neighbours(x: int, y: int, matrix: list[list[int]]) -> int:
positions = [
(x-1, y-1),
(x-1, y),
(x-1, y+1),
(x, y-1),
(x, y+1),
(x+1, y-1),
(x+1, y),
(x+1, y+1)
]
total = 0
for position_x, position_y in positions:
minor = position_x < 0 or position_y < 0
no_index = position_x+1> len(matrix) or position_y+1 > len(matrix[position_x])
if minor or no_index:
continue
if matrix[position_x] and matrix[position_x][position_y]:
total += 1
continue
return total
def run_cells(matrix: list[list[int]]) -> list[list[int]]:
validation_copy = copy.deepcopy(matrix)
for row, row_elements in enumerate(validation_copy):
for column, has_element in enumerate(row_elements):
if dies(row, column, validation_copy) and has_element:
matrix[row][column] = 0
continue
elif new_cell(row, column, validation_copy) and not has_element:
matrix[row][column] = 1
continue
return matrix
def generate_empty_values(matrix: list[list[int]]) -> list[list[int]]:
row_length = len(matrix[0]) + 2
new_matrix = []
if any(matrix[0]):
new_matrix.append([0 for _ in range(row_length)])
for row in matrix:
new_matrix.append([0] + row + [0])
if any(matrix[-1]):
new_matrix.append([0 for _ in range(row_length)])
return new_matrix
def remove_empty_values(matrix: list[list[int]]) -> list[list[int]]:
result = []
for row, elements in enumerate(matrix):
if 1 not in elements and row in [0, len(matrix)-1]:
continue
result.append(elements)
first_col_empty = not any([c[0] == 1 for c in result])
last_col_empty = not any([c[-1] == 1 for c in result])
for row, elements in enumerate(result):
if first_col_empty:
result[row] = result[row][1:]
if last_col_empty:
result[row] = result[row][:-1]
if not any(matrix[0]) or not any(matrix[-1]):
return remove_empty_values(result)
return result
def get_generation(cells : list[list[int]], generations : int) -> list[list[int]]:
for gen in range(generations):
cells = generate_empty_values(cells)
cells = run_cells(cells)
cells = remove_empty_values(cells)
return cells
def print_matrix(matrix, squares = True) -> None:
result = ''
for row in matrix:
for value in row:
if value == 1 and squares:
result+= '■'
continue
elif value == 0 and squares:
result+= '□'
continue
result += str(value)
result += '\n'
print(result)
if __name__ == '__main__':
input = [
[1,1,1,0,0,0,1,0],
[1,0,0,0,0,0,0,1],
[0,1,0,0,0,1,1,1]
]
# Resulting 2D Matrix after 16 generations
print_matrix(get_generation(input, 16))