Skip to content

Commit 6ee25ea

Browse files
BNAndrasAnton-4
andauthored
Add flower-field, deprecating minesweeper (#175)
* Add `flower-field`, deprecating `minesweeper` * snake_case replaceEach --------- Co-authored-by: Anton-4 <[email protected]>
1 parent 7ec8483 commit 6ee25ea

File tree

9 files changed

+397
-1
lines changed

9 files changed

+397
-1
lines changed

config.json

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -739,6 +739,14 @@
739739
"prerequisites": [],
740740
"difficulty": 6
741741
},
742+
{
743+
"slug": "flower-field",
744+
"name": "Flower Field",
745+
"uuid": "3d515ca6-aa26-4e4f-a92d-8381ef6d3809",
746+
"practices": [],
747+
"prerequisites": [],
748+
"difficulty": 6
749+
},
742750
{
743751
"slug": "go-counting",
744752
"name": "Go Counting",
@@ -777,7 +785,8 @@
777785
"uuid": "8d5ea690-9d52-4d0a-a59e-aeec1a03cac7",
778786
"practices": [],
779787
"prerequisites": [],
780-
"difficulty": 6
788+
"difficulty": 6,
789+
"status": "deprecated"
781790
},
782791
{
783792
"slug": "palindrome-products",
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# Instructions
2+
3+
Your task is to add flower counts to empty squares in a completed Flower Field garden.
4+
The garden itself is a rectangle board composed of squares that are either empty (`' '`) or a flower (`'*'`).
5+
6+
For each empty square, count the number of flowers adjacent to it (horizontally, vertically, diagonally).
7+
If the empty square has no adjacent flowers, leave it empty.
8+
Otherwise replace it with the count of adjacent flowers.
9+
10+
For example, you may receive a 5 x 4 board like this (empty spaces are represented here with the '·' character for display on screen):
11+
12+
```text
13+
·*·*·
14+
··*··
15+
··*··
16+
·····
17+
```
18+
19+
Which your code should transform into this:
20+
21+
```text
22+
1*3*1
23+
13*31
24+
·2*2·
25+
·111·
26+
```
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Introduction
2+
3+
[Flower Field][history] is a compassionate reimagining of the popular game Minesweeper.
4+
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.
5+
"Flower Field" shipped in regional versions of Microsoft Windows in Italy, Germany, South Korea, Japan and Taiwan.
6+
7+
[history]: https://web.archive.org/web/20020409051321fw_/http://rcm.usr.dsi.unimi.it/rcmweb/fnm/
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
module [annotate]
2+
3+
is_flower : List (List U8), I64, I64 -> Result Bool [OutOfBounds]
4+
is_flower = |rows, nx, ny|
5+
x = Num.to_u64_checked(nx)?
6+
y = Num.to_u64_checked(ny)?
7+
rows
8+
|> List.get(y)?
9+
|> List.get(x)?
10+
|> Bool.is_eq('*')
11+
|> Ok
12+
13+
count_neighbors : List (List U8), U64, U64 -> Num *
14+
count_neighbors = |rows, x, y|
15+
[-1, 0, 1]
16+
|> List.map(
17+
|dy|
18+
[-1, 0, 1]
19+
|> List.map(
20+
|dx|
21+
when is_flower(rows, (Num.to_i64(x) + dx), (Num.to_i64(y) + dy)) is
22+
Ok(flower) -> if flower then 1 else 0
23+
Err(OutOfBounds) -> 0,
24+
)
25+
|> List.sum,
26+
)
27+
|> List.sum
28+
29+
annotate : Str -> Str
30+
annotate = |garden|
31+
rows = garden |> Str.to_utf8 |> List.split_on('\n')
32+
annotated =
33+
rows
34+
|> List.map_with_index(
35+
|row, y|
36+
row
37+
|> List.map_with_index(
38+
|cell, x|
39+
if cell == '*' then
40+
'*'
41+
else
42+
when count_neighbors(rows, x, y) is
43+
0 -> ' '
44+
n -> '0' + n,
45+
)
46+
|> Str.from_utf8,
47+
)
48+
49+
annotated
50+
|> List.map(
51+
|maybe_row|
52+
when maybe_row is
53+
Ok(row) -> row
54+
Err(_) -> crash("Unreachable"),
55+
) # fromUtf8 cannot fail in the code above
56+
|> Str.join_with("\n")
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"authors": [
3+
"ageron"
4+
],
5+
"contributors": [
6+
"BNAndras"
7+
],
8+
"files": {
9+
"solution": [
10+
"FlowerField.roc"
11+
],
12+
"test": [
13+
"flower-field-test.roc"
14+
],
15+
"example": [
16+
".meta/Example.roc"
17+
]
18+
},
19+
"blurb": "Mark all the flowers in a garden."
20+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{%- import "generator_macros.j2" as macros with context -%}
2+
{{ macros.canonical_ref() }}
3+
{{ macros.header() }}
4+
5+
import {{ exercise | to_pascal }} exposing [{{ cases[0]["property"] | to_snake }}]
6+
7+
{% for case in cases -%}
8+
# {{ case["description"] }}
9+
expect
10+
garden = {{ case["input"]["garden"] | to_roc_multiline_string | replace(" ", "·") | indent(8) }} |> Str.replace_each "·" " "
11+
result = {{ case["property"] | to_snake }} garden
12+
expected = {{ case["expected"] | to_roc_multiline_string | replace(" ", "·") | indent(8) }} |> Str.replace_each "·" " "
13+
result == expected
14+
15+
{% endfor %}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# This is an auto-generated file.
2+
#
3+
# Regenerating this file via `configlet sync` will:
4+
# - Recreate every `description` key/value pair
5+
# - Recreate every `reimplements` key/value pair, where they exist in problem-specifications
6+
# - Remove any `include = true` key/value pair (an omitted `include` key implies inclusion)
7+
# - Preserve any other key/value pair
8+
#
9+
# As user-added comments (using the # character) will be removed when this file
10+
# is regenerated, comments can be added via a `comment` key.
11+
12+
[237ff487-467a-47e1-9b01-8a891844f86c]
13+
description = "no rows"
14+
15+
[4b4134ec-e20f-439c-a295-664c38950ba1]
16+
description = "no columns"
17+
18+
[d774d054-bbad-4867-88ae-069cbd1c4f92]
19+
description = "no flowers"
20+
21+
[225176a0-725e-43cd-aa13-9dced501f16e]
22+
description = "garden full of flowers"
23+
24+
[3f345495-f1a5-4132-8411-74bd7ca08c49]
25+
description = "flower surrounded by spaces"
26+
27+
[6cb04070-4199-4ef7-a6fa-92f68c660fca]
28+
description = "space surrounded by flowers"
29+
30+
[272d2306-9f62-44fe-8ab5-6b0f43a26338]
31+
description = "horizontal line"
32+
33+
[c6f0a4b2-58d0-4bf6-ad8d-ccf4144f1f8e]
34+
description = "horizontal line, flowers at edges"
35+
36+
[a54e84b7-3b25-44a8-b8cf-1753c8bb4cf5]
37+
description = "vertical line"
38+
39+
[b40f42f5-dec5-4abc-b167-3f08195189c1]
40+
description = "vertical line, flowers at edges"
41+
42+
[58674965-7b42-4818-b930-0215062d543c]
43+
description = "cross"
44+
45+
[dd9d4ca8-9e68-4f78-a677-a2a70fd7a7b8]
46+
description = "large garden"
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module [annotate]
2+
3+
annotate : Str -> Str
4+
annotate = |garden|
5+
crash("Please implement the 'annotate' function")

0 commit comments

Comments
 (0)