Skip to content

Commit 0593647

Browse files
authored
Replace Minesweeper with Flower Field (#284)
1 parent fa0ef11 commit 0593647

File tree

11 files changed

+1059
-1
lines changed

11 files changed

+1059
-1
lines changed

config.json

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -770,6 +770,14 @@
770770
"prerequisites": [],
771771
"difficulty": 5
772772
},
773+
{
774+
"slug": "flower-field",
775+
"name": "Flower Field",
776+
"uuid": "2f3d7abb-2365-44f8-a40a-abe5c6212000",
777+
"practices": [],
778+
"prerequisites": [],
779+
"difficulty": 9
780+
},
773781
{
774782
"slug": "forth",
775783
"name": "Forth",
@@ -792,7 +800,8 @@
792800
"uuid": "56376780-4074-4a07-a914-2230ef96abee",
793801
"practices": [],
794802
"prerequisites": [],
795-
"difficulty": 9
803+
"difficulty": 9,
804+
"status": "deprecated"
796805
},
797806
{
798807
"slug": "two-bucket",
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
# ignore
2+
3+
## Additional jq command line options
4+
5+
You'll notice extra options for `jq` in the tests.
6+
7+
```sh
8+
run jq -s -R -c -f flower-field.jq << 'END_INPUT'
9+
```
10+
11+
What are they?
12+
13+
* `-s`
14+
15+
This is "slurp" mode.
16+
Instead of handling the input as a stream of JSON objects,
17+
all the input will be slurped into an array.
18+
19+
```sh
20+
$ seq 5 | jq -c '.'
21+
1
22+
2
23+
3
24+
4
25+
5
26+
$ seq 5 | jq -s -c '.'
27+
[1,2,3,4,5]
28+
```
29+
30+
* `-R`
31+
32+
You may have seen `-r` already, to output "raw" strings (without quotes).
33+
This is the opposite.
34+
Each line of input is taken to be a string.
35+
36+
Two examples:
37+
38+
```sh
39+
$ seq 5 | jq -R -c .
40+
"1"
41+
"2"
42+
"3"
43+
"4"
44+
"5"
45+
46+
$ echo 42 | jq '. * 2' # input is a JSON number
47+
84
48+
$ echo 42 | jq -R '. * 2' # input is a JSON string
49+
"4242"
50+
```
51+
52+
When you put these two options together, you _don't_ get an array of strings:
53+
you get the entire input as a single string, including end-of-line newlines.
54+
55+
```sh
56+
$ seq 5 | jq -s -R -c .
57+
"1\n2\n3\n4\n5\n"
58+
```
59+
60+
Handling this is part of the challenge of this exercise.
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: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"authors": [
3+
"glennj",
4+
"IsaacG"
5+
],
6+
"files": {
7+
"solution": [
8+
"flower-field.jq"
9+
],
10+
"test": [
11+
"test-flower-field.bats"
12+
],
13+
"example": [
14+
".meta/example.jq"
15+
]
16+
},
17+
"blurb": "Mark all the flowers in a garden."
18+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
# perhaps an over-reliance on `reduce`, but this is just a proof
2+
3+
def between($lo; $hi): $lo <= . and . < $hi;
4+
5+
def count_neighbouring_flowers($i; $j):
6+
. as $m
7+
| [[-1,-1],[-1,0],[-1,1],
8+
[ 0,-1], [ 0,1],
9+
[ 1,-1],[ 1,0],[ 1,1]] as $neighbours
10+
| reduce $neighbours[] as [$di, $dj] (
11+
0;
12+
. + if ($i + $di | between(0; $m | length)) and
13+
($j + $dj | between(0; $m[$i] | length)) and
14+
$m[$i + $di][$j + $dj] == 99
15+
then 1
16+
else 0
17+
end
18+
)
19+
;
20+
21+
def annotate:
22+
. as $m
23+
| reduce range(length) as $i ([]; . + [
24+
reduce range($m[$i] | length) as $j ([]; . + [
25+
if $m[$i][$j] == 99
26+
then 99
27+
else ($m | count_neighbouring_flowers($i; $j))
28+
end
29+
])
30+
])
31+
;
32+
33+
# input is a string of newline-terminated rows.
34+
# "*" indicates a flower; "." indicates no flower.
35+
# output is an array of arrays of integers.
36+
# e.g. "*..\n...\n..*\n" => [[99,0,0],[0,0,0],[0,0,99]]
37+
#
38+
def to_numeric_array:
39+
if . == "\n" then [[]] else
40+
rtrimstr("\n")
41+
| split("\n")
42+
| map([ split("")[] | if . == "." then 0 else 99 end ])
43+
end
44+
;
45+
46+
# reverse of above.
47+
# [[99,1,0],[1,2,1],[0,1,99]] => ["*1.","121",".1*"]
48+
def to_string_array:
49+
map(
50+
map(if . == 0 then "." elif . == 99 then "*" else tostring end)
51+
| join("")
52+
)
53+
;
54+
55+
to_numeric_array
56+
| annotate
57+
| to_string_array
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"

0 commit comments

Comments
 (0)