Skip to content
Open
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
87 changes: 82 additions & 5 deletions Challenges/0002-Minesweeeper/Minesweeper.playground/Contents.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,96 @@ Safe squares are denoted by "." and mine squares by "*", both without the quotes
A matrix replacing all the "." characters by the number of mines adjacent to that square.
*/

enum MineSweeperSolverException: ErrorType {
case NoMineThere
case IncorrectMineBounds
}

func challenge_0002(n: Int, _ m: Int, _ field:[[String]]) -> [[String]] {
struct MineSweeperSolver {

let mine: [String]
let rows: Int
let columns: Int

init?(rows: Int, columns: Int, mine: [[String]]) {
// Checking preconditions
precondition(0 < rows)
precondition(columns <= 100)

<#Write here your solution#>
do {
self.rows = rows
self.columns = columns
self.mine = try MineSweeperSolver.loadMineSweeper(rows, columns: columns, mine: mine)
} catch {
print(error)
return nil
}
}

// Simpler array management
subscript(row: Int, column: Int) -> String {
get {
return mine[row * columns + column]
}
}

static func loadMineSweeper(rows: Int, columns: Int, mine: [[String]]) throws -> [String] {
do{
if !(mine.count == rows || mine.map({ $0.count == columns }).contains(false)) {
// Mine dimensions ain't right
throw MineSweeperSolverException.IncorrectMineBounds
}

return mine.reduce([], combine: +)
} catch {
throw error
}
}

// Check for "within-bounds" possible positions (Upper Left, Upper, Upper Right ...)
func checkForPossiblePostions(row r: Int, column c: Int) -> [(Int, Int)] {
return [(r-1, c-1), (r-1, c), (r-1, c+1), (r, c-1), (r, c+1), (r+1, c-1), (r+1, c), (r+1, c+1)].filter {
(let pos: (row: Int, column: Int)) -> Bool in

return pos.0 >= 0 && pos.0 < self.rows && pos.1 >= 0 && pos.1 < self.columns
}
}

// Checks mines inside into adjoining positions
func checkMineForPosition(row: Int, column: Int) -> Int {
return checkForPossiblePostions(row: row, column: column).reduce(0) {
(var sum: Int, position: (row: Int, column: Int)) -> Int in

if (self[position.row, position.column] == "*") { sum += 1 }

return sum
}
}

// Let's do some iterations !!
func solveMinesweeper() -> [[String]] {
var result: [[String]] = []

(0..<self.rows).forEach { row in
var tmpRow: [String] = []
(0..<self.columns).forEach { column in
let count = self[row, column] != "*" ? String(checkMineForPosition(row, column: column)) : "*"
tmpRow.append(count)
}
result.append(tmpRow)
}

return result
}
}

func challenge_0002(n: Int, _ m: Int, _ field:[[String]]) -> [[String]] {
return MineSweeperSolver.init(rows: n, columns: m, mine: field)!.solveMinesweeper()
}

// MARK: Checks


/*
let result_1 = challenge_0002(4, 4, [
["*", ".", ".", "."],
[".", ".", ".", "."],
Expand All @@ -62,8 +141,6 @@ let result_2 = challenge_0002(3, 5, [
assert(result_2[0] == ["*", "*", "1", "0", "0"])
assert(result_2[1] == ["3", "3", "2", "0", "0"])
assert(result_2[2] == ["1", "*", "1", "0", "0"])
*/

/*:
This problem appeared originally in programming-challenges.com
*/
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,19 @@
version = "3.0">
<TimelineItems>
<LoggerValueHistoryTimelineItem
documentLocation = "#CharacterRangeLen=0&amp;CharacterRangeLoc=1891&amp;EndingColumnNumber=18&amp;EndingLineNumber=0&amp;StartingColumnNumber=12&amp;StartingLineNumber=0&amp;Timestamp=476813376.273731"
documentLocation = "#CharacterRangeLen=0&amp;CharacterRangeLoc=4183&amp;EndingColumnNumber=18&amp;EndingLineNumber=7&amp;StartingColumnNumber=12&amp;StartingLineNumber=6&amp;Timestamp=479220843.122055"
lockedSize = "{1430, 52}"
selectedRepresentationIndex = "0"
shouldTrackSuperviewWidth = "NO">
</LoggerValueHistoryTimelineItem>
<LoggerValueHistoryTimelineItem
documentLocation = "#CharacterRangeLen=0&amp;CharacterRangeLoc=1891&amp;EndingColumnNumber=40&amp;EndingLineNumber=0&amp;StartingColumnNumber=1&amp;StartingLineNumber=0&amp;Timestamp=476813376.273912"
documentLocation = "#CharacterRangeLen=0&amp;CharacterRangeLoc=4183&amp;EndingColumnNumber=40&amp;EndingLineNumber=7&amp;StartingColumnNumber=1&amp;StartingLineNumber=6&amp;Timestamp=479220843.122251"
lockedSize = "{1438, 50}"
selectedRepresentationIndex = "0"
shouldTrackSuperviewWidth = "NO">
</LoggerValueHistoryTimelineItem>
<LoggerValueHistoryTimelineItem
documentLocation = "#CharacterRangeLen=0&amp;CharacterRangeLoc=1891&amp;EndingColumnNumber=41&amp;EndingLineNumber=0&amp;StartingColumnNumber=1&amp;StartingLineNumber=0&amp;Timestamp=476813376.274046"
documentLocation = "#CharacterRangeLen=0&amp;CharacterRangeLoc=4183&amp;EndingColumnNumber=41&amp;EndingLineNumber=7&amp;StartingColumnNumber=1&amp;StartingLineNumber=6&amp;Timestamp=479220843.122414"
lockedSize = "{1439, 50}"
selectedRepresentationIndex = "0"
shouldTrackSuperviewWidth = "NO">
Expand Down