Skip to content

Commit 186d670

Browse files
Add game-of-life exercise
1 parent 2ed4818 commit 186d670

File tree

8 files changed

+260
-0
lines changed

8 files changed

+260
-0
lines changed

config.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -546,6 +546,14 @@
546546
"strings"
547547
]
548548
},
549+
{
550+
"slug": "game-of-life",
551+
"name": "Game Of Life",
552+
"uuid": "a870cea4-04e6-4423-aa12-a72da5d18e69",
553+
"practices": [],
554+
"prerequisites": [],
555+
"difficulty": 5
556+
},
549557
{
550558
"slug": "knapsack",
551559
"name": "Knapsack",
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+
"keiravillekode"
4+
],
5+
"files": {
6+
"solution": [
7+
"source/game_of_life.d"
8+
],
9+
"test": [
10+
"source/game_of_life.d"
11+
],
12+
"example": [
13+
"example/game_of_life.d"
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: 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: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
name "game-of-life"
2+
buildRequirements "disallowDeprecations"
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
module game_of_life;
2+
3+
import std.algorithm : max, min;
4+
5+
pure int[][] tick(immutable int[][] matrix)
6+
{
7+
int[][] result;
8+
size_t numRows = matrix.length;
9+
if (numRows == 0)
10+
{
11+
return result;
12+
}
13+
size_t numColumns = matrix[0].length;
14+
result.length = numRows;
15+
for (size_t row = 0; row < numRows; ++row)
16+
{
17+
result[row].length = numColumns;
18+
for (size_t column = 0; column < numColumns; ++column)
19+
{
20+
int previous = matrix[row][column];
21+
int count = -previous;
22+
for (size_t r = max(1, row) - 1; r < min(numRows, row + 2); ++r)
23+
{
24+
for (size_t c = max(1, column) - 1; c < min(numColumns, column + 2); ++c)
25+
{
26+
count += matrix[r][c];
27+
}
28+
}
29+
30+
int next = 0;
31+
if (count == 3 || (count == 2 && previous == 1))
32+
{
33+
next = 1;
34+
}
35+
result[row][column] = next;
36+
}
37+
}
38+
return result;
39+
}
Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
module game_of_life;
2+
3+
pure int[][] tick(immutable int[][] matrix)
4+
{
5+
// implement this function
6+
}
7+
8+
unittest
9+
{
10+
immutable int allTestsEnabled = 0;
11+
12+
// Empty matrix
13+
{
14+
immutable int[][] matrix = [
15+
];
16+
int[][] expected = [
17+
];
18+
assert(tick(matrix) == expected);
19+
}
20+
21+
static if (allTestsEnabled)
22+
{
23+
// Live cells with zero live neighbors die
24+
{
25+
immutable int[][] matrix = [
26+
[0, 0, 0],
27+
[0, 1, 0],
28+
[0, 0, 0],
29+
];
30+
int[][] expected = [
31+
[0, 0, 0],
32+
[0, 0, 0],
33+
[0, 0, 0],
34+
];
35+
assert(tick(matrix) == expected);
36+
}
37+
38+
// Live cells with only one live neighbor die
39+
{
40+
immutable int[][] matrix = [
41+
[0, 0, 0],
42+
[0, 1, 0],
43+
[0, 1, 0],
44+
];
45+
int[][] expected = [
46+
[0, 0, 0],
47+
[0, 0, 0],
48+
[0, 0, 0],
49+
];
50+
assert(tick(matrix) == expected);
51+
}
52+
53+
// Live cells with two live neighbors stay alive
54+
{
55+
immutable int[][] matrix = [
56+
[1, 0, 1],
57+
[1, 0, 1],
58+
[1, 0, 1],
59+
];
60+
int[][] expected = [
61+
[0, 0, 0],
62+
[1, 0, 1],
63+
[0, 0, 0],
64+
];
65+
assert(tick(matrix) == expected);
66+
}
67+
68+
// Live cells with three live neighbors stay alive
69+
{
70+
immutable int[][] matrix = [
71+
[0, 1, 0],
72+
[1, 0, 0],
73+
[1, 1, 0],
74+
];
75+
int[][] expected = [
76+
[0, 0, 0],
77+
[1, 0, 0],
78+
[1, 1, 0],
79+
];
80+
assert(tick(matrix) == expected);
81+
}
82+
83+
// Dead cells with three live neighbors become alive
84+
{
85+
immutable int[][] matrix = [
86+
[1, 1, 0],
87+
[0, 0, 0],
88+
[1, 0, 0],
89+
];
90+
int[][] expected = [
91+
[0, 0, 0],
92+
[1, 1, 0],
93+
[0, 0, 0],
94+
];
95+
assert(tick(matrix) == expected);
96+
}
97+
98+
// Live cells with four or more neighbors die
99+
{
100+
immutable int[][] matrix = [
101+
[1, 1, 1],
102+
[1, 1, 1],
103+
[1, 1, 1],
104+
];
105+
int[][] expected = [
106+
[1, 0, 1],
107+
[0, 0, 0],
108+
[1, 0, 1],
109+
];
110+
assert(tick(matrix) == expected);
111+
}
112+
113+
// Bigger matrix
114+
{
115+
immutable int[][] matrix = [
116+
[1, 1, 0, 1, 1, 0, 0, 0],
117+
[1, 0, 1, 1, 0, 0, 0, 0],
118+
[1, 1, 1, 0, 0, 1, 1, 1],
119+
[0, 0, 0, 0, 0, 1, 1, 0],
120+
[1, 0, 0, 0, 1, 1, 0, 0],
121+
[1, 1, 0, 0, 0, 1, 1, 1],
122+
[0, 0, 1, 0, 1, 0, 0, 1],
123+
[1, 0, 0, 0, 0, 0, 1, 1],
124+
];
125+
int[][] expected = [
126+
[1, 1, 0, 1, 1, 0, 0, 0],
127+
[0, 0, 0, 0, 0, 1, 1, 0],
128+
[1, 0, 1, 1, 1, 1, 0, 1],
129+
[1, 0, 0, 0, 0, 0, 0, 1],
130+
[1, 1, 0, 0, 1, 0, 0, 1],
131+
[1, 1, 0, 1, 0, 0, 0, 1],
132+
[1, 0, 0, 0, 0, 0, 0, 0],
133+
[0, 0, 0, 0, 0, 0, 1, 1],
134+
];
135+
assert(tick(matrix) == expected);
136+
}
137+
}
138+
}

0 commit comments

Comments
 (0)