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
19 changes: 16 additions & 3 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -1274,9 +1274,9 @@
]
},
{
"slug": "minesweeper",
"name": "Minesweeper",
"uuid": "ca6d49c7-e850-4e81-9540-582a1353f604",
"slug": "flower-field",
"name": "Flower Field",
"uuid": "4b9d68e5-2faa-44b2-b330-92be228da2d9",
"practices": [],
"prerequisites": [],
"difficulty": 7,
Expand Down Expand Up @@ -1388,6 +1388,19 @@
"looping",
"strings"
]
},
{
"slug": "minesweeper",
"name": "Minesweeper",
"uuid": "ca6d49c7-e850-4e81-9540-582a1353f604",
"practices": [],
"prerequisites": [],
"difficulty": 7,
"status": "deprecated",
"topics": [
"parsing",
"transforming"
]
}
]
},
Expand Down
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/
4 changes: 4 additions & 0 deletions exercises/practice/flower-field/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.DS_Store
/.build
/Packages
/*.xcodeproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
class Board {
var rows: [String]

init(_ rows: [String]) {
self.rows = rows
}

func transform() -> [String] {
var result = [String]()
for (rowIndex, row) in rows.enumerated() {
var resultRow = ""
for (columnIndex, column) in row.enumerated() {
if column == "*" {
resultRow += "*"
} else {
let count = countFlowers(rowIndex: rowIndex, columnIndex: columnIndex)
resultRow += count == 0 ? " " : String(count)
}
}
result.append(resultRow)
}

return result
}

private func countFlowers(rowIndex: Int, columnIndex: Int) -> Int {
var count = 0
for row in rowIndex - 1...rowIndex + 1 {
for column in columnIndex - 1...columnIndex + 1 {
if row >= 0 && row < rows.count && column >= 0 && column < rows[row].count {
let currentRow = rows[row]
let currentColumn = currentRow[currentRow.index(currentRow.startIndex, offsetBy: column)]
if currentColumn == "*" {
count += 1
}
}
}
}
return count
}
}
25 changes: 25 additions & 0 deletions exercises/practice/flower-field/.meta/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"authors": [],
"contributors": [
"bhargavg",
"BNAndras",
"harquail",
"lyuha",
"masters3d",
"robtimp",
"ThomasHaz",
"meatball133"
],
"files": {
"solution": [
"Sources/FlowerField/FlowerField.swift"
],
"test": [
"Tests/FlowerFieldTests/FlowerFieldTests.swift"
],
"example": [
".meta/Sources/FlowerField/FlowerFieldExample.swift"
]
},
"blurb": "Mark all the flowers in a garden."
}
25 changes: 25 additions & 0 deletions exercises/practice/flower-field/.meta/template.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import Testing
import Foundation
@testable import {{exercise|camelCase}}

let RUNALL = Bool(ProcessInfo.processInfo.environment["RUNALL", default: "false"]) ?? false

@Suite struct {{exercise|camelCase}}Tests {
{% for case in cases %}
{% if forloop.first -%}
@Test("{{case.description}}")
{% else -%}
@Test("{{case.description}}", .enabled(if: RUNALL))
{% endif -%}
func test{{case.description |camelCase }}() {
{% ifnot case.input.garden -%}
let input = [String]()
let output = [String]()
{% else -%}
let input = {{case.input.garden | toStringArray}}
let output = {{case.expected | toStringArray}}
{% endif -%}
#expect(Board(input).transform() == output)
}
{% endfor -%}
}
46 changes: 46 additions & 0 deletions exercises/practice/flower-field/.meta/tests.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# 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"

[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"
21 changes: 21 additions & 0 deletions exercises/practice/flower-field/Package.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// swift-tools-version:6.0

import PackageDescription

let package = Package(
name: "FlowerField",
products: [
.library(
name: "FlowerField",
targets: ["FlowerField"])
],
dependencies: [],
targets: [
.target(
name: "FlowerField",
dependencies: []),
.testTarget(
name: "FlowerFieldTests",
dependencies: ["FlowerField"]),
]
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
class Board {
// Write your code for the 'Flower Field' exercise in this file.
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
import Foundation
import Testing

@testable import FlowerField

let RUNALL = Bool(ProcessInfo.processInfo.environment["RUNALL", default: "false"]) ?? false

@Suite struct FlowerFieldTests {

@Test("no rows")
func testNoRows() {
let input = [String]()
let output = [String]()
#expect(Board(input).transform() == output)
}

@Test("no columns", .enabled(if: RUNALL))
func testNoColumns() {
let input = [""]
let output = [""]
#expect(Board(input).transform() == output)
}

@Test("no flowers", .enabled(if: RUNALL))
func testNoFlowers() {
let input = [" ", " ", " "]
let output = [" ", " ", " "]
#expect(Board(input).transform() == output)
}

@Test("garden full of flowers", .enabled(if: RUNALL))
func testGardenFullOfFlowers() {
let input = ["***", "***", "***"]
let output = ["***", "***", "***"]
#expect(Board(input).transform() == output)
}

@Test("flower surrounded by spaces", .enabled(if: RUNALL))
func testFlowerSurroundedBySpaces() {
let input = [" ", " * ", " "]
let output = ["111", "1*1", "111"]
#expect(Board(input).transform() == output)
}

@Test("space surrounded by flowers", .enabled(if: RUNALL))
func testSpaceSurroundedByFlowers() {
let input = ["***", "* *", "***"]
let output = ["***", "*8*", "***"]
#expect(Board(input).transform() == output)
}

@Test("horizontal line", .enabled(if: RUNALL))
func testHorizontalLine() {
let input = [" * * "]
let output = ["1*2*1"]
#expect(Board(input).transform() == output)
}

@Test("horizontal line, flowers at edges", .enabled(if: RUNALL))
func testHorizontalLineFlowersAtEdges() {
let input = ["* *"]
let output = ["*1 1*"]
#expect(Board(input).transform() == output)
}

@Test("vertical line", .enabled(if: RUNALL))
func testVerticalLine() {
let input = [" ", "*", " ", "*", " "]
let output = ["1", "*", "2", "*", "1"]
#expect(Board(input).transform() == output)
}

@Test("vertical line, flowers at edges", .enabled(if: RUNALL))
func testVerticalLineFlowersAtEdges() {
let input = ["*", " ", " ", " ", "*"]
let output = ["*", "1", " ", "1", "*"]
#expect(Board(input).transform() == output)
}

@Test("cross", .enabled(if: RUNALL))
func testCross() {
let input = [" * ", " * ", "*****", " * ", " * "]
let output = [" 2*2 ", "25*52", "*****", "25*52", " 2*2 "]
#expect(Board(input).transform() == output)
}

@Test("large garden", .enabled(if: RUNALL))
func testLargeGarden() {
let input = [" * * ", " * ", " * ", " * *", " * * ", " "]
let output = ["1*22*1", "12*322", " 123*2", "112*4*", "1*22*2", "111111"]
#expect(Board(input).transform() == output)
}
}