Skip to content

Commit

Permalink
Add game-of-life exercise
Browse files Browse the repository at this point in the history
  • Loading branch information
ErikSchierboom committed Nov 26, 2024
1 parent 9d08d82 commit 8c0c049
Show file tree
Hide file tree
Showing 8 changed files with 191 additions and 0 deletions.
8 changes: 8 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -466,6 +466,14 @@
"prerequisites": [],
"difficulty": 8
},
{
"slug": "game-of-life",
"name": "Conway's Game of Life",
"uuid": "76ecd4a7-487d-4cb6-9fd0-15b6caa769b8",
"practices": [],
"prerequisites": [],
"difficulty": 8
},
{
"slug": "run-length-encoding",
"name": "Run-Length Encoding",
Expand Down
11 changes: 11 additions & 0 deletions exercises/practice/game-of-life/.docs/instructions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Instructions

After each generation, the cells interact with their eight neighbors, which are cells adjacent horizontally, vertically, or diagonally.

The following rules are applied to each cell:

- Any live cell with two or three live neighbors lives on.
- Any dead cell with exactly three live neighbors becomes a live cell.
- All other cells die or stay dead.

Given a matrix of 1s and 0s (corresponding to live and dead cells), apply the rules to each cell, and return the next generation.
9 changes: 9 additions & 0 deletions exercises/practice/game-of-life/.docs/introduction.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Introduction

[Conway's Game of Life][game-of-life] is a fascinating cellular automaton created by the British mathematician John Horton Conway in 1970.

The game consists of a two-dimensional grid of cells that can either be "alive" or "dead."

After each generation, the cells interact with their eight neighbors via a set of rules, which define the new generation.

[game-of-life]: https://en.wikipedia.org/wiki/Conway%27s_Game_of_Life
19 changes: 19 additions & 0 deletions exercises/practice/game-of-life/.meta/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"authors": [
"erikschierboom"
],
"files": {
"solution": [
"game-of-life.ua"
],
"test": [
"tests.ua"
],
"example": [
".meta/example.ua"
]
},
"blurb": "Implement Conway's Game of Life.",
"source": "Wikipedia",
"source_url": "https://en.wikipedia.org/wiki/Conway%27s_Game_of_Life"
}
1 change: 1 addition & 0 deletions exercises/practice/game-of-life/.meta/example.ua
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Tick ← ↥⊙↧∩=3,2-,/+≡⬚0↻☇1-1⇡3_3¤.
34 changes: 34 additions & 0 deletions exercises/practice/game-of-life/.meta/tests.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# This is an auto-generated file.
#
# Regenerating this file via `configlet sync` will:
# - Recreate every `description` key/value pair
# - Recreate every `reimplements` key/value pair, where they exist in problem-specifications
# - Remove any `include = true` key/value pair (an omitted `include` key implies inclusion)
# - Preserve any other key/value pair
#
# As user-added comments (using the # character) will be removed when this file
# is regenerated, comments can be added via a `comment` key.

[ae86ea7d-bd07-4357-90b3-ac7d256bd5c5]
description = "empty matrix"

[4ea5ccb7-7b73-4281-954a-bed1b0f139a5]
description = "live cells with zero live neighbors die"

[df245adc-14ff-4f9c-b2ae-f465ef5321b2]
description = "live cells with only one live neighbor die"

[2a713b56-283c-48c8-adae-1d21306c80ae]
description = "live cells with two live neighbors stay alive"

[86d5c5a5-ab7b-41a1-8907-c9b3fc5e9dae]
description = "live cells with three live neighbors stay alive"

[015f60ac-39d8-4c6c-8328-57f334fc9f89]
description = "dead cells with three live neighbors become alive"

[2ee69c00-9d41-4b8b-89da-5832e735ccf1]
description = "live cells with four or more neighbors die"

[a79b42be-ed6c-4e27-9206-43da08697ef6]
description = "bigger matrix"
3 changes: 3 additions & 0 deletions exercises/practice/game-of-life/game-of-life.ua
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Compute the next generation using Conway's Game of Life rules
# NextGeneration ? CurrentGeneration
Tick ← |1 ⊙(⍤ "Please implement Tick" 0)
106 changes: 106 additions & 0 deletions exercises/practice/game-of-life/tests.ua
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
~ "game-of-life.ua" ~ Tick

# Empty matrix
Matrix ← []
⍤⤙≍ 0 ⧻ Tick Matrix

# Live cells with zero live neighbors die
Matrix ← [
[0 0 0]
[0 1 0]
[0 0 0]
]
Expected ← [
[0 0 0]
[0 0 0]
[0 0 0]
]
⍤⤙≍ Expected Tick Matrix

# Live cells with only one live neighbor die
Matrix ← [
[0 0 0]
[0 1 0]
[0 1 0]
]
Expected ← [
[0 0 0]
[0 0 0]
[0 0 0]
]
⍤⤙≍ Expected Tick Matrix

# Live cells with two live neighbors stay alive
Matrix ← [
[1 0 1]
[1 0 1]
[1 0 1]
]
Expected ← [
[0 0 0]
[1 0 1]
[0 0 0]
]
⍤⤙≍ Expected Tick Matrix

# Live cells with three live neighbors stay alive
Matrix ← [
[0 1 0]
[1 0 0]
[1 1 0]
]
Expected ← [
[0 0 0]
[1 0 0]
[1 1 0]
]
⍤⤙≍ Expected Tick Matrix

# Dead cells with three live neighbors become alive
Matrix ← [
[1 1 0]
[0 0 0]
[1 0 0]
]
Expected ← [
[0 0 0]
[1 1 0]
[0 0 0]
]
⍤⤙≍ Expected Tick Matrix

# Live cells with four or more neighbors die
Matrix ← [
[1 1 1]
[1 1 1]
[1 1 1]
]
Expected ← [
[1 0 1]
[0 0 0]
[1 0 1]
]
⍤⤙≍ Expected Tick Matrix

# Bigger matrix
Matrix ← [
[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]
]
Expected ← [
[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]
]
⍤⤙≍ Expected Tick Matrix

0 comments on commit 8c0c049

Please sign in to comment.