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
15 changes: 12 additions & 3 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -2671,9 +2671,9 @@
"difficulty": 7
},
{
"slug": "minesweeper",
"name": "Minesweeper",
"uuid": "8d665135-31dc-490a-8f6d-51c6654099bd",
"slug": "flower-field",
"name": "Flower Field",
"uuid": "65b9ba19-0d82-4cde-b909-6a8440b24fd6",
"practices": [
"enum"
],
Expand All @@ -2693,6 +2693,15 @@
],
"difficulty": 7
},
{
"slug": "minesweeper",
"name": "Minesweeper",
"uuid": "8d665135-31dc-490a-8f6d-51c6654099bd",
"practices": [],
"prerequisites": [],
"difficulty": 7,
"status": "deprecated"
},
{
"slug": "piecing-it-together",
"name": "Piecing It Together",
Expand Down
26 changes: 26 additions & 0 deletions exercises/practice/flower-field/.docs/instructions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Instructions

Your task is to add flower counts to empty squares in a completed Flower Field garden.
The garden itself is a rectangle board composed of squares that are either empty (`' '`) or a flower (`'*'`).

For each empty square, count the number of flowers adjacent to it (horizontally, vertically, diagonally).
If the empty square has no adjacent flowers, leave it empty.
Otherwise replace it with the count of adjacent flowers.

For example, you may receive a 5 x 4 board like this (empty spaces are represented here with the '·' character for display on screen):

```text
·*·*·
··*··
··*··
·····
```

Which your code should transform into this:

```text
1*3*1
13*31
·2*2·
·111·
```
7 changes: 7 additions & 0 deletions exercises/practice/flower-field/.docs/introduction.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Introduction

[Flower Field][history] is a compassionate reimagining of the popular game Minesweeper.
The object of the game is to find all the flowers in the garden using numeric hints that indicate how many flowers are directly adjacent (horizontally, vertically, diagonally) to a square.
"Flower Field" shipped in regional versions of Microsoft Windows in Italy, Germany, South Korea, Japan and Taiwan.

[history]: https://web.archive.org/web/20020409051321fw_/http://rcm.usr.dsi.unimi.it/rcmweb/fnm/
4 changes: 4 additions & 0 deletions exercises/practice/flower-field/.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}"]
]
35 changes: 35 additions & 0 deletions exercises/practice/flower-field/.meta/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
{
"authors": [],
"contributors": [
"andrewsardone",
"angelikatyborska",
"Cohen-Carlisle",
"dalexj",
"devonestes",
"jinyeow",
"kahgoh",
"kytrinyx",
"lpil",
"neenjaw",
"parkerl",
"petehuang",
"pminten",
"rubysolo",
"sotojuan",
"Teapane",
"tjcelaya",
"waiting-for-dev"
],
"files": {
"solution": [
"lib/flower_field.ex"
],
"test": [
"test/flower_field_test.exs"
],
"example": [
".meta/example.ex"
]
},
"blurb": "Mark all the flowers in a garden."
}
57 changes: 57 additions & 0 deletions exercises/practice/flower-field/.meta/example.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
defmodule FlowerField do
@doc """
Annotate empty spots next to flowers with the number of flowers next to them.
"""
@spec annotate([String.t()]) :: [String.t()]
def annotate([]), do: []

def annotate(board) do
h = length(board)
# Only 7-bit ASCII in the board, so this is safe
w = String.length(hd(board))

annotations =
Enum.reduce(Stream.with_index(board), %{}, fn {line, y}, acc ->
Enum.reduce(Stream.with_index(String.to_charlist(line)), acc, fn
{?*, x}, acc -> add_adjacents(acc, {x, y}, {w, h})
_, acc -> acc
end)
end)

Enum.map(Stream.with_index(board), fn {line, y} ->
Enum.map(Stream.with_index(String.to_charlist(line)), fn
# Don't replace flowers
{?*, _} ->
?*

{_, x} ->
case annotations[{x, y}] do
nil -> ?\s
n -> ?0 + n
end
end)
|> to_string
end)
end

@adjacent_vecs for x <- [-1, 0, 1],
y <- [-1, 0, 1],
x != 0 or y != 0,
do: {x, y}

defp add_adjacents(d, c, bounds) do
Enum.reduce(@adjacent_vecs, d, fn v, acc ->
c1 = add_vec(c, v)

if valid?(c1, bounds) do
Map.update(acc, c1, 1, &(&1 + 1))
else
acc
end
end)
end

defp add_vec({cx, cy}, {vx, vy}), do: {cx + vx, cy + vy}

defp valid?({cx, cy}, {w, h}), do: cx >= 0 and cx < w and cy >= 0 and cy < h
end
46 changes: 46 additions & 0 deletions exercises/practice/flower-field/.meta/tests.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# 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.

[237ff487-467a-47e1-9b01-8a891844f86c]
description = "no rows"

[4b4134ec-e20f-439c-a295-664c38950ba1]
description = "no columns"

[d774d054-bbad-4867-88ae-069cbd1c4f92]
description = "no flowers"

[225176a0-725e-43cd-aa13-9dced501f16e]
description = "garden full of flowers"

[3f345495-f1a5-4132-8411-74bd7ca08c49]
description = "flower surrounded by spaces"

[6cb04070-4199-4ef7-a6fa-92f68c660fca]
description = "space surrounded by flowers"

[272d2306-9f62-44fe-8ab5-6b0f43a26338]
description = "horizontal line"

[c6f0a4b2-58d0-4bf6-ad8d-ccf4144f1f8e]
description = "horizontal line, flowers at edges"

[a54e84b7-3b25-44a8-b8cf-1753c8bb4cf5]
description = "vertical line"

[b40f42f5-dec5-4abc-b167-3f08195189c1]
description = "vertical line, flowers at edges"

[58674965-7b42-4818-b930-0215062d543c]
description = "cross"

[dd9d4ca8-9e68-4f78-a677-a2a70fd7a7b8]
description = "large garden"
9 changes: 9 additions & 0 deletions exercises/practice/flower-field/lib/flower_field.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
defmodule FlowerField do
@doc """
Annotate empty spots next to flowers with the number of flowers next to them.
"""
@spec annotate([String.t()]) :: [String.t()]

def annotate(board) do
end
end
28 changes: 28 additions & 0 deletions exercises/practice/flower-field/mix.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
defmodule FlowerField.MixProject do
use Mix.Project

def project do
[
app: :flower_field,
version: "0.1.0",
# elixir: "~> 1.8",
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
Loading