Skip to content

Commit b84705d

Browse files
committed
feat(day 125): implement Conway's Game of Life next-state simulation
1 parent f2da7fc commit b84705d

1 file changed

Lines changed: 97 additions & 0 deletions

File tree

FreeCodeCamp/CodingQ/GameOfLife.py

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
"""
2+
Game of Life
3+
Given a matrix (array of arrays) representing the current state in Conway's Game of Life, return the next state of the matrix using these rules:
4+
5+
Each cell is either 1 (alive) or 0 (dead).
6+
A cell's neighbors are the up to eight surrounding cells (vertically, horizontally, and diagonally).
7+
Cells on the edges have fewer than eight neighbors.
8+
Rules for updating each cell:
9+
10+
Any live cell with fewer than two live neighbors dies (underpopulation).
11+
Any live cell with two or three live neighbors lives on.
12+
Any live cell with more than three live neighbors dies (overpopulation).
13+
Any dead cell with exactly three live neighbors becomes alive (reproduction).
14+
For example, given:
15+
16+
[
17+
[0, 1, 0],
18+
[0, 1, 1],
19+
[1, 1, 0]
20+
]
21+
return:
22+
23+
[
24+
[0, 1, 1],
25+
[0, 0, 1],
26+
[1, 1, 1]
27+
]
28+
Each cell updates according to the number of live neighbors. For instance, [0][0] stays dead (2 live neighbors), [0][1] stays alive (2 live neighbors), [0][2] dies (3 live neighbors), and so on.
29+
"""
30+
31+
import unittest
32+
33+
34+
class GameOfLifeTest(unittest.TestCase):
35+
36+
def test1(self):
37+
self.assertEqual(game_of_life([[0, 1, 0], [0, 1, 1], [1, 1, 0]]),[[0, 1, 1], [0, 0, 1], [1, 1, 1]])
38+
39+
def test2(self):
40+
self.assertEqual(game_of_life([[1, 1, 0, 0], [1, 0, 1, 0], [0, 1, 1, 1], [0, 0, 1, 0]]),[[1, 1, 0, 0], [1, 0, 0, 1], [0, 0, 0, 1], [0, 1, 1, 1]])
41+
42+
def test3(self):
43+
self.assertEqual(game_of_life([[1, 0, 0], [0, 1, 0], [0, 0, 1]]),[[0, 0, 0], [0, 1, 0], [0, 0, 0]])
44+
45+
def test4(self):
46+
self.assertEqual(game_of_life([[0, 1, 1, 0], [1, 1, 0, 1], [0, 1, 1, 0], [0, 0, 1, 0]]),[[1, 1, 1, 0], [1, 0, 0, 1], [1, 0, 0, 1], [0, 1, 1, 0]])
47+
48+
49+
50+
"""
51+
The algorithm is O(n x m) where n and m are dimensions of the board.
52+
Each cell is updated based on its neighbors, producing a new board state.
53+
Works for any rectangular grid, not just square ones.
54+
55+
"""
56+
def game_of_life(grid):
57+
58+
rows, cols = len(grid), len(grid[0])
59+
60+
def count_neighbors(r, c):
61+
directions = [
62+
(-1, -1), (-1, 0), (-1, 1),
63+
(0, -1), (0, 1),
64+
(1, -1), (1, 0), (1, 1)
65+
]
66+
67+
count = 0
68+
for dr, dc in directions:
69+
nr, nc = r + dr, c + dc
70+
if 0 <= nr < rows and 0 <= nc < cols:
71+
count += grid[nr][nc]
72+
73+
return count
74+
75+
new_board = [[0] * cols for _ in range(rows)]
76+
77+
for r in range(rows):
78+
for c in range(cols):
79+
neighbors = count_neighbors(r, c)
80+
if grid[r][c] == 1:
81+
if neighbors < 2 or neighbors > 3:
82+
new_board[r][c] = 0
83+
else:
84+
new_board[r][c] = 1
85+
else:
86+
if neighbors == 3:
87+
new_board[r][c] = 1
88+
89+
return new_board
90+
91+
92+
93+
if __name__ == "__main__":
94+
print(game_of_life([[0, 1, 1, 0], [1, 1, 0, 1], [0, 1, 1, 0], [0, 0, 1, 0]]))
95+
unittest.main()
96+
97+

0 commit comments

Comments
 (0)