Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add practice exercise game-of-life #755

Merged
merged 2 commits into from
Jul 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -969,6 +969,14 @@
"practices": [],
"prerequisites": [],
"difficulty": 5
},
{
"slug": "game-of-life",
"name": "Conway's Game of Life",
"uuid": "31e83423-a0c3-4745-8fca-24125e8ef6ce",
"practices": [],
"prerequisites": [],
"difficulty": 3
}
]
},
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
17 changes: 17 additions & 0 deletions exercises/practice/game-of-life/.meta/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"authors": [],
"files": {
"solution": [
"game-of-life.jl"
],
"test": [
"runtests.jl"
],
"example": [
".meta/example.jl"
]
},
"blurb": "Implement Conway's Game of Life.",
"source": "Wikipedia",
"source_url": "https://en.wikipedia.org/wiki/Conway%27s_Game_of_Life"
}
11 changes: 11 additions & 0 deletions exercises/practice/game-of-life/.meta/example.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
function gameoflife(matrix)
neighbors(x,y) = ((x+1,y),(x+1,y+1),(x,y+1),(x-1,y),(x-1,y-1),(x,y-1),(x-1,y+1),(x+1,y-1))
rows, cols = size(matrix)
padded = hcat(zeros(Int, rows+2), vcat(zeros(Int, cols)', matrix, zeros(Int, cols)'), zeros(Int, rows+2))
for i in 2:rows+1, j in 2:cols+1
alive = sum(padded[x, y] for (x, y) in neighbors(i, j))
alive == 3 && (matrix[i-1, j-1] = 1)
(alive < 2 || 3 < alive) && (matrix[i-1, j-1] = 0)
end
matrix
end
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.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
function gameoflife(matrix)
# your code here
end
99 changes: 99 additions & 0 deletions exercises/practice/game-of-life/runtests.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
using Test

include("game-of-life.jl")

@testset verbose = true "tests" begin
@testset "empty matrix" begin
matrix = reshape([], 0,2)

expected = reshape([], 0,2)
@test gameoflife(matrix) == expected
end

@testset "live cells with zero live neighbors die" begin
matrix = [0 0 0;
0 1 0;
0 0 0]

expected = [0 0 0;
0 0 0;
0 0 0]
@test gameoflife(matrix) == expected
end

@testset "live cells with only one live neighbor die" begin
matrix = [0 0 0;
0 1 0;
0 1 0]

expected = [0 0 0;
0 0 0;
0 0 0]
@test gameoflife(matrix) == expected
end

@testset "live cells with two live neighbors stay alive" begin
matrix = [1 0 1;
1 0 1;
1 0 1]

expected = [0 0 0;
1 0 1;
0 0 0]
@test gameoflife(matrix) == expected
end

@testset "live cells with three live neighbors stay alive" begin
matrix = [0 1 0;
1 0 0;
1 1 0]

expected = [0 0 0;
1 0 0;
1 1 0]
@test gameoflife(matrix) == expected
end

@testset "dead cells with three live neighbors become alive" begin
matrix = [1 1 0;
0 0 0;
1 0 0]

expected = [0 0 0;
1 1 0;
0 0 0]
@test gameoflife(matrix) == expected
end

@testset "live cells with four or more neighbors die" begin
matrix = [1 1 1;
1 1 1;
1 1 1]

expected = [1 0 1;
0 0 0;
1 0 1]
@test gameoflife(matrix) == expected
end

@testset "bigger matrix" begin
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]
@test gameoflife(matrix) == expected
end
end