Skip to content

Commit ae83508

Browse files
committed
Add flower-field
1 parent 55e4209 commit ae83508

File tree

9 files changed

+512
-0
lines changed

9 files changed

+512
-0
lines changed

config.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -392,6 +392,14 @@
392392
"prerequisites": [],
393393
"difficulty": 5
394394
},
395+
{
396+
"slug": "flower-field",
397+
"name": "Flower Field",
398+
"uuid": "6c162b52-01dc-4845-af09-1af65f112946",
399+
"practices": [],
400+
"prerequisites": [],
401+
"difficulty": 5
402+
},
395403
{
396404
"slug": "matching-brackets",
397405
"name": "Matching Brackets",
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: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"authors": [
3+
"BNAndras"
4+
],
5+
"files": {
6+
"solution": [
7+
"flower-field.red"
8+
],
9+
"test": [
10+
"flower-field-test.red"
11+
],
12+
"example": [
13+
".meta/example.red"
14+
]
15+
},
16+
"blurb": "Mark all the flowers in a garden."
17+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
Red [
2+
description: {"Flower Field" exercise solution for exercism platform}
3+
author: "BNAndras"
4+
]
5+
6+
annotate: function [
7+
garden
8+
][
9+
rows: length? garden
10+
cols: length? first garden
11+
result: []
12+
13+
repeat i rows [
14+
marked-row: copy ""
15+
current-row: pick garden i
16+
17+
repeat j cols [
18+
cell: pick current-row j
19+
20+
either cell = #"*" [
21+
append marked-row "*"
22+
][
23+
count: 0
24+
min-row: max 1 (i - 1)
25+
max-row: min rows (i + 1)
26+
min-col: max 1 (j - 1)
27+
max-col: min cols (j + 1)
28+
29+
row-indices: collect [
30+
repeat k (max-row - min-row + 1) [keep min-row + k - 1]
31+
]
32+
col-indices: collect [
33+
repeat k (max-col - min-col + 1) [keep min-col + k - 1]
34+
]
35+
36+
foreach ni row-indices [
37+
foreach nj col-indices [
38+
if not and~ ni = i nj = j [
39+
neighbor-row: pick garden ni
40+
neighbor: pick neighbor-row nj
41+
if neighbor = #"*" [
42+
count: count + 1
43+
]
44+
]
45+
]
46+
]
47+
48+
either count = 0 [
49+
append marked-row " "
50+
][
51+
append marked-row to string! count
52+
]
53+
]
54+
]
55+
56+
append result marked-row
57+
]
58+
59+
result
60+
]
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: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
Red [
2+
description: {Tests for "Flower Field" Exercism exercise}
3+
author: "loziniak"
4+
]
5+
6+
#include %testlib.red
7+
8+
test-init/limit %flower-field.red 1
9+
; test-init/limit %.meta/example.red 1 ; test example solution
10+
11+
canonical-cases: [#[
12+
description: "no rows"
13+
input: #[
14+
garden: []
15+
]
16+
expected: []
17+
function: "annotate"
18+
uuid: "237ff487-467a-47e1-9b01-8a891844f86c"
19+
] #[
20+
description: "no columns"
21+
input: #[
22+
garden: [""]
23+
]
24+
expected: [""]
25+
function: "annotate"
26+
uuid: "4b4134ec-e20f-439c-a295-664c38950ba1"
27+
] #[
28+
description: "no flowers"
29+
input: #[
30+
garden: [" " " " " "]
31+
]
32+
expected: [" " " " " "]
33+
function: "annotate"
34+
uuid: "d774d054-bbad-4867-88ae-069cbd1c4f92"
35+
] #[
36+
description: "garden full of flowers"
37+
input: #[
38+
garden: ["***" "***" "***"]
39+
]
40+
expected: ["***" "***" "***"]
41+
function: "annotate"
42+
uuid: "225176a0-725e-43cd-aa13-9dced501f16e"
43+
] #[
44+
description: "flower surrounded by spaces"
45+
input: #[
46+
garden: [" " " * " " "]
47+
]
48+
expected: ["111" "1*1" "111"]
49+
function: "annotate"
50+
uuid: "3f345495-f1a5-4132-8411-74bd7ca08c49"
51+
] #[
52+
description: "space surrounded by flowers"
53+
input: #[
54+
garden: ["***" "* *" "***"]
55+
]
56+
expected: ["***" "*8*" "***"]
57+
function: "annotate"
58+
uuid: "6cb04070-4199-4ef7-a6fa-92f68c660fca"
59+
] #[
60+
description: "horizontal line"
61+
input: #[
62+
garden: [" * * "]
63+
]
64+
expected: ["1*2*1"]
65+
function: "annotate"
66+
uuid: "272d2306-9f62-44fe-8ab5-6b0f43a26338"
67+
] #[
68+
description: "horizontal line, flowers at edges"
69+
input: #[
70+
garden: ["* *"]
71+
]
72+
expected: ["*1 1*"]
73+
function: "annotate"
74+
uuid: "c6f0a4b2-58d0-4bf6-ad8d-ccf4144f1f8e"
75+
] #[
76+
description: "vertical line"
77+
input: #[
78+
garden: [" " "*" " " "*" " "]
79+
]
80+
expected: ["1" "*" "2" "*" "1"]
81+
function: "annotate"
82+
uuid: "a54e84b7-3b25-44a8-b8cf-1753c8bb4cf5"
83+
] #[
84+
description: "vertical line, flowers at edges"
85+
input: #[
86+
garden: ["*" " " " " " " "*"]
87+
]
88+
expected: ["*" "1" " " "1" "*"]
89+
function: "annotate"
90+
uuid: "b40f42f5-dec5-4abc-b167-3f08195189c1"
91+
] #[
92+
description: "cross"
93+
input: #[
94+
garden: [" * " " * " "*****" " * " " * "]
95+
]
96+
expected: [" 2*2 " "25*52" "*****" "25*52" " 2*2 "]
97+
function: "annotate"
98+
uuid: "58674965-7b42-4818-b930-0215062d543c"
99+
] #[
100+
description: "large garden"
101+
input: #[
102+
garden: [" * * " " * " " * " " * *" " * * " " "]
103+
]
104+
expected: ["1*22*1" "12*322" " 123*2" "112*4*" "1*22*2" "111111"]
105+
function: "annotate"
106+
uuid: "dd9d4ca8-9e68-4f78-a677-a2a70fd7a7b8"
107+
]]
108+
109+
110+
foreach c-case canonical-cases [
111+
case-code: reduce [
112+
'expect c-case/expected compose [
113+
(to word! c-case/function) (values-of c-case/input)
114+
]
115+
]
116+
117+
test c-case/description case-code
118+
]
119+
120+
test-results/print
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
Red [
2+
description: {"Flower Field" exercise solution for exercism platform}
3+
author: "" ; you can write your name here, in quotes
4+
]
5+
6+
annotate: function [
7+
garden
8+
] [
9+
cause-error 'user 'message "You need to implement annotate function."
10+
]
11+

0 commit comments

Comments
 (0)