Skip to content
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 @@ -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",
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": [
"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"
}
37 changes: 37 additions & 0 deletions exercises/practice/game-of-life/.meta/example.vim
Original file line number Diff line number Diff line change
@@ -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
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"
91 changes: 91 additions & 0 deletions exercises/practice/game-of-life/game_of_life.vader
Original file line number Diff line number Diff line change
@@ -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)
8 changes: 8 additions & 0 deletions exercises/practice/game-of-life/game_of_life.vim
Original file line number Diff line number Diff line change
@@ -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