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
11 changes: 10 additions & 1 deletion config.json
Original file line number Diff line number Diff line change
Expand Up @@ -800,6 +800,14 @@
"randomness"
]
},
{
"slug": "flower-field",
"name": "Flower Field",
"uuid": "0af8fefb-2d20-483d-9f4a-9a274ad46bb7",
"practices": [],
"prerequisites": [],
"difficulty": 5
},
{
"slug": "food-chain",
"name": "Food Chain",
Expand Down Expand Up @@ -831,7 +839,8 @@
"uuid": "eae564ba-b4d9-4463-ae3f-f7f05f4e52ea",
"practices": [],
"prerequisites": [],
"difficulty": 5
"difficulty": 5,
"status": "deprecated"
},
{
"slug": "rational-numbers",
Expand Down
19 changes: 19 additions & 0 deletions exercises/practice/flower-field/.docs/hints.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Hints for Bash Flower Field Exercise

This exercise seems to require a two-dimensional array.
Bash only offers one-dimensional indexed or associative [arrays][array].

Multi-dimensional arrays can be simulated by using an associative arrays: encode the indices into a comma-separated string to use as the array key.

```bash
# store this matrix:
# a b
# c d
declare -A array2d
array2d["0,0"]="a"
array2d["0,1"]="b"
array2d["1,0"]="c"
array2d["1,1"]="d"
```

[array]: https://www.gnu.org/software/bash/manual/bash.html#Arrays
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/
17 changes: 17 additions & 0 deletions exercises/practice/flower-field/.meta/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"authors": [
"IsaacG"
],
"files": {
"solution": [
"flower_field.sh"
],
"test": [
"flower_field.bats"
],
"example": [
".meta/example.sh"
]
},
"blurb": "Mark all the flowers in a garden."
}
51 changes: 51 additions & 0 deletions exercises/practice/flower-field/.meta/example.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#!/usr/bin/env bash

# some global vars
declare -A board
height=$#
width=${#1}

parse_input() {
local rownum=0 row col index char
for row in "$@"; do
for ((col = 0; col < "${#row}"; col++)); do
index="${rownum},${col}"
char=${row:col:1}
board[$index]=$char
done
(( rownum++ ))
done
}

count() {
local r=$1 c=$2
local count=0
for dr in -1 0 1; do
(( r + dr < 0 || r + dr == height )) && continue
for dc in -1 0 1; do
(( c + dc < 0 || c + dc == width )) && continue
(( dr == 0 && dc == 0 )) && continue
index="$((r + dr)),$((c + dc))"
[[ ${board[$index]} == "*" ]] && (( count++ ))
done
done
(( count == 0 )) && echo " " || echo "$count"
}

create_output() {
local row output col
for ((row = 0; row < height; row++)); do
output=""
for ((col = 0; col < width; col++)); do
if [[ ${board[$row,$col]} == "*" ]]; then
output+="*"
else
output+=$( count "$row" "$col" )
fi
done
echo "$output"
done
}

parse_input "$@"
create_output
47 changes: 47 additions & 0 deletions exercises/practice/flower-field/.meta/tests.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# 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"
include = false

[4b4134ec-e20f-439c-a295-664c38950ba1]
description = "no columns"

[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"
Loading