Skip to content

Commit 35ebca7

Browse files
Add game-of-life exercise (#68)
1 parent 43e2452 commit 35ebca7

File tree

8 files changed

+144
-0
lines changed

8 files changed

+144
-0
lines changed

config.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -409,6 +409,14 @@
409409
"prerequisites": [],
410410
"difficulty": 5
411411
},
412+
{
413+
"slug": "game-of-life",
414+
"name": "Conway's Game of Life",
415+
"uuid": "f38b0cc2-0d10-40e7-9f25-ad254e93084d",
416+
"practices": [],
417+
"prerequisites": [],
418+
"difficulty": 6
419+
},
412420
{
413421
"slug": "flower-field",
414422
"name": "Flower Field",
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+
"game_of_life.fut"
8+
],
9+
"test": [
10+
"test.fut"
11+
],
12+
"example": [
13+
".meta/example.fut"
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: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
2+
def tick_cell [n] (matrix: [n][n]i32) (i: i64) (j: i64): i32 =
3+
let count = loop count = -matrix[i][j] for k in 0..<9 do
4+
let i2 = i + (k / 3) - 1
5+
let j2 = j + (k % 3) - 1
6+
in
7+
if i2 < 0 || i2 >= n || j2 < 0 || j2 >= n || matrix[i2][j2] != 1 then count else
8+
count + 1
9+
in
10+
-- if current cell is alive, treat 2 alive neighbours as 3
11+
if count | matrix[i][j] == 3 then 1 else
12+
0
13+
14+
def tick_row [n] (matrix: [n][n]i32) (i: i64): [n]i32 =
15+
tabulate n (tick_cell matrix i)
16+
17+
def tick [n] (matrix: [n][n]i32): [n][n]i32 =
18+
tabulate n (tick_row matrix)
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: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
def tick [n] (matrix: [n][n]i32): [n][n]i32 = ???
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import "game_of_life"
2+
3+
-- empty matrix
4+
-- ==
5+
-- input { empty([0][0]i32) }
6+
-- output { empty([0][0]i32) }
7+
8+
-- live cells with zero live neighbors die
9+
-- ==
10+
-- input { [[0, 0, 0], [0, 1, 0], [0, 0, 0]] }
11+
-- output { [[0, 0, 0], [0, 0, 0], [0, 0, 0]] }
12+
13+
-- live cells with only one live neighbor die
14+
-- ==
15+
-- input { [[0, 0, 0], [0, 1, 0], [0, 1, 0]] }
16+
-- output { [[0, 0, 0], [0, 0, 0], [0, 0, 0]] }
17+
18+
-- live cells with two live neighbors stay alive
19+
-- ==
20+
-- input { [[1, 0, 1], [1, 0, 1], [1, 0, 1]] }
21+
-- output { [[0, 0, 0], [1, 0, 1], [0, 0, 0]] }
22+
23+
-- live cells with three live neighbors stay alive
24+
-- ==
25+
-- input { [[0, 1, 0], [1, 0, 0], [1, 1, 0]] }
26+
-- output { [[0, 0, 0], [1, 0, 0], [1, 1, 0]] }
27+
28+
-- dead cells with three live neighbors become alive
29+
-- ==
30+
-- input { [[1, 1, 0], [0, 0, 0], [1, 0, 0]] }
31+
-- output { [[0, 0, 0], [1, 1, 0], [0, 0, 0]] }
32+
33+
-- live cells with four or more neighbors die
34+
-- ==
35+
-- input { [[1, 1, 1], [1, 1, 1], [1, 1, 1]] }
36+
-- output { [[1, 0, 1], [0, 0, 0], [1, 0, 1]] }
37+
38+
-- bigger matrix
39+
-- ==
40+
-- input { [[1, 1, 0, 1, 1, 0, 0, 0], [1, 0, 1, 1, 0, 0, 0, 0], [1, 1, 1, 0, 0, 1, 1, 1], [0, 0, 0, 0, 0, 1, 1, 0], [1, 0, 0, 0, 1, 1, 0, 0], [1, 1, 0, 0, 0, 1, 1, 1], [0, 0, 1, 0, 1, 0, 0, 1], [1, 0, 0, 0, 0, 0, 1, 1]] }
41+
-- output { [[1, 1, 0, 1, 1, 0, 0, 0], [0, 0, 0, 0, 0, 1, 1, 0], [1, 0, 1, 1, 1, 1, 0, 1], [1, 0, 0, 0, 0, 0, 0, 1], [1, 1, 0, 0, 1, 0, 0, 1], [1, 1, 0, 1, 0, 0, 0, 1], [1, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 1, 1]] }
42+
43+
let main [n] (matrix: [n][n]i32): [n][n]i32 =
44+
tick matrix

0 commit comments

Comments
 (0)