Skip to content

Commit 137e46c

Browse files
Add flower-field exercise (#510)
1 parent bb799e9 commit 137e46c

File tree

8 files changed

+396
-0
lines changed

8 files changed

+396
-0
lines changed

config.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1035,6 +1035,14 @@
10351035
"prerequisites": [],
10361036
"difficulty": 6
10371037
},
1038+
{
1039+
"slug": "flower-field",
1040+
"name": "Flower Field",
1041+
"uuid": "afd931dd-33dd-4961-bd2b-251c62fb9311",
1042+
"practices": [],
1043+
"prerequisites": [],
1044+
"difficulty": 6
1045+
},
10381046
{
10391047
"slug": "piecing-it-together",
10401048
"name": "Piecing It Together",
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+
"keiravillekode"
4+
],
5+
"files": {
6+
"solution": [
7+
"flower_field.zig"
8+
],
9+
"test": [
10+
"test_flower_field.zig"
11+
],
12+
"example": [
13+
".meta/example.zig"
14+
]
15+
},
16+
"blurb": "Mark all the flowers in a garden."
17+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
const std = @import("std");
2+
const mem = std.mem;
3+
4+
fn free(allocator: mem.Allocator, array_list: *std.ArrayList([]u8)) void {
5+
for (array_list.items) |item| {
6+
allocator.free(item);
7+
}
8+
array_list.deinit(allocator);
9+
}
10+
11+
pub fn annotate(allocator: mem.Allocator, garden: []const []const u8) mem.Allocator.Error![][]u8 {
12+
const height = garden.len;
13+
var array_list = try std.ArrayList([]u8).initCapacity(allocator, height);
14+
errdefer free(allocator, &array_list);
15+
if (height == 0) {
16+
return array_list.toOwnedSlice(allocator);
17+
}
18+
19+
const width = garden[0].len;
20+
21+
for (0..height) |i| {
22+
var row = try allocator.alloc(u8, width);
23+
for (0..width) |j| {
24+
if (garden[i][j] == '*') {
25+
row[j] = '*';
26+
} else {
27+
var count: u8 = 0;
28+
for (0..3) |di| {
29+
for (0..3) |dj| {
30+
const r = i + di;
31+
const c = j + dj;
32+
if (r >= 1 and c >= 1 and r <= height and c <= width and garden[r - 1][c - 1] == '*') {
33+
count += 1;
34+
}
35+
}
36+
}
37+
38+
if (count == 0) {
39+
row[j] = ' ';
40+
} else {
41+
row[j] = '0' + count;
42+
}
43+
}
44+
}
45+
46+
array_list.appendAssumeCapacity(row);
47+
}
48+
49+
return array_list.toOwnedSlice(allocator);
50+
}
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: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
const std = @import("std");
2+
const mem = std.mem;
3+
4+
pub fn annotate(allocator: mem.Allocator, garden: []const []const u8) mem.Allocator.Error![][]u8 {
5+
_ = allocator;
6+
_ = garden;
7+
@compileError("please implement the annotate function");
8+
}

0 commit comments

Comments
 (0)