Skip to content
Open
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 @@ -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",
Expand Down
29 changes: 29 additions & 0 deletions exercises/practice/game-of-life/.docs/instructions.append.md
Original file line number Diff line number Diff line change
@@ -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.
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": [
"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"
}
42 changes: 42 additions & 0 deletions exercises/practice/game-of-life/.meta/example.zig
Original file line number Diff line number Diff line change
@@ -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;
}
Loading
Loading