-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsolve.js
622 lines (525 loc) · 15.5 KB
/
solve.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
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
var generate = require("./generate.js");
var genArray = generate.genArray;
var colCheck = generate.colCheck;
var squCheck = generate.squCheck;
var genPseudoku = generate.genPseudoku;
var visPseudoku = generate.visPseudoku;
//a function to check if all integers from 1 to n appear in a single row of a 2d array where n is the number of columns of the 2d array
function singleRowCheck(arr,row) {
var numCols = arr[row].length;
// first we have a loop to check for all integers from 1 to the length of the row
for (var i = 1; i <= numCols; i++) {
// this variable is going to count the number of times the integer i is in the row; if it is ever greater than 1 then we will return false, if it equal to zero after checking all columns then we return false
var count = 0;
// this loop is going to SEARCH every column for the integer i
for (var j = 0; j < numCols; j++) {
if (arr[row][j] == i) {
count++;
}
if (count > 1) {
return false;
}
}
// this is to check if the integer is in the row at all; if it is not, then count == 0 and we will return false
if (count == 0) {
return false;
}
}
// so if it has gone through all of the checks and passed we return true;
return true;
}
// this takes two numbers n and len and returns an array of length len which is the representation of number n in base 4 with as many zeroes at the beginning as necessary
// be careful that len is as big as it needs to be to print n in full
function conversion(n,len) {
var con = [];
while (Math.floor(n/4) != 0) {
con.push(n % 4);
n = Math.floor(n/4);
}
con.push(n % 4);
// the array con is in the wrong order, so we will create a new array which gives us what we want in the right order
var out = [];
for (var i = con.length - 1; i >= 0; i--) {
out.push(con[i]);
con.pop();
}
// this will add extra zeroes at the beginning of the array so that conversion is at length len
while (out.length < len) {
out.splice(0,0,0);
}
return out;
}
// WRITE YOUR CODE INTO THE BODY OF THESE FUNCTIONS TO GET THEM WORKING
function rowCheck(array) {
// this function should return a Boolean
var numRows = array.length;
var count = 0;
for (let i = 0; i < numRows; i++) {
if (singleRowCheck(array, i)) {
count++;
}
}
return (count == numRows) ? true : false;
}
function blankEntries(array) {
// this function should return an array
var numRows = array.length;
var numCols = array[0].length;
var blank = [];
for (let i = 0; i < numRows; i++) {
for (let j = 0; j < numCols; j++) {
if (array[i][j] == " ") {
blank.push([i,j]);
}
}
}
return blank;
}
function makeCandidate(n,len) {
// this function should return an array of integers between 1 and 4 of length len
var candidate = conversion(n, len);
for (let i = 0; i < candidate.length; i++) {
candidate[i] += 1;
}
return candidate;
}
function checkCandidate(array,candidate) {
// this function should return a Boolean saying whether a candidate assignment of numbers satisfies the Pseudoku conditions
var blank = blankEntries(array);
var numBlank = blank.length;
for (let i = 0; i < numBlank; i++) {
array[blank[i][0]][blank[i][1]] = candidate[i];
}
if (rowCheck(array) && colCheck(array) && squCheck(array)) {
return true;
}
for (let i = 0; i < numBlank; i++) {
array[blank[i][0]][blank[i][1]] = " ";
}
return false;
}
function solvePseudoku(array) {
// this returns an array which is the completed Pseudoku puzzle
var numBlank = blankEntries(array).length;
for (let i = 0; i < 4 ** numBlank; i++) {
if (checkCandidate(array, makeCandidate(i, numBlank))) {
return array;
}
}
return "No solution!";
}
// WRITE YOUR TESTING CODE BELOW HERE
// Task 12
var row = [1, 2, 3, 4];
// var arr = genArray(row);
// var sudoku0 = genPseudoku(row,5);
// console.log(rowCheck(arr));
// console.log(rowCheck(sudoku0));
// // Task 13
// var sudoku1 = genPseudoku(row,5);
// var sudoku2 = [[1, " ", " ", 4],[3, 4, 1, " "],[2, 3, 4, " "],[" ", 1, 2, 3]];
// console.log(visPseudoku(sudoku1));
// console.log(blankEntries(sudoku1));
// console.log(visPseudoku(sudoku2));
// console.log(blankEntries(sudoku2));
// // Task 14
// console.log(makeCandidate(5,5));
// console.log(makeCandidate(12,4));
// // Task 15
// var candidate1 = [1, 1, 1, 1, 1];
// var candidate2 = [2, 3, 2, 1, 4];
// console.log(checkCandidate(sudoku1,candidate1));
// console.log(sudoku1);
// console.log(checkCandidate(sudoku2,candidate1));
// console.log(sudoku2);
// console.log(checkCandidate(sudoku2,candidate2));
// console.log(sudoku2);
// // Task 16
var sudoku3 = genPseudoku(row,7);
// console.log(solvePseudoku(sudoku3));
// var sudoku4 = genPseudoku(row,10);
// console.log(solvePseudoku(sudoku4));
// // Task 17 Question 1
// var arr1 = genPseudoku([2,3,4,1],8);
// console.log(visPseudoku(arr1));
// var arr2 = genPseudoku([4,2,3,1],10);
// console.log(visPseudoku(arr2));
// console.log(visPseudoku(solvePseudoku(arr1)));
// console.log(visPseudoku(solvePseudoku(arr2)));
// Task 18 Question 2
function solvePseudoku18(array) {
// this returns an array which is the completed Pseudoku puzzle
function singleRowCyclicPerm(array) {
// single row cyclic permutation
for (let i = 0; i < 1; i++) {
array.push(array.shift());
}
return array;
}
function generateOptions(array) {
// generate options for blank entries
var options = [];
for (let i = 1; i <= array.length; i++) {
options.push(i);
}
return options;
}
function generateBlankRows(array,blank) {
// generate options for blank entries by row/column
var blankArr = [];
for (let i = 0; i < array.length; i++) {
blankArr.push([]);
for (let j = 0; j < blank.length; j++) {
if (blank[j][0] == i) {
blankArr[blankArr.length - 1].push(blank[j]);
}
}
}
return blankArr;
}
function generateBlankCols(array,blank) {
// generate options for blank entries by row/column
var blankArr = [];
for (let i = 0; i < array.length; i++) {
blankArr.push([]);
for (let j = 0; j < blank.length; j++) {
if (blank[j][1] == i) {
blankArr[blankArr.length - 1].push(blank[j]);
}
}
}
return blankArr;
}
function countSqus(array) {
// count squares per row/column
return array.length ** 0.5;
}
function generateBlankSqus(array,blank) {
// generate options for blank entries by square
var blankSqus = [];
var squs = countSqus(array);
for (let i = 0; i < squs; i++) {
for (let j = 0; j < squs; j++) {
let x1 = i*squs;
let y1 = j*squs;
let x2 = i*squs + squs - 1;
let y2 = j*squs + squs - 1;
blankSqus.push([]);
for (let k = 0; k < blank.length; k++) {
if (blank[k][0] >= x1 && blank[k][0] <= x2 &&
blank[k][1] >= y1 && blank[k][1] <= y2) {
blankSqus[blankSqus.length - 1].push(blank[k]);
}
}
}
}
return blankSqus;
}
function generateCandidateRows(array,blank) {
// generate candidate entries per row
var candidate = [];
for (let i = 0; i < array.length; i++) {
let options = generateOptions(array);
for (let j = 0; j < array.length; j++) {
for (let k = 0; k < options.length; k++) {
if (array[i][j] == options[k]) {
options.splice(k, 1);
}
}
}
for (let j = 0; j < options.length; j++) {
candidate.push(options[j]);
}
}
for (let i = 0; i < blank.length; i++) {
candidate.push(candidate.splice(0, blank[i].length));
}
return candidate;
}
function generateCandidateCols(array,blank) {
// generate candidate entries per column
var candidate = [];
for (let i = 0; i < array.length; i++) {
let options = generateOptions(array);
for (let j = 0; j < array.length; j++) {
for (let k = 0; k < options.length; k++) {
if (array[j][i] == options[k]) {
options.splice(k, 1);
}
}
}
for (let j = 0; j < options.length; j++) {
candidate.push(options[j]);
}
}
for (let i = 0; i < blank.length; i++) {
candidate.push(candidate.splice(0, blank[i].length));
}
return candidate;
}
function generateCandidateSqus(array,blank) {
// generate candidate entries per square
var squs = countSqus(array);
var candidate = [];
for(var i = 0; i < squs; i++) {
for(var j = 0; j < squs; j++) {
var options = generateOptions(array);
let x1 = i*squs;
let y1 = j*squs;
let x2 = i*squs + squs - 1;
let y2 = j*squs + squs - 1;
for (var k = y1; k <= y2; k++) {
for (var l = x1; l <= x2; l++) {
for(var m = 0; m < options.length; m++) {
if(array[l][k] == options[m]) {
options.splice(m,1);
}
}
}
}
for(var k = 0; k < options.length; k++) {
candidate.push(options[k]);
}
}
}
for(var i = 0; i < blank.length; i++) {
candidate.push(candidate.splice(0, blank[i].length));
}
return candidate;
}
function generateCombs(array) {
// generate non-exhaustive combinations
var combs = [];
for (let i = 0; i < array.length; i++) {
let levels = [];
let ind = 0;
let numCombs = 1;
let arrayCopy = array[i].slice();
while (ind < array[i].length) {
levels.unshift(numCombs *= ++ind);
}
combs.push([]);
for (var j = 0; j < levels.length; j++) {
for (var k = 0; k < levels[0]; k++) {
if (j == 0) {
combs[i].push(new Array(arrayCopy.length));
}
if (k % levels[j+1] == 0 && k > 0) {
singleRowCyclicPerm(arrayCopy);
}
var count = 0;
for (var l = 0; l < j; l++) {
for (var m = 0; m < j; m++) {
if (combs[i][k][m] == arrayCopy[j]) {
singleRowCyclicPerm(arrayCopy);
count++;
break;
}
}
if (l == count) {
break;
}
}
combs[i][k][j] = arrayCopy[j];
}
}
}
return combs;
}
function checkCols(blankRows,blankCols,rowCombs,colCombs) {
// check rows and columns
var spliced;
var matchRow;
var slicedRow;
for(let k = 0; k < rowCombs.length; k++) {
if(spliced && k > 0) {
slicedRow = true;
k--;
}
for(let l = 0; l < rowCombs[k].length; l++) {
if (slicedRow) {
slicedRow = false;
l = rowCombs[k].length - 1;
} else if (spliced && l > 0) {
l--;
}
for(let t = 0; t < blankRows[k].length; t++) {
if(spliced) {
spliced = false;
t = 0;
}
matchRow = false;
for(let d = 0; d < blankCols[blankRows[k][t][1]].length; d++) {
if(blankRows[k][t] == blankCols[blankRows[k][t][1]][d]) {
for(let b = 0; b < colCombs[blankRows[k][t][1]].length; b++){
if(rowCombs[k][l] &&
rowCombs[k][l][t] == colCombs[blankRows[k][t][1]][b][d]) {
matchRow = true;
break;
}
if(!matchRow &&
rowCombs[k][l] &&
rowCombs[k][l][t] != colCombs[blankRows[k][t][1]][b][d] &&
b == colCombs[blankRows[k][t][1]].length - 1) {
spliced = true;
rowCombs[k].splice(l,1);
}
}
}
}
}
}
}
return rowCombs;
}
function checkSqus(array,blankRows,blankSqus,rowCombs,squCombs) {
// check rows and squares
var squs = countSqus(array);
var squInd = 0;
var spliced;
var matchRow;
for(let a = 0; a < squs; a++) {
for(let b = 0; b < squs; b++) {
squInd++;
let x1 = a*squs;
let x2 = a*squs + squs - 1;
for (let j = x1; j <= x2; j++) {
for(let t = 0; t < blankRows[j].length; t++) {
if (spliced) {
spliced = false;
t = 0;
}
for (let l = 0; l < blankSqus[squInd - 1].length; l++) {
if(blankRows[j][t] == blankSqus[squInd - 1][l]) {
for(let c = 0; c < rowCombs[blankRows[j][t][0]].length; c++) {
matchRow = false;
for(let d = 0; d < squCombs[squInd - 1].length; d++) {
if(rowCombs[blankRows[j][t][0]][c][t] == squCombs[squInd - 1][d][l]) {
matchRow = true;
break;
}
if(!matchRow &&
rowCombs[blankRows[j][t][0]][c][t] != squCombs[squInd - 1][d][l] &&
d == squCombs[squInd - 1].length - 1) {
spliced = true;
rowCombs[blankRows[j][t][0]].splice(c,1);
}
}
}
}
}
}
}
}
}
return rowCombs;
}
function generateRowLen(rowCombs){
// generate row lengths
var rowLen = [];
for(var i = 0; i < rowCombs.length; i++) {
if(rowCombs[i].length == 0) {
rowLen.push(1);
} else {
rowLen.push(rowCombs[i].length);
}
}
return rowLen;
}
function countPerms(array,rowLen) {
// generate cyclic permutations count
var permsCount = [];
for(var i = 0; i < rowLen[0]; i++) {
permsCount.push(0);
for(var j = 0; j < rowLen[1]; j++) {
permsCount.push(1);
for(var k = 0; k < rowLen[2]; k++) {
permsCount.push(2);
for(var l = 0; l < rowLen[3]; l++) {
permsCount.push(3);
for(var l = 0; l < rowLen[4]; l++) {
permsCount.push(4);
for(var l = 0; l < rowLen[5]; l++) {
permsCount.push(5);
for(var l = 0; l < rowLen[6]; l++) {
permsCount.push(6);
for(var l = 0; l < rowLen[7]; l++) {
permsCount.push(7);
for(var l = 0; l < rowLen[8]; l++) {
permsCount.push(8);
}
}
}
}
}
}
}
}
}
return permsCount;
}
function generateSolution(rowCombs) {
// generate solution
var solution = [];
for(var i = 0; i < rowCombs.length; i++) {
if(rowCombs[i][0]){
for(var j = 0; j < rowCombs[i][0].length; j++) {
solution.push(rowCombs[i][0][j]);
}
}
}
return solution;
}
function solve(array,permsCount,rowCombs) {
// solve pseudoku/sudoku
for(var i = 0; i < permsCount.length; i++) {
var solution = generateSolution(rowCombs);
if(checkCandidate(array, solution)) {
return array;
}
if(rowCombs[permsCount[i]].length > 1) {
singleRowCyclicPerm(rowCombs[permsCount[i]]);
}
}
return "There is no solution!";
}
// generate blank entries
var blank = blankEntries(array);
// generate blank entries by row, column and square
var blankRows = generateBlankRows(array,blank);
var blankCols = generateBlankCols(array,blank);
var blankSqus = generateBlankSqus(array,blank);
// generate candidate entries for rows, columns and squares
var candidateRows = generateCandidateRows(array,blankRows);
var candidateCols = generateCandidateCols(array,blankCols);
var candidateSqus = generateCandidateSqus(array,blankSqus);
// generate non-exhaustive row combinations
var rowCombs = generateCombs(candidateRows);
// generate non-exhaustive column and square combinations
if(blank.length != array.length ** 2) {
var colCombs = generateCombs(candidateCols);
var squCombs = generateCombs(candidateSqus);
rowCombs = checkCols(blankRows,blankCols,rowCombs,colCombs);
rowCombs = checkSqus(array,blankRows,blankSqus,rowCombs,squCombs);
}
// generate cyclic permutations count per row
var permsCount = countPerms(array,generateRowLen(rowCombs));
// generate solved pseudoku
var solvedPseudoku = solve(array,permsCount,rowCombs);
// return solved pseudoku
return solvedPseudoku;
}
// console.log(sudoku3);
// console.log(solvePseudoku18(sudoku3));
// console.log(solvePseudoku18(sudoku4));
var sudoku5 = [[' ',' ',' ','1',' ',' ','9',' ',' '],
['6', ' ', ' ', ' ', ' ','9',' ',' ',' '],
[' ',' ','7',' ','4', '3', '8', ' ', ' '],
['5',' ',' ','6',' ', ' ', ' ', '3','4'],
['1','4',' ',' ','3','5',' ','6','8'],
[' ',' ','3','7',' ','4','2',' ',' '],
['3',' ','4',' ','9',' ','5',' ',' '],
[' ','2',' ',' ','8',' ',' ',' ','1'],
[' ',' ',' ',' ',' ','1',' ','9',' ']];
console.log(visPseudoku(sudoku5));
console.log(visPseudoku(solvePseudoku18(sudoku5)));