Skip to content

Commit

Permalink
Add new practice exercise game-of-life (#1481)
Browse files Browse the repository at this point in the history
* improve generator

* add game of life

* remove unused variable

* ignore vimeo link
  • Loading branch information
jiegillet authored Jul 2, 2024
1 parent 1b25f0f commit 587f44e
Show file tree
Hide file tree
Showing 15 changed files with 324 additions and 37 deletions.
1 change: 1 addition & 0 deletions .lycheeignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ twitter.com
regexr.com
papyr.com
reddit.com
vimeo.com/53221562
39 changes: 2 additions & 37 deletions bin/bootstrap_practice_exercise.exs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# Generate all files required for a practice exercise.
# File content is filled as much as possible, but some, including tests, need manual input.
#
# Run the following command from the root of the repo:
# $ elixir bin/boostrap_practice_exercise.exs complex-numbers
# Do not run directly, instead use this script:
# $ bin/generate_practice_exercise.sh complex-numbers
# Pass the name of the exercise (e. g., "complex-numbers") as an argument

Mix.install([
Expand Down Expand Up @@ -143,9 +143,6 @@ module =

## Step 1: create folder structure

Mix.Generator.create_directory("exercises/practice/#{exercise}")
Mix.Generator.create_directory("exercises/practice/#{exercise}/.docs")
Mix.Generator.create_directory("exercises/practice/#{exercise}/.meta")
Mix.Generator.create_directory("exercises/practice/#{exercise}/lib")
Mix.Generator.create_directory("exercises/practice/#{exercise}/test")

Expand Down Expand Up @@ -243,38 +240,6 @@ url =
:inets.start()
:ssl.start()

# .docs/instructions.md
{:ok, {_status, _header, description}} =
:httpc.request(:get, {url ++ ~c"/description.md", []}, [], [])

Mix.Generator.create_file("exercises/practice/#{exercise}/.docs/instructions.md", description)

# .meta/config.json
{:ok, {_status, _header, metadata}} =
:httpc.request(:get, {url ++ ~c"/metadata.toml", []}, [], [])

metadata =
metadata
|> to_string
|> Toml.decode!()

config = %{
authors: [],
contributors: [],
files: %{
solution: ["lib/#{exercise_snake_case}.ex"],
test: ["test/#{exercise_snake_case}_test.exs"],
example: [".meta/example.ex"]
}
}

config =
Map.merge(metadata, config)
|> Jason.encode!(pretty: true)

Mix.Generator.create_file("exercises/practice/#{exercise}/.meta/config.json", config)
IO.puts("Don't forget to add your name and the names of contributors")

# tests and lib files
{:ok, {_status, _header, data}} =
:httpc.request(:get, {url ++ ~c"/canonical-data.json", []}, [], [])
Expand Down
36 changes: 36 additions & 0 deletions bin/generate_practice_exercise.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#!/usr/bin/env bash

# Exit if anything fails.
set -eo pipefail

# If argument not provided, print usage and exit
if [ -z "$1" ]; then
echo "Usage: bin/generate_practice_exercise.sh <exercise-slug>"
exit 1
fi

SLUG="$1"

# build configlet
echo "Fetching latest version of configlet..."
./bin/fetch-configlet

# Preparing config.json
echo "Adding instructions and configuration files..."
UUID=$(bin/configlet uuid)
jq --arg slug "$SLUG" --arg uuid "$UUID" \
'.exercises.practice += [{slug: $slug, name: "TODO", uuid: $uuid, practices: [], prerequisites: [], difficulty: 5}]' \
config.json > config.json.tmp
mv config.json.tmp config.json

# Create instructions and config files
./bin/configlet sync --update --yes --docs --filepaths --metadata --exercise "$SLUG"

# Create Elixir files
echo "Creating Elixir files..."
elixir bin/bootstrap_practice_exercise.exs $SLUG

echo "All stub files were created. After implementing the solution, tests and configuration, please run:"
echo " elixir bin/check_practice_exercise_order.exs --write"
echo " ./bin/configlet sync --update --tests --exercise ${SLUG}"
echo " ./bin/configlet fmt --update --yes --exercise ${SLUG}"
16 changes: 16 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -2313,6 +2313,22 @@
],
"difficulty": 5
},
{
"slug": "game-of-life",
"name": "Game of Life",
"uuid": "32d53ab8-abea-4483-a8cb-ee3b908a9dd7",
"practices": [
"enum"
],
"prerequisites": [
"enum",
"lists",
"pattern-matching",
"pipe-operator",
"multiple-clause-functions"
],
"difficulty": 6
},
{
"slug": "knapsack",
"name": "Knapsack",
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
4 changes: 4 additions & 0 deletions exercises/practice/game-of-life/.formatter.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Used by "mix format"
[
inputs: ["{mix,.formatter}.exs", "{config,lib,test}/**/*.{ex,exs}"]
]
26 changes: 26 additions & 0 deletions exercises/practice/game-of-life/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# The directory Mix will write compiled artifacts to.
/_build/

# If you run "mix test --cover", coverage assets end up here.
/cover/

# The directory Mix downloads your dependencies sources to.
/deps/

# Where third-party dependencies like ExDoc output generated docs.
/doc/

# Ignore .fetch files in case you like to edit your project deps locally.
/.fetch

# If the VM crashes, it generates a dump, let's ignore it too.
erl_crash.dump

# Also ignore archive artifacts (built via "mix archive.build").
*.ez

# Ignore package tarball (built via "mix hex.build").
game_of_life-*.tar

# Temporary files, for example, from tests.
/tmp/
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": [
"jiegillet"
],
"files": {
"solution": [
"lib/game_of_life.ex"
],
"test": [
"test/game_of_life_test.exs"
],
"example": [
".meta/example.ex"
]
},
"blurb": "Implement Conway's Game of Life.",
"source": "Wikipedia",
"source_url": "https://en.wikipedia.org/wiki/Conway%27s_Game_of_Life"
}
44 changes: 44 additions & 0 deletions exercises/practice/game-of-life/.meta/example.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
defmodule GameOfLife do
@doc """
Apply the rules of Conway's Game of Life to a grid of cells
"""

@spec tick(matrix :: list(list(0 | 1))) :: list(list(0 | 1))
def tick([]), do: []

def tick(matrix) do
neighbors = count_neighbors(matrix)

Enum.zip_with(matrix, neighbors, fn row_matrix, row_neighbor ->
Enum.zip_with(row_matrix, row_neighbor, &rule/2)
end)
end

defp count_neighbors(matrix) do
shift_left = shift_left(matrix)
shift_right = shift_right(matrix)

shifts = [
shift_left,
shift_right,
shift_up(matrix),
shift_down(matrix),
shift_left |> shift_up(),
shift_left |> shift_down(),
shift_right |> shift_up(),
shift_right |> shift_down()
]

Enum.zip_with(shifts, fn rows -> Enum.zip_with(rows, &Enum.sum/1) end)
end

defp shift_right(matrix), do: Enum.map(matrix, fn row -> [0 | row] end)
defp shift_left(matrix), do: Enum.map(matrix, fn row -> Enum.drop(row, 1) ++ [0] end)
defp shift_up([first | rest]), do: rest ++ [Enum.map(first, fn _ -> 0 end)]
defp shift_down([first | _] = matrix), do: [Enum.map(first, fn _ -> 0 end) | matrix]

defp rule(1, 2), do: 1
defp rule(1, 3), do: 1
defp rule(0, 3), do: 1
defp rule(_cell, _live_neighbors), do: 0
end
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"
9 changes: 9 additions & 0 deletions exercises/practice/game-of-life/lib/game_of_life.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
defmodule GameOfLife do
@doc """
Apply the rules of Conway's Game of Life to a grid of cells
"""

@spec tick(matrix :: list(list(0 | 1))) :: list(list(0 | 1))
def tick(matrix) do
end
end
28 changes: 28 additions & 0 deletions exercises/practice/game-of-life/mix.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
defmodule GameOfLife.MixProject do
use Mix.Project

def project do
[
app: :game_of_life,
version: "0.1.0",
# elixir: "~> 1.10",
start_permanent: Mix.env() == :prod,
deps: deps()
]
end

# Run "mix help compile.app" to learn about applications.
def application do
[
extra_applications: [:logger]
]
end

# Run "mix help deps" to learn about dependencies.
defp deps do
[
# {:dep_from_hexpm, "~> 0.3.0"},
# {:dep_from_git, git: "https://github.com/elixir-lang/my_dep.git", tag: "0.1.0"}
]
end
end
Loading

0 comments on commit 587f44e

Please sign in to comment.