-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsudokusolver-logic.js
526 lines (446 loc) · 12.9 KB
/
sudokusolver-logic.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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
////
// Author: Johan Beronius, [email protected], http://www.athega.se/, 2009
// License: Use however you want, just credit the author :-)
////
Array.implement({
hasTrueDuplicates: function() {
var l = this.length;
for (var y=0; y<l; y++) {
for (var x=l-1; x>0; x--) {
if (x<=y) break;
if (!this[x]) continue;
if (this[x]==this[y]) return true;
}
}
return false;
},
eraseAll: function(array) {
for (var i = array.length; i--; i) {
this.erase(array[i]);
}
return this;
}
});
var SudokuGrid = new Class({
initialize: function() {
this.count = 0;
this.options = [];
for (var ry=0; ry<3; ry++) {
this[ry] = [];
this.options[ry] = [];
for (var rx=0; rx<3; rx++) {
this[ry][rx] = [];
this.options[ry][rx] = [];
for (var y=0; y<3; y++) {
this[ry][rx][y] = [];
this.options[ry][rx][y] = [];
for (var x=0; x<3; x++) {
this.options[ry][rx][y][x] = [1,2,3,4,5,6,7,8,9];
}
}
}
}
},
getRowArray: function(ry, y) {
return (
this[ry][0][y].concat(
this[ry][1][y],
this[ry][2][y]
));
},
getColumnArray: function(rx, x) {
return [
this[0][rx][0][x],
this[0][rx][1][x],
this[0][rx][2][x],
this[1][rx][0][x],
this[1][rx][1][x],
this[1][rx][2][x],
this[2][rx][0][x],
this[2][rx][1][x],
this[2][rx][2][x]
];
},
getRegionArray: function(rx, ry) {
return (
this[ry][rx][0].concat(
this[ry][rx][1],
this[ry][rx][2]
));
},
getOptions: function(rx, ry, x, y) {
var row = this.getRowArray(ry, y);
var column = this.getColumnArray(rx, x);
var region = this.getRegionArray(rx, ry);
var other = row.concat(column).concat(region);
var options = [1,2,3,4,5,6,7,8,9].eraseAll(other);
return options;
},
calcOptions: function() {
// Pre-calc possible options for all positions
this.count = 0;
for (var ry=0; ry<3; ry++) {
for (var rx=0; rx<3; rx++) {
for (var y=0; y<3; y++) {
for (var x=0; x<3; x++) {
if (this[ry][rx][y][x]) this.count++;
this.options[ry][rx][y][x] = this.getOptions(rx, ry, x, y);
}
}
}
}
},
setNumber: function(rx, ry, x, y, n) {
this[ry][rx][y][x] = n;
this.count++;
// Remove this option from related column
for (var iry=0; iry<3; iry++) {
for (var iy=0; iy<3; iy++) {
this.options[iry][rx][iy][x].erase(n);
}
}
// Remove this option from related row
for (var irx=0; irx<3; irx++) {
for (var ix=0; ix<3; ix++) {
this.options[ry][irx][y][ix].erase(n);
}
}
// Remove this option from related region
for (var iy=0; iy<3; iy++) {
for (var ix=0; ix<3; ix++) {
this.options[ry][rx][iy][ix].erase(n);
}
}
}
});
var SudokuData = new Class({
initialize: function() {
this.grid = new SudokuGrid();
this.solution;
this.queue = [];
this.stats = {};
},
load: function(inputs) {
var i=0;
for (var ry=0; ry<3; ry++) {
for (var rx=0; rx<3; rx++) {
for (var y=0; y<3; y++) {
for (var x=0; x<3; x++) {
var v = inputs[i++].value;
this.grid[ry][rx][y][x] = v ? parseInt(v) : '';
}
}
}
}
},
update: function(inputs) {
var i=0;
for (var ry=0; ry<3; ry++) {
for (var rx=0; rx<3; rx++) {
for (var y=0; y<3; y++) {
for (var x=0; x<3; x++) {
inputs[i++].value = this.grid[ry][rx][y][x];
}
}
}
}
},
base64chars: "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_",
encode: function() {
var string = '';
var count = 0;
var bits = 0;
for (var ry=0; ry<3; ry++) {
for (var rx=0; rx<3; rx++) {
for (var y=0; y<3; y++) {
for (var x=0; x<3; x++) {
var v = this.grid[ry][rx][y][x];
// Encode 1 bit as a flag if there is a value or not
bits <<= 1;
if (v) bits++;
count++;
if (count == 6) { // Flush if we got 6 bits
string += this.base64chars.charAt(bits);
bits = 0;
count = 0;
}
// If there is a value, encode it in 4 bits
if (!v) continue;
for (var i=0; i<4; i++) {
bits <<= 1;
if (v & 8) bits++;
v <<= 1;
count++;
if (count == 6) { // Flush if we got 6 bits
string += this.base64chars.charAt(bits);
bits = 0;
count = 0;
}
}
}
}
}
}
if (count > 0) { // Flush the last 1-5 bits
bits <<= (6-count);
string += this.base64chars.charAt(bits);
}
return string;
},
decode: function(string) {
var values = [];
var value = 0;
var bitcount = 0;
for (var i=0; i<string.length; i++) {
var bits = this.base64chars.indexOf(string.charAt(i));
if (bits == -1) return false; // Illegal character detected
for (var b=0; b<6; b++) {
var bit = bits & 32 ? 1 : 0;
bits <<= 1;
if (bit == 0 && bitcount == 0) { // Store a 0 and continue
values.push(0);
} else if (bit == 1 && bitcount == 0) { // Skip this bit and accumulate next 4
bitcount = 4;
} else if (bitcount > 0) { // If we got bits left
value <<= 1;
value += bit;
if (--bitcount == 0) { // Store value after 4 bits
if (value > 9) return false; // To high detected
values.push(value);
value = 0;
}
}
}
}
var i=0;
for (var ry=0; ry<3; ry++) {
for (var rx=0; rx<3; rx++) {
for (var y=0; y<3; y++) {
for (var x=0; x<3; x++) {
var v = values[i++];
this.grid[ry][rx][y][x] = v ? v : '';
}
}
}
}
return true;
},
count: function() {
var c=0;
for (var ry=0; ry<3; ry++) {
for (var rx=0; rx<3; rx++) {
for (var y=0; y<3; y++) {
for (var x=0; x<3; x++) {
if (this.grid[ry][rx][y][x]) c++;
}
}
}
}
return c;
},
validate: function() {
for (var ry=0; ry<3; ry++) {
for (var y=0; y<3; y++) {
var row = this.grid.getRowArray(ry, y);
if (row.hasTrueDuplicates()) return this.valid = false;
}
}
for (var rx=0; rx<3; rx++) {
for (var x=0; x<3; x++) {
var column = this.grid.getColumnArray(rx, x);
if (column.hasTrueDuplicates()) return this.valid = false;
}
}
for (var ry=0; ry<3; ry++) {
for (var rx=0; rx<3; rx++) {
var region = this.grid.getRegionArray(rx, ry);
if (region.hasTrueDuplicates()) return this.valid = false;
}
}
return this.valid = true;
},
solve: function() {
this.stats.solutionsFound = 0;
this.stats.branches = 0;
this.stats.iterations = 0;
this.stats.unsolvable = 0;
this.stats.solvedByNumberExclusion = 0;
this.stats.solvedByPositionExclusion = 0;
this.stats.solvedByBranching = 0;
this.stats.maxQueueLength = 0;
this.stats.processingTime = 0;
this.grid.calcOptions();
this.queue.push(this.grid);
this.workQueue();
},
workQueue: function() {
var startTime = new Date().getTime();
var runTime;
while (this.queue.length > 0) {
if (this.stats.maxQueueLength < this.queue.length)
this.stats.maxQueueLength = this.queue.length;
this.grid = this.queue.pop();
if (this.solveBranch()) {
this.solution = this.grid;
this.stats.solutionsFound++;
}
runTime = new Date().getTime() - startTime;
if (runTime > 100) {
if (this.solution)
this.grid = this.solution;
this.stats.processingTime += runTime;
this.updateCallback();
return this.workQueueTimeoutId = this.workQueue.delay(20, this);
}
}
this.stats.processingTime += runTime;
this.updateCallback();
this.doneCallback();
},
stop: function() {
this.queue.empty();
$clear(this.workQueueTimeoutId);
},
solveBranch: function() {
this.stats.branches++;
// Iterate over grid until no more numbers can be solved.
do {
var solvedThisIteration=0;
this.stats.iterations++;
// Search for possible positions for each missing number in every row
for (var ry=0; ry<3; ry++) {
for (var y=0; y<3; y++) {
var row = this.grid.getRowArray(ry, y);
var missingNumbers = [1,2,3,4,5,6,7,8,9].eraseAll(row);
rowNumbers: for (var i=0; i<missingNumbers.length; i++) {
var n = missingNumbers[i];
var possiblePosition = undefined;
for (var rx=0; rx<3; rx++) {
for (var x=0; x<3; x++) {
if (this.grid[ry][rx][y][x]) continue;
var options = this.grid.options[ry][rx][y][x];
if (options.contains(n)) { // The number can exist at this position
if (possiblePosition) continue rowNumbers; // More than one possible position found
possiblePosition = {rx: rx, x: x};
}
}
}
if (!possiblePosition) { // Found a dead end with no possible positions.
this.stats.unsolvable++;
return false;
}
solvedThisIteration++;
this.stats.solvedByPositionExclusion++;
this.grid.setNumber(possiblePosition.rx, ry, possiblePosition.x, y, n);
if (this.grid.count == 81) return true; // We have a solution
}
}
}
// Search for possible positions for each missing number in every column
for (var rx=0; rx<3; rx++) {
for (var x=0; x<3; x++) {
var column = this.grid.getColumnArray(rx, x);
var missingNumbers = [1,2,3,4,5,6,7,8,9].eraseAll(column);
columnNumbers: for (var i=0; i<missingNumbers.length; i++) {
var n = missingNumbers[i];
var possiblePosition = undefined;
for (var ry=0; ry<3; ry++) {
for (var y=0; y<3; y++) {
if (this.grid[ry][rx][y][x]) continue;
var options = this.grid.options[ry][rx][y][x];
if (options.contains(n)) { // The number can exist at this position
if (possiblePosition) continue columnNumbers; // More than one possible position found
possiblePosition = {ry: ry, y: y};
}
}
}
if (!possiblePosition) { // Found a dead end with no possible positions.
this.stats.unsolvable++;
return false;
}
solvedThisIteration++;
this.stats.solvedByPositionExclusion++;
this.grid.setNumber(rx, possiblePosition.ry, x, possiblePosition.y, n);
if (this.grid.count == 81) return true; // We have a solution
}
}
}
// Search for possible positions for each missing number in every region
for (var ry=0; ry<3; ry++) {
for (var rx=0; rx<3; rx++) {
var region = this.grid.getRegionArray(rx, ry);
var missingNumbers = [1,2,3,4,5,6,7,8,9].eraseAll(region);
regionNumbers: for (var i=0; i<missingNumbers.length; i++) {
var n = missingNumbers[i];
var possiblePosition = undefined;
for (var y=0; y<3; y++) {
for (var x=0; x<3; x++) {
if (this.grid[ry][rx][y][x]) continue;
var options = this.grid.options[ry][rx][y][x];
if (options.contains(n)) { // The number can exist at this position
if (possiblePosition) continue regionNumbers; // More than one possible position found
possiblePosition = {x: x, y: y};
}
}
}
if (!possiblePosition) { // Found a dead end with no possible positions.
this.stats.unsolvable++;
return false;
}
solvedThisIteration++;
this.stats.solvedByPositionExclusion++;
this.grid.setNumber(rx, ry, possiblePosition.x, possiblePosition.y, n);
if (this.grid.count == 81) return true; // We have a solution
}
}
}
// Fill in numbers for each position that can be determined without ambiguity.
for (var ry=0; ry<3; ry++) {
for (var rx=0; rx<3; rx++) {
for (var y=0; y<3; y++) {
for (var x=0; x<3; x++) {
if (this.grid[ry][rx][y][x]) continue;
var options = this.grid.options[ry][rx][y][x];
if (options.length == 0) { // Found a dead end with no options left.
this.stats.unsolvable++;
return false;
}
if (options.length != 1) continue; // More than one option left, skip for now.
solvedThisIteration++;
this.stats.solvedByNumberExclusion++;
this.grid.setNumber(rx, ry, x, y, options[0]);
if (this.grid.count == 81) return true; // We have a solution
}
}
}
}
} while (solvedThisIteration > 0);
// We have a solution
if (this.grid.count == 81) return true;
// No solution yet but we're stuck.
// Find options for first empty slot, clone alternative branchs and push on queue.
for (var v=2; v<=9; v++) {
for (var ry=0; ry<3; ry++) {
for (var rx=0; rx<3; rx++) {
for (var y=0; y<3; y++) {
for (var x=0; x<3; x++) {
if (this.grid[ry][rx][y][x]) continue;
var options = this.grid.options[ry][rx][y][x];
if (options.length == 0) return false; // Should not happen, dead ends should be trapped by earlier iteration.
if (options.length == 1) return false; // Should not happen, unambiguous options should be set by earlier iteration.
if (options.length != v) continue; // Continue if this is not the box with the least number of options.
this.stats.solvedByBranching += options.length;
options.each(function(option) {
var branch = $unlink(this.grid);
branch.setNumber(rx, ry, x, y, option);
this.queue.push(branch);
}, this);
return false;
}
}
}
}
}
return false;
}
});