-
Notifications
You must be signed in to change notification settings - Fork 0
/
sudoku.js
295 lines (246 loc) · 6.24 KB
/
sudoku.js
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
class SudokuSolver {
static SIZE = 9
static gridConstraint(row, col, n) {
// Each grid can only have the number 1-9 appearing once.
const r = Math.floor(row / 3)
const c = Math.floor(col / 3)
return (r * 3 + c) * 9 + n - 1
}
static rowConstraint(row, n) {
return row * 9 + n - 1
}
static columnConstraint(col, n) {
return col * 9 + n - 1
}
static cellConstraint(row, col) {
return row * 9 + col
}
static buildConstraintMatrix(row, col, n) {
const constraints = [
SudokuSolver.gridConstraint(row, col, n),
SudokuSolver.rowConstraint(row, n),
SudokuSolver.columnConstraint(col, n),
SudokuSolver.cellConstraint(row, col)
]
return constraints.map(constraint => {
const matrix = Array(9 * 9).fill(0)
matrix[constraint] = 1
return matrix
}).reduce((acc, mat) => acc.concat(mat), [])
}
static parse(sudoku = []) {
const matrix = []
const metadata = []
for (let row = 0; row < 9; row += 1) {
for (let col = 0; col < 9; col += 1) {
const n = sudoku[row][col]
// The value ranges from 1-9. 0 means that it is unset.
if (n === 0) {
for (let n = 1; n < 10; n += 1) {
// For each number n that is not in the current row, fill them up.
if (!sudoku[row].includes(n)) {
const m = SudokuSolver.buildConstraintMatrix(row, col, n)
matrix.push(m)
metadata.push({
row,
col,
n
})
}
}
} else {
const m = SudokuSolver.buildConstraintMatrix(row, col, n)
matrix.push(m)
metadata.push({
row,
col,
n
})
}
}
}
return {
matrix,
metadata
}
}
}
class DataObject {
constructor(column, row) {
this.up = this
this.down = this
this.left = this
this.right = this
this.column = column
this.row = row
}
toString() {
return `${this.column.name}:${this.row}`
}
}
class ColumnObject extends DataObject {
constructor(name = '', size = 0) {
super(null, null)
this.column = this
this.name = name
this.size = size
}
}
class DLX {
static ALPHABETS = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'.split()
static solve(A) {
const torodial = DLX.initTorodial(A)
return DLX.search(torodial)
}
static initColumnLabels(A = [
[]
]) {
const cols = (A && A[0].length) || 0
if (cols < 26) {
return DLX.ALPHABETS.slice(0, cols)
}
return Array(cols).fill(0).map((_, n) => n.toString())
}
static initColumnHeader(A) {
const cols = (A && A[0].length) || 0
const labels = DLX.initColumnLabels(A)
// The root header links all the column labels.
const root = new ColumnObject('root', Infinity)
let curr = root
for (let col = 0; col < cols; col += 1) {
curr.right = new ColumnObject(labels[col])
curr.right.left = curr
curr = curr.right
}
curr.right = root
curr.right.left = curr
return root
}
static headerPointer(root) {
const header = {}
for (let curr = root.right; curr != root; curr = curr.right) {
header[curr.name] = curr
}
return header
}
static smallestColumnObject(root) {
let smallest = root
for (let curr = root.column.right; curr != root; curr = curr.right) {
if (curr.size < smallest.size) {
smallest = curr
}
}
return smallest
}
static initTorodial(A) {
const labels = DLX.initColumnLabels(A)
const root = DLX.initColumnHeader(A)
const header = DLX.headerPointer(root)
for (let i = 0; i < A.length; i += 1) {
const row = A[i]
let prev = null
let left = null
for (let j = 0; j < row.length; j += 1) {
const col = row[j]
if (col !== 1) continue
const head = header[labels[j]]
head.size += 1
let curr = head.up
curr.down = new DataObject(head, i)
curr.down.up = curr
curr = curr.down
curr.down = curr.column
curr.down.up = curr
if (!prev) {
prev = curr
prev.right = curr
prev.right.left = prev
left = curr
} else {
prev.right = curr
prev.right.left = prev
prev = curr
}
}
if (prev) {
prev.right = left
prev.right.left = prev
}
}
return root
}
static cover(col) {
col = col.column
col.right.left = col.left
col.left.right = col.right
for (let i = col.down; i != col; i = i.down) {
for (let j = i.right; j != i; j = j.right) {
j.up.down = j.down
j.down.up = j.up
j.column.size -= 1
}
}
}
static uncover(col) {
col = col.column
for (let i = col.up; i != col; i = i.up) {
for (let j = i.left; j != i; j = j.left) {
j.column.size += 1
j.up.down = j
j.down.up = j
}
}
col.right.left = col
col.left.right = col
}
static search(root, k = 0, solution = []) {
if (root.right === root) {
return [...solution]
}
let col = DLX.smallestColumnObject(root)
DLX.cover(col)
for (let r = col.down; r != col; r = r.down) {
let res = r
solution.push(res.row)
for (let j = r.right; j != r; j = j.right) {
DLX.cover(j)
}
const result = DLX.search(root, k + 1, solution)
if (result) return result
solution.pop()
r = res
col = r.column
for (let j = r.left; j != r; j = j.left) {
DLX.uncover(j)
}
}
DLX.uncover(col)
}
}
// assert(DLX.initColumnLabels([[1,2,3]]) === ['a', 'b', 'c'])
const sudoku = [
[8, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 3, 6, 0, 0, 0, 0, 0],
[0, 7, 0, 0, 9, 0, 2, 0, 0],
[0, 5, 0, 0, 0, 7, 0, 0, 0],
[0, 0, 0, 0, 4, 5, 7, 0, 0],
[0, 0, 0, 1, 0, 0, 0, 3, 0],
[0, 0, 1, 0, 0, 0, 0, 6, 8],
[0, 0, 8, 5, 0, 0, 0, 1, 0],
[0, 9, 0, 0, 0, 0, 4, 0, 0]
]
const {
matrix,
metadata
} = SudokuSolver.parse(sudoku)
const rows = DLX.solve(matrix)
const solution = Array(9).fill(() => Array(9).fill(0)).map(fn => fn())
for (let r of rows) {
const {
row,
col,
n
} = metadata[r]
solution[row][col] = n
}
console.log(solution)