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 @@ -1372,9 +1372,9 @@
"difficulty": 8
},
{
"slug": "minesweeper",
"name": "Minesweeper",
"uuid": "c4e75eb8-6e26-4582-95b6-7ea3205ede6e",
"slug": "flower-field",
"name": "Flower Field",
"uuid": "e26395e9-2fed-4ede-8175-5e9d7c556566",
"practices": [
"dict"
],
Expand All @@ -1388,6 +1388,15 @@
],
"difficulty": 5
},
{
"slug": "minesweeper",
"name": "Minesweeper",
"uuid": "c4e75eb8-6e26-4582-95b6-7ea3205ede6e",
"practices": [],
"prerequisites": [],
"difficulty": 5,
"status": "deprecated"
},
{
"slug": "protein-translation",
"name": "Protein Translation",
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/
20 changes: 20 additions & 0 deletions exercises/practice/flower-field/.meta/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"authors": [
"jiegillet"
],
"contributors": [
"BNAndras"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

],
"files": {
"solution": [
"src/FlowerField.elm"
],
"test": [
"tests/Tests.elm"
],
"example": [
".meta/src/FlowerField.example.elm"
]
},
"blurb": "Mark all the flowers in a garden."
}
95 changes: 95 additions & 0 deletions exercises/practice/flower-field/.meta/src/FlowerField.example.elm
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
module FlowerField exposing (annotate)

import Dict exposing (Dict)


type Tile
= Empty
| Flower
| Annotated Int


annotate : String -> String
annotate garden =
let
positionMap =
makePositionMap garden
in
positionMap
|> Dict.map (annotateTile positionMap)
|> mergeRows
|> Dict.values
|> List.map (List.map printTile >> String.concat)
|> String.join "\n"


makePositionMap : String -> Dict ( Int, Int ) Tile
makePositionMap input =
input
|> String.lines
|> List.indexedMap
(\row line ->
line
|> String.toList
|> List.indexedMap
(\column char ->
case char of
'*' ->
( ( row, column ), Flower )

_ ->
( ( row, column ), Empty )
)
)
|> List.concat
|> Dict.fromList


annotateTile : Dict ( Int, Int ) Tile -> ( Int, Int ) -> Tile -> Tile
annotateTile garden ( row, col ) tile =
case tile of
Empty ->
[ ( row + 1, col + 1 )
, ( row + 1, col )
, ( row + 1, col - 1 )
, ( row, col + 1 )
, ( row, col - 1 )
, ( row - 1, col + 1 )
, ( row - 1, col )
, ( row - 1, col - 1 )
]
|> List.filter (\p -> Dict.get p garden == Just Flower)
|> List.length
|> Annotated

other ->
other


mergeRows : Dict ( Int, Int ) Tile -> Dict Int (List Tile)
mergeRows =
Dict.foldl
-- we rely on Dict.foldl traversing the keys in order
(\( row, _ ) tile rows ->
Dict.update row
(\line -> Just (tile :: Maybe.withDefault [] line))
rows
)
Dict.empty
>> Dict.map (always List.reverse)


printTile : Tile -> String
printTile tile =
case tile of
Flower ->
"*"

Empty ->
"."

Annotated 0 ->
"."

Annotated count ->
String.fromInt count
48 changes: 48 additions & 0 deletions exercises/practice/flower-field/.meta/tests.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# 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"
include = false
comment = "empty board already covered by 237ff487-467a-47e1-9b01-8a891844f86c"

[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"
29 changes: 29 additions & 0 deletions exercises/practice/flower-field/elm.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"type": "application",
"source-directories": [
"src"
],
"elm-version": "0.19.1",
"dependencies": {
"direct": {
"elm/core": "1.0.5",
"elm/json": "1.1.3",
"elm/parser": "1.1.0",
"elm/random": "1.0.0",
"elm/regex": "1.0.0",
"elm/time": "1.0.0",
"elm/html": "1.0.0"
},
"indirect": {}
},
"test-dependencies": {
"direct": {
"elm-explorations/test": "2.1.0",
"rtfeldman/elm-iso8601-date-strings": "1.1.4"
},
"indirect": {
"elm/bytes": "1.0.8",
"elm/virtual-dom": "1.0.3"
}
}
}
6 changes: 6 additions & 0 deletions exercises/practice/flower-field/src/FlowerField.elm
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module FlowerField exposing (annotate)


annotate : String -> String
annotate garden =
Debug.todo "Please implement annotate"
108 changes: 108 additions & 0 deletions exercises/practice/flower-field/tests/Tests.elm
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
module Tests exposing (tests)

import Expect
import FlowerField
import Test exposing (Test, describe, skip, test)


tests : Test
tests =
describe "FlowerField"
[ -- skip <|
test "no rows" <|
\() ->
FlowerField.annotate ""
|> Expect.equal ""
, skip <|
test "no flowers" <|
\() ->
FlowerField.annotate """...
...
...""" |> Expect.equal """...
...
..."""
, skip <|
test "garden full of flowers" <|
\() ->
FlowerField.annotate """***
***
***""" |> Expect.equal """***
***
***"""
, skip <|
test "flower surrounded by spaces" <|
\() ->
FlowerField.annotate """...
.*.
...""" |> Expect.equal """111
1*1
111"""
, skip <|
test "space surrounded by flowers" <|
\() ->
FlowerField.annotate """***
*.*
***""" |> Expect.equal """***
*8*
***"""
, skip <|
test "horizontal line" <|
\() ->
FlowerField.annotate ".*.*."
|> Expect.equal "1*2*1"
, skip <|
test "horizontal line, flowers at edges" <|
\() ->
FlowerField.annotate "*...*"
|> Expect.equal "*1.1*"
, skip <|
test "vertical line" <|
\() ->
FlowerField.annotate """.
*
.
*
.""" |> Expect.equal """1
*
2
*
1"""
, skip <|
test "vertical line, flowers at edges" <|
\() ->
FlowerField.annotate """*
.
.
.
*""" |> Expect.equal """*
1
.
1
*"""
, skip <|
test "cross" <|
\() ->
FlowerField.annotate """..*..
..*..
*****
..*..
..*..""" |> Expect.equal """.2*2.
25*52
*****
25*52
.2*2."""
, skip <|
test "large garden" <|
\() ->
FlowerField.annotate """.*..*.
..*...
....*.
...*.*
.*..*.
......""" |> Expect.equal """1*22*1
12*322
.123*2
112*4*
1*22*2
111111"""
]