-
Notifications
You must be signed in to change notification settings - Fork 3
/
SudokuSolution.swift
167 lines (136 loc) · 5.71 KB
/
SudokuSolution.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
//
// SudokuSolution.swift
// SudokuKit
//
// Created by Eneko Alonso on 12/22/19.
//
import Foundation
import Matrix
public struct SudokuSolution {
public static let allowedValues = (1...9).map(String.init)
let squareSide = 3
public private(set) var matrix: Matrix<[[String]]>
public enum Error: Swift.Error, LocalizedError {
case invalidCellSolution(value: String, index: Int)
case valueNotFound(value: String, index: Int)
public var errorDescription: String? {
switch self {
case .invalidCellSolution(let value, let index):
return "Error: failed to set \(value) at index \(index)"
case .valueNotFound(let value, let index):
return "Error: value \(value) not found at index \(index)"
}
}
}
// MARK: Initialization
public init(puzzle: SudokuPuzzle) throws {
let values = (1...puzzle.configuration.cellCount).map { _ in Self.allowedValues }
matrix = Matrix(values: values, columns: puzzle.configuration.columns)
try setCellsFromPuzzle(puzzle: puzzle)
}
mutating func setCellsFromPuzzle(puzzle: SudokuPuzzle) throws {
for index in matrix.cellRange {
guard let value = puzzle.matrix.cells[index] else {
continue
}
print("Set initial value \(value) at index \(index)")
try setCell(value: value, at: index)
}
}
// MARK: Solved Puzzle
public var isIncomplete: Bool {
return matrix.cells.contains(where: { $0.count > 1 })
}
public var solvedPuzzle: SudokuPuzzle? {
return try? SudokuPuzzle(values: matrix.cells.map { $0[0] })
}
// MARK: Value modifiers
mutating func setCell(value: String, at index: Int) throws {
// print("BEGIN Set value \(value) at index \(index) [\(column(for: index)),\(row(for: index))]")
matrix.cells[index] = [value]
print(renderTable(highlight: index))
try remove(value: value, fromColumnWithCellIndex: index)
try remove(value: value, fromRowWithCellIndex: index)
try remove(value: value, fromSquareWithCellIndex: index)
// print("END Set value \(value) at index \(index) [\(column(for: index)),\(row(for: index))]")
}
mutating func remove(value: String, fromColumnWithCellIndex index: Int) throws {
for cellIndex in matrix.cellRange where cellIndex != index && matrix.columnIndex(forCell: cellIndex) == matrix.columnIndex(forCell: index) {
try remove(value: value, fromCell: cellIndex)
}
}
mutating func remove(value: String, fromRowWithCellIndex index: Int) throws {
for cellIndex in matrix.cellRange where cellIndex != index && matrix.rowIndex(forCell: cellIndex) == matrix.rowIndex(forCell: index) {
try remove(value: value, fromCell: cellIndex)
}
}
mutating func remove(value: String, fromSquareWithCellIndex index: Int) throws {
for cellIndex in matrix.cellRange where cellIndex != index && squareIndex(forCell: cellIndex) == squareIndex(forCell: index) {
try remove(value: value, fromCell: cellIndex)
}
}
mutating func remove(value: String, fromCell index: Int) throws {
guard matrix.cells[index].contains(value) else {
return
}
matrix.cells[index].removeAll { $0 == value }
guard matrix.cells[index].isEmpty == false else {
print(renderTable())
throw Error.invalidCellSolution(value: value, index: index)
}
if matrix.cells[index].count == 1 {
// Recursively remove candidates when only one value is left
let value = matrix.cells[index][0]
print("Set confirmed cell with value \(value) at index \(index)")
try setCell(value: value, at: index)
}
}
// MARK: Value fetchers
var columns: [[[String]]] {
return matrix.allColumns
}
var rows: [[[String]]] {
return matrix.allRows
}
var squares: [[[String]]] {
return (0...8).map {
let index = cellIndex(forSquare: $0)
return matrix.subMatrix(columns: squareSide, rows: squareSide, fromCell: index).cells
}
}
// MARK: Index calculations
func squareIndex(forCell index: Int) -> Int {
let squareColumn = (index % matrix.columns) / squareSide
let squareRow = (index / matrix.rows) / squareSide
return squareSide * squareRow + squareColumn
}
func columnIndex(forSquare index: Int) -> Int {
return matrix.columnIndex(forCell: cellIndex(forSquare: index))
}
func rowIndex(forSquare index: Int) -> Int {
return matrix.rowIndex(forCell: cellIndex(forSquare: index))
}
func cellIndex(forSquare index: Int) -> Int {
let row = index / squareSide
let column = index % squareSide
return squareSide * matrix.columns * row + squareSide * column
}
func cellIndex(forSquare index: Int, offset: Int) -> Int {
let row = offset / squareSide
let column = offset % squareSide
return cellIndex(forSquare: index) + row * matrix.columns + column
}
func cellIndices(forSquare index: Int) -> [Int] {
let squareCells = squareSide*squareSide
return (0..<squareCells).map { self.cellIndex(forSquare: index, offset: $0)}
}
func cellIndex(forRow row: Int, column: Int) -> Int {
return row * matrix.columns + column
}
func cellIndices(forRow row: Int) -> [Int] {
return (0..<matrix.columns).map { self.cellIndex(forRow: row, column: $0) }
}
func cellIndices(forColumn column: Int) -> [Int] {
return (0..<matrix.rows).map { self.cellIndex(forRow: $0, column: column) }
}
}