-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgameOfLife.py
More file actions
25 lines (23 loc) · 831 Bytes
/
gameOfLife.py
File metadata and controls
25 lines (23 loc) · 831 Bytes
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
class Solution:
def gameOfLife(self, board: list[list[int]]) -> None:
m = len(board)
n = len(board[0])
for i in range(m):
for j in range(n):
ones = 0
for x in range(max(0, i - 1), min(m, i + 2)):
for y in range(max(0, j - 1), min(n, j + 2)):
ones += board[x][y] & 1
print(board[x][y] & 1)
print(board[x][y])
# Any live cell with 2 or 3 live neighbors
# lives on to the next generation
if board[i][j] == 1 and (ones == 3 or ones == 4):
board[i][j] |= 0b10
# Any dead cell with exactly 3 live neighbors
# becomes a live cell, as if by reproduction
if board[i][j] == 0 and ones == 3:
board[i][j] |= 0b10
for i in range(m):
for j in range(n):
board[i][j] >>= 1