Skip to content

Commit 2857e8f

Browse files
authored
Add flower-field (exercism#490)
1 parent e0b0add commit 2857e8f

File tree

8 files changed

+295
-1
lines changed

8 files changed

+295
-1
lines changed

config.json

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -703,6 +703,17 @@
703703
"prerequisites": [],
704704
"difficulty": 5
705705
},
706+
{
707+
"slug": "flower-field",
708+
"name": "Flower Field",
709+
"uuid": "31e7c5c0-17eb-4521-b4f9-0579e3917c83",
710+
"practices": [],
711+
"prerequisites": [],
712+
"difficulty": 5,
713+
"topics": [
714+
"control_flow_loops"
715+
]
716+
},
706717
{
707718
"slug": "food-chain",
708719
"name": "Food Chain",
@@ -777,7 +788,8 @@
777788
"difficulty": 5,
778789
"topics": [
779790
"control_flow_loops"
780-
]
791+
],
792+
"status": "deprecated"
781793
},
782794
{
783795
"slug": "nucleotide-count",
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: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"authors": [
3+
"keiravillekode"
4+
],
5+
"contributors": [
6+
"BNAndras"
7+
],
8+
"files": {
9+
"solution": [
10+
"flower-field.el"
11+
],
12+
"test": [
13+
"flower-field-test.el"
14+
],
15+
"example": [
16+
".meta/example.el"
17+
]
18+
},
19+
"blurb": "Mark all the flowers in a garden."
20+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
;;; flower-field.el --- Flower Field (exercism) -*- lexical-binding: t; -*-
2+
3+
;;; Commentary:
4+
5+
;;; Code:
6+
7+
(require 'cl-lib)
8+
9+
(defun annotate (garden)
10+
(when garden
11+
(let* ((num-columns (length (car garden)))
12+
(lines (vconcat garden))
13+
(num-rows (length lines))
14+
(results nil))
15+
(cl-loop for row from (1- num-rows) downto 0
16+
for result = (copy-sequence (aref lines row))
17+
for r-start = (max 0 (1- row))
18+
for r-end = (min num-rows (+ 2 row))
19+
do (cl-loop for column from 0 below num-columns
20+
for c-start = (max 0 (1- column))
21+
for c-end = (min num-columns (+ 2 column))
22+
for count = 0
23+
do (unless (= ?* (aref result column))
24+
(cl-loop for r from r-start below r-end
25+
for line = (aref lines r)
26+
do (cl-loop for c from c-start below c-end
27+
do (when (= ?* (aref line c))
28+
(setq count (1+ count)))))
29+
(unless (= 0 count)
30+
(aset result column (+ ?0 count)))))
31+
do (setq results (cons result results)))
32+
results)))
33+
34+
(provide 'flower-field)
35+
;;; flower-field.el ends here
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: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
;;; flower-field-test.el --- Tests for Flower Field (exercism) -*- lexical-binding: t; -*-
2+
3+
;;; Commentary:
4+
5+
;;; Code:
6+
7+
8+
(load-file "flower-field.el")
9+
(declare-function annotate "flower-field.el" (garden))
10+
11+
12+
(ert-deftest no-rows ()
13+
(should (equal '()
14+
15+
(annotate '()))))
16+
17+
18+
(ert-deftest no-columns ()
19+
(should (equal '("")
20+
21+
(annotate '("")))))
22+
23+
24+
(ert-deftest no-flowers ()
25+
(should (equal '(" "
26+
" "
27+
" ")
28+
29+
(annotate '(" "
30+
" "
31+
" ")))))
32+
33+
34+
(ert-deftest garden-full-of-flowers ()
35+
(should (equal '("***"
36+
"***"
37+
"***")
38+
39+
(annotate '("***"
40+
"***"
41+
"***")))))
42+
43+
44+
(ert-deftest flower-surrounded-by-spaces ()
45+
(should (equal '("111"
46+
"1*1"
47+
"111")
48+
49+
(annotate '(" "
50+
" * "
51+
" ")))))
52+
53+
54+
(ert-deftest space-surrounded-by-flowers ()
55+
(should (equal '("***"
56+
"*8*"
57+
"***")
58+
59+
(annotate '("***"
60+
"* *"
61+
"***")))))
62+
63+
64+
(ert-deftest horizontal-line ()
65+
(should (equal '("1*2*1")
66+
67+
(annotate '(" * * ")))))
68+
69+
70+
(ert-deftest horizontal-line-flowers-at-edges ()
71+
(should (equal '("*1 1*")
72+
73+
(annotate '("* *")))))
74+
75+
76+
(ert-deftest vertical-line ()
77+
(should (equal '("1"
78+
"*"
79+
"2"
80+
"*"
81+
"1")
82+
83+
(annotate '(" "
84+
"*"
85+
" "
86+
"*"
87+
" ")))))
88+
89+
90+
(ert-deftest vertical-line-flowers-at-edges ()
91+
(should (equal '("*"
92+
"1"
93+
" "
94+
"1"
95+
"*")
96+
97+
(annotate '("*"
98+
" "
99+
" "
100+
" "
101+
"*")))))
102+
103+
104+
(ert-deftest cross ()
105+
(should (equal '(" 2*2 "
106+
"25*52"
107+
"*****"
108+
"25*52"
109+
" 2*2 ")
110+
111+
(annotate '(" * "
112+
" * "
113+
"*****"
114+
" * "
115+
" * ")))))
116+
117+
118+
(ert-deftest large-garden ()
119+
(should (equal '("1*22*1"
120+
"12*322"
121+
" 123*2"
122+
"112*4*"
123+
"1*22*2"
124+
"111111")
125+
126+
(annotate '(" * * "
127+
" * "
128+
" * "
129+
" * *"
130+
" * * "
131+
" ")))))
132+
133+
134+
(provide 'flower-field-test)
135+
;;; flower-field-test.el ends here
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
;;; flower-field.el --- Flower Field (exercism) -*- lexical-binding: t; -*-
2+
3+
;;; Commentary:
4+
5+
;;; Code:
6+
7+
8+
(defun annotate (garden)
9+
(error "Delete this S-Expression and write your own implementation"))
10+
11+
12+
(provide 'flower-field)
13+
;;; flower-field.el ends here

0 commit comments

Comments
 (0)