diff --git a/config.json b/config.json index a0379614..911f9405 100644 --- a/config.json +++ b/config.json @@ -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 } ] }, diff --git a/exercises/practice/game-of-life/.docs/instructions.md b/exercises/practice/game-of-life/.docs/instructions.md new file mode 100644 index 00000000..49531406 --- /dev/null +++ b/exercises/practice/game-of-life/.docs/instructions.md @@ -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. diff --git a/exercises/practice/game-of-life/.docs/introduction.md b/exercises/practice/game-of-life/.docs/introduction.md new file mode 100644 index 00000000..2347b936 --- /dev/null +++ b/exercises/practice/game-of-life/.docs/introduction.md @@ -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 diff --git a/exercises/practice/game-of-life/.meta/config.json b/exercises/practice/game-of-life/.meta/config.json new file mode 100644 index 00000000..3ba3ca0e --- /dev/null +++ b/exercises/practice/game-of-life/.meta/config.json @@ -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" +} diff --git a/exercises/practice/game-of-life/.meta/example.jl b/exercises/practice/game-of-life/.meta/example.jl new file mode 100644 index 00000000..3968593a --- /dev/null +++ b/exercises/practice/game-of-life/.meta/example.jl @@ -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 diff --git a/exercises/practice/game-of-life/.meta/tests.toml b/exercises/practice/game-of-life/.meta/tests.toml new file mode 100644 index 00000000..398cd454 --- /dev/null +++ b/exercises/practice/game-of-life/.meta/tests.toml @@ -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" diff --git a/exercises/practice/game-of-life/game-of-life.jl b/exercises/practice/game-of-life/game-of-life.jl new file mode 100644 index 00000000..954cc6aa --- /dev/null +++ b/exercises/practice/game-of-life/game-of-life.jl @@ -0,0 +1,3 @@ +function gameoflife(matrix) + # your code here +end diff --git a/exercises/practice/game-of-life/runtests.jl b/exercises/practice/game-of-life/runtests.jl new file mode 100644 index 00000000..d005730b --- /dev/null +++ b/exercises/practice/game-of-life/runtests.jl @@ -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