Skip to content

Commit 3d72d6c

Browse files
authored
Add game-of-life (#1586)
* Add `game-of-life` * Prettier format * Add .vscode JSON files
1 parent 50e5119 commit 3d72d6c

18 files changed

+7178
-1
lines changed

config.json

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,9 @@
3838
"slug": "lasagna",
3939
"name": "lasagna",
4040
"uuid": "a0bed44f-b179-4d09-8788-a23d2ace359e",
41-
"concepts": ["basics"],
41+
"concepts": [
42+
"basics"
43+
],
4244
"prerequisites": [],
4345
"status": "wip"
4446
}
@@ -1456,6 +1458,14 @@
14561458
"topics": [
14571459
"algorithms"
14581460
]
1461+
},
1462+
{
1463+
"slug": "game-of-life",
1464+
"name": "Conway's Game of Life",
1465+
"uuid": "c338cbce-fad4-4de0-bf0d-4a90803a0fe9",
1466+
"practices": [],
1467+
"prerequisites": [],
1468+
"difficulty": 2
14591469
}
14601470
]
14611471
},
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Instructions
2+
3+
After each generation, the cells interact with their eight neighbors, which are cells adjacent horizontally, vertically, or diagonally.
4+
5+
The following rules are applied to each cell:
6+
7+
- Any live cell with two or three live neighbors lives on.
8+
- Any dead cell with exactly three live neighbors becomes a live cell.
9+
- All other cells die or stay dead.
10+
11+
Given a matrix of 1s and 0s (corresponding to live and dead cells), apply the rules to each cell, and return the next generation.
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# Introduction
2+
3+
[Conway's Game of Life][game-of-life] is a fascinating cellular automaton created by the British mathematician John Horton Conway in 1970.
4+
5+
The game consists of a two-dimensional grid of cells that can either be "alive" or "dead."
6+
7+
After each generation, the cells interact with their eight neighbors via a set of rules, which define the new generation.
8+
9+
[game-of-life]: https://en.wikipedia.org/wiki/Conway%27s_Game_of_Life
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"authors": [
3+
"BNAndras"
4+
],
5+
"files": {
6+
"solution": [
7+
"game-of-life.ts"
8+
],
9+
"test": [
10+
"game-of-life.test.ts"
11+
],
12+
"example": [
13+
".meta/proof.ci.ts"
14+
]
15+
},
16+
"blurb": "Implement Conway's Game of Life.",
17+
"source": "Wikipedia",
18+
"source_url": "https://en.wikipedia.org/wiki/Conway%27s_Game_of_Life"
19+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
export class GameOfLife {
2+
constructor(private matrix: number[][]) {}
3+
4+
public tick(): void {
5+
if (this.matrix.length === 0) {
6+
return
7+
}
8+
const rows = this.matrix.length
9+
const cols = this.matrix[0].length
10+
11+
const newMatrix = JSON.parse(JSON.stringify(this.matrix)) as number[][]
12+
13+
for (let row = 0; row < rows; row++) {
14+
for (let col = 0; col < cols; col++) {
15+
let liveNeighbors = 0
16+
for (let newRow = row - 1; newRow <= row + 1; newRow++) {
17+
for (let newCol = col - 1; newCol <= col + 1; newCol++) {
18+
if (newRow === row && newCol === col) {
19+
continue
20+
}
21+
if (newRow >= 0 && newRow < rows && newCol >= 0 && newCol < cols) {
22+
liveNeighbors += this.matrix[newRow][newCol]
23+
}
24+
}
25+
}
26+
27+
let cell = this.matrix[row][col]
28+
if (cell === 1) {
29+
if (liveNeighbors < 2 || liveNeighbors > 3) {
30+
cell = 0
31+
}
32+
} else {
33+
if (liveNeighbors === 3) {
34+
cell = 1
35+
}
36+
}
37+
38+
newMatrix[row][col] = cell
39+
}
40+
}
41+
42+
this.matrix = newMatrix
43+
}
44+
45+
public state(): number[][] {
46+
return this.matrix
47+
}
48+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# This is an auto-generated file.
2+
#
3+
# Regenerating this file via `configlet sync` will:
4+
# - Recreate every `description` key/value pair
5+
# - Recreate every `reimplements` key/value pair, where they exist in problem-specifications
6+
# - Remove any `include = true` key/value pair (an omitted `include` key implies inclusion)
7+
# - Preserve any other key/value pair
8+
#
9+
# As user-added comments (using the # character) will be removed when this file
10+
# is regenerated, comments can be added via a `comment` key.
11+
12+
[ae86ea7d-bd07-4357-90b3-ac7d256bd5c5]
13+
description = "empty matrix"
14+
15+
[4ea5ccb7-7b73-4281-954a-bed1b0f139a5]
16+
description = "live cells with zero live neighbors die"
17+
18+
[df245adc-14ff-4f9c-b2ae-f465ef5321b2]
19+
description = "live cells with only one live neighbor die"
20+
21+
[2a713b56-283c-48c8-adae-1d21306c80ae]
22+
description = "live cells with two live neighbors stay alive"
23+
24+
[86d5c5a5-ab7b-41a1-8907-c9b3fc5e9dae]
25+
description = "live cells with three live neighbors stay alive"
26+
27+
[015f60ac-39d8-4c6c-8328-57f334fc9f89]
28+
description = "dead cells with three live neighbors become alive"
29+
30+
[2ee69c00-9d41-4b8b-89da-5832e735ccf1]
31+
description = "live cells with four or more neighbors die"
32+
33+
[a79b42be-ed6c-4e27-9206-43da08697ef6]
34+
description = "bigger matrix"
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"recommendations": [
3+
"arcanis.vscode-zipfs",
4+
"dbaeumer.vscode-eslint",
5+
"esbenp.prettier-vscode"
6+
]
7+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"cSpell.words": ["exercism"],
3+
"search.exclude": {
4+
"**/.yarn": true,
5+
"**/.pnp.*": true
6+
}
7+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
compressionLevel: mixed
2+
3+
enableGlobalCache: true
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module.exports = {
2+
// eslint-disable-next-line @typescript-eslint/no-require-imports
3+
presets: [[require('@exercism/babel-preset-typescript'), { corejs: '3.38' }]],
4+
plugins: [],
5+
}

0 commit comments

Comments
 (0)