From e788c9aaa5e2548c4388102f3fca76df39d632b9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A1s=20B=20Nagy?= <20251272+BNAndras@users.noreply.github.com> Date: Fri, 4 Apr 2025 18:55:28 -0700 Subject: [PATCH] Add `game-of-life` --- config.json | 8 ++ .../game-of-life/.docs/instructions.md | 11 +++ .../game-of-life/.docs/introduction.md | 9 ++ .../practice/game-of-life/.meta/config.json | 19 ++++ .../practice/game-of-life/.meta/example.vim | 37 ++++++++ .../practice/game-of-life/.meta/tests.toml | 34 +++++++ .../practice/game-of-life/game_of_life.vader | 91 +++++++++++++++++++ .../practice/game-of-life/game_of_life.vim | 8 ++ 8 files changed, 217 insertions(+) create mode 100644 exercises/practice/game-of-life/.docs/instructions.md create mode 100644 exercises/practice/game-of-life/.docs/introduction.md create mode 100644 exercises/practice/game-of-life/.meta/config.json create mode 100644 exercises/practice/game-of-life/.meta/example.vim create mode 100644 exercises/practice/game-of-life/.meta/tests.toml create mode 100644 exercises/practice/game-of-life/game_of_life.vader create mode 100644 exercises/practice/game-of-life/game_of_life.vim diff --git a/config.json b/config.json index 4724d4b..ff94d80 100644 --- a/config.json +++ b/config.json @@ -122,6 +122,14 @@ "prerequisites": [], "difficulty": 2 }, + { + "slug": "game-of-life", + "name": "Conway's Game of Life", + "uuid": "6e275aa3-4af6-45d6-be39-1f68d098afd3", + "practices": [], + "prerequisites": [], + "difficulty": 2 + }, { "slug": "darts", "name": "Darts", 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 0000000..4953140 --- /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 0000000..2347b93 --- /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 0000000..248d5a8 --- /dev/null +++ b/exercises/practice/game-of-life/.meta/config.json @@ -0,0 +1,19 @@ +{ + "authors": [ + "BNAndras" + ], + "files": { + "solution": [ + "game_of_life.vim" + ], + "test": [ + "game_of_life.vader" + ], + "example": [ + ".meta/example.vim" + ] + }, + "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.vim b/exercises/practice/game-of-life/.meta/example.vim new file mode 100644 index 0000000..9a685a5 --- /dev/null +++ b/exercises/practice/game-of-life/.meta/example.vim @@ -0,0 +1,37 @@ +function! Tick(matrix) abort + if empty(a:matrix) + return [] + endif + + let l:rows = len(a:matrix) + let l:cols = len(a:matrix[0]) + let l:new_matrix = deepcopy(a:matrix) + + for l:row in range(l:rows) + for l:col in range(l:cols) + let l:live_neighbors = 0 + for l:dRow in [-1, 0, 1] + for l:dCol in [-1, 0, 1] + if l:dRow == 0 && l:dCol == 0 + continue " Skip current cell + endif + + let l:nRow = l:row + l:dRow + let l:nCol = l:col + l:dCol + + if l:nRow >= 0 && l:nRow < l:rows && l:nCol >= 0 && l:nCol < l:cols + let l:live_neighbors += a:matrix[l:nRow][l:nCol] + endif + endfor + endfor + + if a:matrix[l:row][l:col] == 1 && l:live_neighbors < 2 || l:live_neighbors > 3 + let l:new_matrix[l:row][l:col] = 0 + elseif a:matrix[l:row][l:col] == 0 && l:live_neighbors == 3 + let l:new_matrix[l:row][l:col] = 1 + endif + endfor + endfor + + return l:new_matrix +endfunction 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 0000000..398cd45 --- /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.vader b/exercises/practice/game-of-life/game_of_life.vader new file mode 100644 index 0000000..2b53af2 --- /dev/null +++ b/exercises/practice/game-of-life/game_of_life.vader @@ -0,0 +1,91 @@ +Execute (empty matrix): + let g:matrix = [] + let g:expected = [] + AssertEqual g:expected, Tick(g:matrix) + +Execute (live cells with zero live neighbors die): + let g:matrix = [ + \ [0, 0, 0], + \ [0, 1, 0], + \ [0, 0, 0]] + let g:expected = [ + \ [0, 0, 0], + \ [0, 0, 0], + \ [0, 0, 0]] + AssertEqual g:expected, Tick(g:matrix) + +Execute (live cells with only one live neighbor die): + let g:matrix = [ + \ [0, 0, 0], + \ [0, 1, 0], + \ [0, 1, 0]] + let g:expected = [ + \ [0, 0, 0], + \ [0, 0, 0], + \ [0, 0, 0]] + AssertEqual g:expected, Tick(g:matrix) + +Execute (live cells with two live neighbors stay alive): + let g:matrix = [ + \ [1, 0, 1], + \ [1, 0, 1], + \ [1, 0, 1]] + let g:expected = [ + \ [0, 0, 0], + \ [1, 0, 1], + \ [0, 0, 0]] + AssertEqual g:expected, Tick(g:matrix) + +Execute (live cells with three live neighbors stay alive): + let g:matrix = [ + \ [0, 1, 0], + \ [1, 0, 0], + \ [1, 1, 0]] + let g:expected = [ + \ [0, 0, 0], + \ [1, 0, 0], + \ [1, 1, 0]] + AssertEqual g:expected, Tick(g:matrix) + +Execute (dead cells with three live neighbors become alive): + let g:matrix = [ + \ [1, 1, 0], + \ [0, 0, 0], + \ [1, 0, 0]] + let g:expected = [ + \ [0, 0, 0], + \ [1, 1, 0], + \ [0, 0, 0]] + AssertEqual g:expected, Tick(g:matrix) + +Execute (live cells with four or more neighbors die): + let g:matrix = [ + \ [1, 1, 1], + \ [1, 1, 1], + \ [1, 1, 1]] + let g:expected = [ + \ [1, 0, 1], + \ [0, 0, 0], + \ [1, 0, 1]] + AssertEqual g:expected, Tick(g:matrix) + +Execute (bigger matrix): + let g: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]] + let g: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]] + AssertEqual g:expected, Tick(g:matrix) diff --git a/exercises/practice/game-of-life/game_of_life.vim b/exercises/practice/game-of-life/game_of_life.vim new file mode 100644 index 0000000..0be6c25 --- /dev/null +++ b/exercises/practice/game-of-life/game_of_life.vim @@ -0,0 +1,8 @@ +" +" Given a matrix of 0s (dead cells) and 1s (live cells), +" apply the rules of Conway's Game of Life to the matrix, +" and return the matrix after one generation. +" +function! Tick(matrix) abort + " your implementation goes here +endfunction