From 0e28e4af6bd356e90323309c72b6387b12fc51cc Mon Sep 17 00:00:00 2001 From: Eric Willigers Date: Tue, 1 Dec 2026 08:28:44 +1100 Subject: [PATCH] add `game-of-life` exercise --- config.json | 8 + .../game-of-life/.docs/instructions.append.md | 29 + .../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.zig | 42 + .../game-of-life/.meta/supplements.json | 2334 +++++++++++++++++ .../practice/game-of-life/.meta/tests.toml | 35 + .../practice/game-of-life/game_of_life.zig | 8 + .../game-of-life/test_game_of_life.zig | 217 ++ generators/exercises/game_of_life.py | 36 + 11 files changed, 2748 insertions(+) create mode 100644 exercises/practice/game-of-life/.docs/instructions.append.md 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.zig create mode 100644 exercises/practice/game-of-life/.meta/supplements.json create mode 100644 exercises/practice/game-of-life/.meta/tests.toml create mode 100644 exercises/practice/game-of-life/game_of_life.zig create mode 100644 exercises/practice/game-of-life/test_game_of_life.zig create mode 100644 generators/exercises/game_of_life.py diff --git a/config.json b/config.json index 6de8f787..ba324dc4 100644 --- a/config.json +++ b/config.json @@ -1219,6 +1219,14 @@ "prerequisites": [], "difficulty": 8 }, + { + "slug": "game-of-life", + "name": "Conway's Game of Life", + "uuid": "baf57c17-272b-424b-967c-943635b239f8", + "practices": [], + "prerequisites": [], + "difficulty": 8 + }, { "slug": "intergalactic-transmission", "name": "Intergalactic Transmission", diff --git a/exercises/practice/game-of-life/.docs/instructions.append.md b/exercises/practice/game-of-life/.docs/instructions.append.md new file mode 100644 index 00000000..a3e6e1bc --- /dev/null +++ b/exercises/practice/game-of-life/.docs/instructions.append.md @@ -0,0 +1,29 @@ +# Instructions append + +## Input/Output format + +Each row of the grid is represented as an unsigned integer. +The comptime parameter `T` is the row type: the number of bits in `T` is the number of columns. + +For example, a grid with 3 columns uses `u3`, so + +```text + 0 1 0 + 1 0 0 + 1 1 0 +``` + +is represented as + +```zig +const matrix = [_]u3{ + 0b010, + 0b100, + 0b110, +}; +``` + +The grid has between 1 and 128 columns, so `T` ranges from `u1` to `u128`. + +Use the supplied allocator to allocate the next generation. +The caller owns the returned slice and will free it. 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..10be7d3e --- /dev/null +++ b/exercises/practice/game-of-life/.meta/config.json @@ -0,0 +1,19 @@ +{ + "authors": [ + "keiravillekode" + ], + "files": { + "solution": [ + "game_of_life.zig" + ], + "test": [ + "test_game_of_life.zig" + ], + "example": [ + ".meta/example.zig" + ] + }, + "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.zig b/exercises/practice/game-of-life/.meta/example.zig new file mode 100644 index 00000000..ff414b12 --- /dev/null +++ b/exercises/practice/game-of-life/.meta/example.zig @@ -0,0 +1,42 @@ +const std = @import("std"); +const mem = std.mem; + +pub fn tick(comptime T: type, allocator: mem.Allocator, matrix: []const T) mem.Allocator.Error![]T { + const column_count = @bitSizeOf(T); + comptime std.debug.assert(@typeInfo(T).int.signedness == .unsigned and 1 <= column_count and column_count <= 128); + + const row_count: usize = matrix.len; + const result = try allocator.alloc(T, row_count); + for (0..row_count) |i| { + var above_row: T = if (i == 0) 0 else matrix[i - 1]; + var row: T = matrix[i]; + var below_row: T = if (i + 1 == row_count) 0 else matrix[i + 1]; + + var new_row: u128 = 0; + + var right_column: u128 = 0; + var cell: u128 = 0; + var column: u128 = 0; + var left_cell: u128 = row & 1; + var left_column: u128 = (above_row & 1) + left_cell + (below_row & 1); + + for (0..column_count) |_| { + above_row = std.math.shr(T, above_row, 1); + row = std.math.shr(T, row, 1); + below_row = std.math.shr(T, below_row, 1); + + right_column = column; + cell = left_cell; + column = left_column; + left_cell = row & 1; + left_column = (above_row & 1) + left_cell + (below_row & 1); + + const alive = ((right_column + column + left_column - cell) | cell) == 3; + new_row = std.math.rotr(u128, new_row | @intFromBool(alive), 1); + } + + result[i] = @truncate(std.math.rotl(u128, new_row, column_count)); + } + + return result; +} diff --git a/exercises/practice/game-of-life/.meta/supplements.json b/exercises/practice/game-of-life/.meta/supplements.json new file mode 100644 index 00000000..e43aafe6 --- /dev/null +++ b/exercises/practice/game-of-life/.meta/supplements.json @@ -0,0 +1,2334 @@ +{ + "cases": [ + { + "description": "matrix with 32 columns", + "property": "tick", + "input": { + "matrix": [ + [ + 1, + 1, + 1, + 0, + 1, + 1, + 0, + 0, + 0, + 1, + 1, + 0, + 1, + 1, + 1, + 0, + 1, + 1, + 1, + 1, + 1, + 0, + 1, + 1, + 0, + 1, + 0, + 0, + 1, + 0, + 0, + 0 + ], + [ + 1, + 0, + 1, + 1, + 1, + 1, + 1, + 0, + 1, + 0, + 1, + 1, + 0, + 0, + 1, + 0, + 0, + 0, + 1, + 1, + 1, + 0, + 0, + 0, + 1, + 0, + 0, + 1, + 1, + 0, + 0, + 0 + ], + [ + 1, + 1, + 1, + 0, + 1, + 1, + 0, + 1, + 0, + 0, + 0, + 0, + 0, + 1, + 1, + 0, + 1, + 0, + 1, + 1, + 1, + 1, + 1, + 0, + 1, + 0, + 1, + 1, + 0, + 1, + 1, + 0 + ], + [ + 1, + 0, + 0, + 1, + 0, + 0, + 0, + 1, + 0, + 0, + 1, + 0, + 0, + 0, + 0, + 0, + 0, + 1, + 0, + 1, + 1, + 0, + 1, + 0, + 1, + 0, + 0, + 1, + 0, + 1, + 1, + 0 + ], + [ + 1, + 0, + 0, + 1, + 0, + 0, + 1, + 1, + 0, + 1, + 1, + 1, + 0, + 0, + 0, + 1, + 0, + 0, + 0, + 0, + 1, + 1, + 0, + 0, + 0, + 0, + 1, + 0, + 1, + 1, + 0, + 0 + ] + ] + }, + "expected": [ + [ + 1, + 0, + 1, + 0, + 0, + 0, + 1, + 0, + 0, + 1, + 1, + 0, + 1, + 1, + 1, + 1, + 0, + 1, + 0, + 0, + 1, + 1, + 0, + 1, + 1, + 0, + 0, + 1, + 1, + 0, + 0, + 0 + ], + [ + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 1, + 1, + 0, + 1, + 1, + 0, + 0, + 0, + 0, + 1, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 1, + 0, + 0, + 0, + 0, + 0, + 0, + 0 + ], + [ + 1, + 0, + 0, + 0, + 0, + 0, + 0, + 1, + 1, + 1, + 1, + 1, + 0, + 1, + 1, + 1, + 0, + 0, + 0, + 0, + 0, + 0, + 1, + 0, + 1, + 0, + 1, + 0, + 0, + 0, + 1, + 0 + ], + [ + 1, + 0, + 0, + 1, + 0, + 1, + 0, + 1, + 0, + 1, + 1, + 1, + 0, + 0, + 1, + 1, + 1, + 1, + 0, + 0, + 0, + 0, + 1, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0 + ], + [ + 0, + 0, + 0, + 0, + 0, + 0, + 1, + 1, + 1, + 1, + 1, + 1, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 1, + 1, + 1, + 0, + 0, + 0, + 0, + 0, + 1, + 1, + 1, + 1, + 0 + ] + ] + }, + { + "description": "matrix with 64 columns", + "property": "tick", + "input": { + "matrix": [ + [ + 1, + 1, + 1, + 0, + 0, + 0, + 0, + 1, + 1, + 0, + 1, + 0, + 1, + 0, + 0, + 1, + 0, + 1, + 0, + 0, + 0, + 1, + 0, + 1, + 0, + 0, + 1, + 0, + 1, + 1, + 1, + 1, + 1, + 0, + 0, + 1, + 0, + 0, + 0, + 0, + 0, + 1, + 1, + 1, + 0, + 0, + 1, + 0, + 1, + 1, + 0, + 1, + 0, + 1, + 1, + 1, + 0, + 1, + 1, + 1, + 1, + 0, + 1, + 0 + ], + [ + 0, + 0, + 0, + 0, + 0, + 0, + 1, + 0, + 0, + 1, + 0, + 1, + 0, + 0, + 0, + 1, + 0, + 1, + 0, + 1, + 0, + 0, + 0, + 0, + 1, + 1, + 0, + 1, + 0, + 1, + 1, + 1, + 1, + 1, + 0, + 1, + 0, + 1, + 0, + 1, + 0, + 0, + 1, + 1, + 0, + 0, + 1, + 1, + 1, + 1, + 1, + 1, + 0, + 0, + 1, + 0, + 0, + 0, + 1, + 0, + 1, + 1, + 0, + 0 + ], + [ + 1, + 0, + 0, + 1, + 1, + 1, + 0, + 0, + 0, + 0, + 1, + 0, + 0, + 0, + 0, + 0, + 1, + 1, + 1, + 1, + 1, + 1, + 0, + 1, + 1, + 1, + 0, + 0, + 1, + 0, + 1, + 1, + 0, + 0, + 0, + 0, + 1, + 1, + 1, + 1, + 1, + 0, + 1, + 0, + 1, + 1, + 0, + 1, + 1, + 1, + 0, + 0, + 0, + 0, + 1, + 0, + 0, + 0, + 0, + 1, + 0, + 0, + 1, + 0 + ], + [ + 0, + 1, + 0, + 1, + 0, + 1, + 0, + 1, + 1, + 0, + 0, + 1, + 0, + 1, + 0, + 0, + 0, + 0, + 0, + 1, + 1, + 1, + 0, + 0, + 0, + 0, + 1, + 1, + 1, + 1, + 1, + 1, + 0, + 1, + 0, + 1, + 0, + 1, + 0, + 0, + 1, + 0, + 0, + 1, + 1, + 0, + 0, + 1, + 0, + 0, + 1, + 1, + 0, + 1, + 1, + 0, + 0, + 0, + 0, + 1, + 0, + 0, + 0, + 0 + ], + [ + 0, + 0, + 1, + 1, + 1, + 1, + 0, + 1, + 1, + 1, + 0, + 1, + 1, + 1, + 0, + 1, + 1, + 0, + 0, + 1, + 1, + 1, + 1, + 1, + 0, + 0, + 0, + 1, + 0, + 1, + 1, + 1, + 1, + 1, + 0, + 1, + 0, + 0, + 1, + 0, + 0, + 1, + 1, + 0, + 0, + 1, + 0, + 1, + 0, + 0, + 0, + 0, + 1, + 0, + 0, + 0, + 0, + 1, + 1, + 1, + 1, + 0, + 1, + 0 + ] + ] + }, + "expected": [ + [ + 0, + 1, + 0, + 0, + 0, + 0, + 0, + 1, + 1, + 1, + 1, + 1, + 0, + 0, + 0, + 0, + 0, + 0, + 1, + 0, + 0, + 0, + 0, + 0, + 1, + 1, + 1, + 1, + 1, + 0, + 0, + 0, + 0, + 1, + 1, + 0, + 1, + 0, + 0, + 0, + 0, + 1, + 0, + 1, + 0, + 0, + 1, + 0, + 0, + 0, + 0, + 1, + 1, + 1, + 1, + 1, + 0, + 1, + 1, + 0, + 1, + 0, + 0, + 0 + ], + [ + 1, + 0, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 0, + 1, + 0, + 0, + 0, + 1, + 0, + 0, + 0, + 1, + 0, + 1, + 0, + 0, + 0, + 0, + 0, + 1, + 0, + 0, + 0, + 0, + 0, + 1, + 1, + 1, + 0, + 1, + 0, + 1, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 1, + 1, + 0, + 0, + 0, + 0, + 1, + 0, + 0, + 0, + 0, + 1, + 0 + ], + [ + 0, + 0, + 1, + 1, + 0, + 1, + 0, + 1, + 1, + 1, + 1, + 1, + 1, + 0, + 0, + 0, + 1, + 1, + 0, + 0, + 0, + 1, + 1, + 1, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 1, + 0, + 1, + 0, + 0, + 0, + 0, + 1, + 0, + 1, + 0, + 0, + 1, + 0, + 0, + 0, + 0, + 0, + 0, + 1, + 0, + 1, + 1, + 0, + 0, + 1, + 1, + 0, + 1, + 0, + 0 + ], + [ + 0, + 1, + 0, + 0, + 0, + 0, + 0, + 1, + 0, + 0, + 0, + 1, + 0, + 1, + 1, + 1, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 1, + 1, + 0, + 0, + 0, + 0, + 0, + 0, + 1, + 0, + 1, + 0, + 0, + 0, + 0, + 1, + 0, + 0, + 0, + 0, + 0, + 0, + 1, + 0, + 1, + 1, + 1, + 1, + 1, + 1, + 0, + 0, + 0, + 0, + 0, + 0, + 1, + 0, + 0 + ], + [ + 0, + 0, + 1, + 1, + 0, + 1, + 0, + 1, + 0, + 1, + 1, + 1, + 0, + 1, + 1, + 0, + 0, + 0, + 0, + 1, + 0, + 0, + 1, + 0, + 0, + 0, + 1, + 1, + 0, + 0, + 0, + 0, + 0, + 1, + 0, + 0, + 1, + 0, + 0, + 0, + 0, + 1, + 1, + 1, + 1, + 0, + 1, + 0, + 0, + 0, + 0, + 1, + 1, + 1, + 0, + 0, + 0, + 0, + 1, + 1, + 1, + 0, + 0, + 0 + ] + ] + }, + { + "description": "matrix with 128 columns", + "property": "tick", + "input": { + "matrix": [ + [ + 1, + 0, + 1, + 0, + 1, + 0, + 0, + 1, + 0, + 0, + 1, + 0, + 0, + 1, + 0, + 1, + 1, + 0, + 0, + 1, + 1, + 0, + 0, + 1, + 0, + 0, + 1, + 0, + 0, + 0, + 0, + 0, + 0, + 1, + 0, + 0, + 1, + 1, + 0, + 1, + 1, + 1, + 1, + 0, + 1, + 0, + 1, + 0, + 0, + 0, + 1, + 0, + 0, + 1, + 0, + 1, + 1, + 1, + 0, + 0, + 1, + 0, + 0, + 1, + 1, + 1, + 0, + 1, + 1, + 0, + 1, + 0, + 1, + 0, + 0, + 0, + 1, + 0, + 1, + 1, + 1, + 0, + 1, + 0, + 0, + 1, + 0, + 0, + 1, + 1, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 1, + 0, + 0, + 1, + 1, + 0, + 0, + 0, + 1, + 0, + 1, + 1, + 1, + 0, + 1, + 0, + 0, + 1, + 1, + 1, + 1, + 0, + 0, + 0, + 1, + 0, + 0, + 1, + 0, + 1, + 0 + ], + [ + 0, + 1, + 0, + 0, + 1, + 1, + 1, + 1, + 1, + 0, + 1, + 1, + 0, + 0, + 1, + 1, + 0, + 0, + 0, + 1, + 1, + 0, + 0, + 0, + 1, + 1, + 0, + 1, + 0, + 1, + 1, + 0, + 0, + 1, + 0, + 0, + 1, + 1, + 1, + 0, + 0, + 1, + 0, + 0, + 0, + 0, + 0, + 1, + 0, + 1, + 1, + 0, + 0, + 0, + 0, + 0, + 1, + 0, + 0, + 0, + 1, + 0, + 1, + 0, + 0, + 1, + 0, + 1, + 0, + 0, + 0, + 1, + 0, + 0, + 0, + 0, + 1, + 1, + 0, + 0, + 1, + 0, + 1, + 1, + 0, + 0, + 0, + 0, + 1, + 0, + 0, + 0, + 0, + 1, + 1, + 0, + 1, + 0, + 0, + 1, + 0, + 1, + 1, + 0, + 1, + 0, + 1, + 0, + 0, + 0, + 0, + 1, + 0, + 0, + 1, + 0, + 0, + 0, + 0, + 0, + 0, + 1, + 0, + 1, + 0, + 0, + 1, + 1 + ], + [ + 1, + 0, + 0, + 1, + 1, + 0, + 1, + 0, + 1, + 0, + 1, + 0, + 0, + 0, + 0, + 1, + 0, + 1, + 0, + 0, + 1, + 0, + 0, + 1, + 0, + 0, + 0, + 0, + 1, + 0, + 1, + 0, + 1, + 0, + 0, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 0, + 1, + 0, + 1, + 1, + 0, + 1, + 0, + 0, + 1, + 0, + 0, + 0, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 0, + 1, + 0, + 1, + 0, + 1, + 0, + 0, + 0, + 0, + 0, + 1, + 0, + 0, + 1, + 1, + 1, + 0, + 1, + 0, + 0, + 1, + 1, + 1, + 1, + 0, + 1, + 1, + 1, + 0, + 1, + 0, + 0, + 1, + 1, + 1, + 1, + 0, + 1, + 0, + 0, + 1, + 1, + 1, + 1, + 1, + 0, + 1, + 0, + 1, + 0, + 0, + 1, + 1, + 0, + 0, + 1, + 1, + 0, + 0, + 1, + 1, + 1, + 1, + 0, + 1 + ], + [ + 0, + 0, + 1, + 1, + 0, + 0, + 0, + 0, + 0, + 1, + 0, + 0, + 0, + 0, + 0, + 1, + 0, + 1, + 1, + 0, + 0, + 1, + 1, + 1, + 0, + 0, + 0, + 1, + 0, + 0, + 1, + 1, + 1, + 1, + 0, + 0, + 1, + 0, + 1, + 1, + 1, + 0, + 1, + 0, + 0, + 0, + 0, + 1, + 1, + 0, + 1, + 1, + 1, + 0, + 1, + 0, + 0, + 0, + 0, + 0, + 0, + 1, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 1, + 1, + 1, + 0, + 1, + 1, + 0, + 1, + 0, + 1, + 0, + 0, + 0, + 0, + 1, + 1, + 0, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 0, + 1, + 0, + 1, + 0, + 1, + 0, + 1, + 0, + 1, + 1, + 1, + 1, + 0, + 0, + 1, + 0, + 1, + 1, + 0, + 0, + 1, + 1, + 1, + 0, + 1, + 0, + 0, + 0, + 1, + 1, + 0, + 1, + 0 + ], + [ + 0, + 0, + 0, + 1, + 0, + 0, + 1, + 0, + 0, + 0, + 1, + 1, + 1, + 1, + 0, + 1, + 1, + 0, + 1, + 0, + 1, + 0, + 0, + 0, + 1, + 0, + 1, + 1, + 0, + 0, + 1, + 1, + 1, + 1, + 0, + 1, + 0, + 1, + 1, + 1, + 0, + 1, + 0, + 0, + 1, + 1, + 1, + 1, + 0, + 0, + 0, + 1, + 1, + 1, + 1, + 1, + 0, + 0, + 1, + 0, + 1, + 0, + 1, + 0, + 0, + 1, + 1, + 1, + 1, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 1, + 0, + 0, + 0, + 0, + 1, + 0, + 1, + 0, + 0, + 0, + 1, + 0, + 0, + 1, + 1, + 1, + 0, + 0, + 1, + 0, + 0, + 0, + 1, + 1, + 1, + 0, + 1, + 0, + 0, + 0, + 0, + 0, + 1, + 1, + 0, + 1, + 1, + 0, + 0, + 1, + 0, + 1, + 1, + 1, + 0, + 1 + ] + ] + }, + "expected": [ + [ + 0, + 1, + 0, + 1, + 1, + 0, + 0, + 1, + 1, + 1, + 1, + 1, + 0, + 0, + 0, + 1, + 1, + 0, + 0, + 1, + 1, + 0, + 0, + 0, + 1, + 1, + 1, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 1, + 0, + 0, + 1, + 1, + 1, + 1, + 0, + 0, + 0, + 0, + 0, + 0, + 1, + 1, + 0, + 0, + 0, + 0, + 1, + 1, + 1, + 0, + 0, + 0, + 1, + 0, + 1, + 1, + 1, + 0, + 1, + 1, + 0, + 0, + 1, + 0, + 0, + 0, + 0, + 1, + 0, + 1, + 1, + 1, + 0, + 1, + 1, + 0, + 0, + 0, + 0, + 1, + 1, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 1, + 1, + 1, + 0, + 0, + 1, + 1, + 1, + 1, + 0, + 1, + 0, + 0, + 0, + 1, + 1, + 1, + 0, + 0, + 0, + 0, + 0, + 1, + 0, + 0, + 1, + 1, + 1 + ], + [ + 1, + 1, + 1, + 0, + 0, + 0, + 0, + 0, + 1, + 0, + 1, + 1, + 0, + 0, + 0, + 0, + 0, + 0, + 1, + 0, + 0, + 1, + 0, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 1, + 1, + 0, + 0, + 1, + 1, + 1, + 1, + 1, + 0, + 0, + 1, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 1, + 0, + 0, + 1, + 0, + 1, + 0, + 0, + 0, + 1, + 1, + 0, + 0, + 1, + 0, + 0, + 0, + 0, + 1, + 0, + 1, + 0, + 0, + 0, + 1, + 0, + 0, + 0, + 1, + 0, + 0, + 1, + 1, + 0, + 0, + 0, + 0, + 1, + 0, + 0, + 1, + 0, + 0, + 0, + 0, + 0, + 0, + 1, + 0, + 1, + 1, + 1, + 0, + 0, + 0, + 1, + 1, + 0, + 1, + 1, + 0, + 0, + 0, + 0, + 0, + 1 + ], + [ + 0, + 1, + 0, + 0, + 0, + 0, + 1, + 0, + 1, + 0, + 1, + 1, + 0, + 0, + 0, + 1, + 0, + 1, + 0, + 0, + 1, + 0, + 0, + 1, + 0, + 0, + 1, + 1, + 1, + 0, + 0, + 0, + 0, + 0, + 1, + 1, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 1, + 0, + 0, + 0, + 0, + 0, + 1, + 0, + 0, + 1, + 1, + 1, + 1, + 1, + 0, + 0, + 0, + 0, + 1, + 0, + 0, + 0, + 1, + 0, + 0, + 1, + 0, + 1, + 1, + 0, + 0, + 0, + 1, + 0, + 0, + 1, + 0, + 0, + 0, + 0, + 1, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 1, + 1, + 0, + 0, + 1, + 0, + 1, + 1, + 0, + 0, + 1, + 1, + 1, + 1, + 0, + 0, + 0, + 0, + 0, + 0, + 1 + ], + [ + 0, + 0, + 1, + 0, + 0, + 1, + 0, + 1, + 0, + 1, + 0, + 0, + 1, + 0, + 0, + 1, + 0, + 0, + 1, + 0, + 1, + 1, + 1, + 1, + 1, + 0, + 1, + 1, + 1, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 1, + 1, + 0, + 0, + 0, + 0, + 1, + 1, + 1, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 1, + 1, + 0, + 0, + 0, + 1, + 1, + 0, + 1, + 1, + 1, + 1, + 1, + 0, + 1, + 1, + 0, + 0, + 1, + 1, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 1, + 0, + 0, + 0, + 0, + 0, + 1, + 0, + 1, + 1, + 0, + 0, + 0, + 0, + 0, + 0, + 1, + 0, + 1, + 1, + 1, + 0, + 0, + 0, + 0, + 0, + 1, + 1, + 0, + 0, + 0, + 0, + 0, + 0, + 1 + ], + [ + 0, + 0, + 1, + 1, + 0, + 0, + 0, + 0, + 0, + 0, + 1, + 1, + 1, + 0, + 1, + 1, + 1, + 0, + 1, + 1, + 0, + 1, + 1, + 1, + 0, + 0, + 1, + 1, + 0, + 0, + 1, + 0, + 0, + 1, + 1, + 0, + 1, + 1, + 0, + 0, + 0, + 1, + 0, + 0, + 0, + 1, + 1, + 1, + 1, + 0, + 1, + 0, + 0, + 0, + 1, + 1, + 0, + 0, + 0, + 0, + 0, + 1, + 0, + 0, + 0, + 0, + 1, + 1, + 1, + 1, + 1, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 1, + 1, + 0, + 1, + 0, + 1, + 0, + 1, + 1, + 1, + 1, + 0, + 1, + 1, + 0, + 1, + 0, + 0, + 1, + 0, + 0, + 1, + 1, + 0, + 0, + 0, + 0, + 0, + 0, + 1, + 1, + 0, + 1, + 1, + 0, + 0, + 0, + 1, + 1, + 0, + 1, + 1, + 0 + ] + ] + } + ] +} 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..4386fd50 --- /dev/null +++ b/exercises/practice/game-of-life/.meta/tests.toml @@ -0,0 +1,35 @@ +# 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" +include = false + +[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.zig b/exercises/practice/game-of-life/game_of_life.zig new file mode 100644 index 00000000..a95d4221 --- /dev/null +++ b/exercises/practice/game-of-life/game_of_life.zig @@ -0,0 +1,8 @@ +const std = @import("std"); +const mem = std.mem; + +pub fn tick(comptime T: type, allocator: mem.Allocator, matrix: []const T) mem.Allocator.Error![]T { + _ = allocator; + _ = matrix; + @compileError("please implement the tick function"); +} diff --git a/exercises/practice/game-of-life/test_game_of_life.zig b/exercises/practice/game-of-life/test_game_of_life.zig new file mode 100644 index 00000000..e321b087 --- /dev/null +++ b/exercises/practice/game-of-life/test_game_of_life.zig @@ -0,0 +1,217 @@ +const std = @import("std"); +const mem = std.mem; +const testing = std.testing; + +const game_of_life = @import("game_of_life.zig"); + +fn testTick(comptime T: type) fn (mem.Allocator, []const T, []const T) anyerror!void { + return struct { + fn run(allocator: mem.Allocator, expected: []const T, matrix: []const T) anyerror!void { + const actual = try game_of_life.tick(T, allocator, matrix); + defer allocator.free(actual); + try testing.expectEqualSlices(T, expected, actual); + } + }.run; +} + +test "live cells with zero live neighbors die" { + const matrix = [_]u3{ + 0b000, + 0b010, + 0b000, + }; + const expected = [_]u3{ + 0b000, + 0b000, + 0b000, + }; + try testing.checkAllAllocationFailures( + testing.allocator, + testTick(u3), + .{ &expected, &matrix }, + ); +} + +test "live cells with only one live neighbor die" { + const matrix = [_]u3{ + 0b000, + 0b010, + 0b010, + }; + const expected = [_]u3{ + 0b000, + 0b000, + 0b000, + }; + try testing.checkAllAllocationFailures( + testing.allocator, + testTick(u3), + .{ &expected, &matrix }, + ); +} + +test "live cells with two live neighbors stay alive" { + const matrix = [_]u3{ + 0b101, + 0b101, + 0b101, + }; + const expected = [_]u3{ + 0b000, + 0b101, + 0b000, + }; + try testing.checkAllAllocationFailures( + testing.allocator, + testTick(u3), + .{ &expected, &matrix }, + ); +} + +test "live cells with three live neighbors stay alive" { + const matrix = [_]u3{ + 0b010, + 0b100, + 0b110, + }; + const expected = [_]u3{ + 0b000, + 0b100, + 0b110, + }; + try testing.checkAllAllocationFailures( + testing.allocator, + testTick(u3), + .{ &expected, &matrix }, + ); +} + +test "dead cells with three live neighbors become alive" { + const matrix = [_]u3{ + 0b110, + 0b000, + 0b100, + }; + const expected = [_]u3{ + 0b000, + 0b110, + 0b000, + }; + try testing.checkAllAllocationFailures( + testing.allocator, + testTick(u3), + .{ &expected, &matrix }, + ); +} + +test "live cells with four or more neighbors die" { + const matrix = [_]u3{ + 0b111, + 0b111, + 0b111, + }; + const expected = [_]u3{ + 0b101, + 0b000, + 0b101, + }; + try testing.checkAllAllocationFailures( + testing.allocator, + testTick(u3), + .{ &expected, &matrix }, + ); +} + +test "bigger matrix" { + const matrix = [_]u8{ + 0b11011000, + 0b10110000, + 0b11100111, + 0b00000110, + 0b10001100, + 0b11000111, + 0b00101001, + 0b10000011, + }; + const expected = [_]u8{ + 0b11011000, + 0b00000110, + 0b10111101, + 0b10000001, + 0b11001001, + 0b11010001, + 0b10000000, + 0b00000011, + }; + try testing.checkAllAllocationFailures( + testing.allocator, + testTick(u8), + .{ &expected, &matrix }, + ); +} + +test "matrix with 32 columns" { + const matrix = [_]u32{ + 0b11101100011011101111101101001000, + 0b10111110101100100011100010011000, + 0b11101101000001101011111010110110, + 0b10010001001000000101101010010110, + 0b10010011011100010000110000101100, + }; + const expected = [_]u32{ + 0b10100010011011110100110110011000, + 0b00000001101100001000000010000000, + 0b10000001111101110000001010100010, + 0b10010101011100111100001000000000, + 0b00000011111100000001110000011110, + }; + try testing.checkAllAllocationFailures( + testing.allocator, + testTick(u32), + .{ &expected, &matrix }, + ); +} + +test "matrix with 64 columns" { + const matrix = [_]u64{ + 0b1110000110101001010001010010111110010000011100101101011101111010, + 0b0000001001010001010100001101011111010101001100111111001000101100, + 0b1001110000100000111111011100101100001111101011011100001000010010, + 0b0101010110010100000111000011111101010100100110010011011000010000, + 0b0011110111011101100111110001011111010010011001010000100001111010, + }; + const expected = [_]u64{ + 0b0100000111110000001000001111100001101000010100100001111101101000, + 0b1011111111010001000101000001000001110101000000000001100001000010, + 0b0011010111111000110001110000000001010000101001000000101100110100, + 0b0100000100010111000000000110000001010000100000010111111000000100, + 0b0011010101110110000100100011000001001000011110100001110000111000, + }; + try testing.checkAllAllocationFailures( + testing.allocator, + testTick(u64), + .{ &expected, &matrix }, + ); +} + +test "matrix with 128 columns" { + const matrix = [_]u128{ + 0b10101001001001011001100100100000010011011110101000100101110010011101101010001011101001001100000001001100010111010011110001001010, + 0b01001111101100110001100011010110010011100100000101100000100010100101000100001100101100001000011010010110101000010010000001010011, + 0b10011010101000010100100100001010100111111101011010010001111111010101000001001110100111101110100111101001111101010011001100111101, + 0b00110000010000010110011100010011110010111010000110111010000001000000011101101010000110111111110101010101111001011001110100011010, + 0b00010010001111011010100010110011110101110100111100011111001010100111100000000000010000101000100111001000111010000011011001011101, + }; + const expected = [_]u128{ + 0b01011001111100011001100011100000000010011110000001100001110001011101100100001011101100001100000000001110011110100011100000100111, + 0b11100000101100000010010111111111111000000001100111110010000000100101000110010000101000100010011000010010000001011100011011000001, + 0b01000010101100010100100100111000001100000000001000001001111100001000100101100010010000100000000000000000000110010110011110000001, + 0b00100101010010010010111110111000000000000011000011100000000000001100011011111011001100000000010000010110000001011100000110000001, + 0b00110000001110111011011100110010011011000100011110100011000001000011111000000000000001101010111101101001001100000011011000110110, + }; + try testing.checkAllAllocationFailures( + testing.allocator, + testTick(u128), + .{ &expected, &matrix }, + ); +} diff --git a/generators/exercises/game_of_life.py b/generators/exercises/game_of_life.py new file mode 100644 index 00000000..a0c2f502 --- /dev/null +++ b/generators/exercises/game_of_life.py @@ -0,0 +1,36 @@ +USE_MEM = True + +HEADER = """ +fn testTick(comptime T: type) fn (mem.Allocator, []const T, []const T) anyerror!void { + return struct { + fn run(allocator: mem.Allocator, expected: []const T, matrix: []const T) anyerror!void { + const actual = try game_of_life.tick(T, allocator, matrix); + defer allocator.free(actual); + try testing.expectEqualSlices(T, expected, actual); + } + }.run; +} +""" + + +def encode(row): + """A row of 0/1 cells as a binary literal, one digit per column (leftmost column first).""" + return "0b" + "".join(map(str, row)) + + +def rows_literal(row_type, matrix): + return f"[_]{row_type}{{\n" + "".join(f" {encode(row)},\n" for row in matrix) + "}" + + +def gen_case(case): + matrix = case["input"]["matrix"] + row_type = f"u{len(matrix[0])}" + return ( + f" const matrix = {rows_literal(row_type, matrix)};\n" + f" const expected = {rows_literal(row_type, case['expected'])};\n" + " try testing.checkAllAllocationFailures(\n" + " testing.allocator,\n" + f" testTick({row_type}),\n" + " .{ &expected, &matrix },\n" + " );\n" + )