-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy path37.kkxujq.js
154 lines (125 loc) · 3.76 KB
/
37.kkxujq.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
/**
* @param {character[][]} board
* @return {void} Do not return anything, modify board in-place instead.
*/
var solveSudoku = function (board) {
const { floor } = Math;
const seeds = [1, 2, 3, 4, 5, 6, 7, 8, 9];
const row = {
0: new Set(),
1: new Set(),
2: new Set(),
3: new Set(),
4: new Set(),
5: new Set(),
6: new Set(),
7: new Set(),
8: new Set(),
};
const col = {
0: new Set(),
1: new Set(),
2: new Set(),
3: new Set(),
4: new Set(),
5: new Set(),
6: new Set(),
7: new Set(),
8: new Set(),
};
const cellGroup = {
'00': new Set(),
'01': new Set(),
'02': new Set(),
'10': new Set(),
'11': new Set(),
'12': new Set(),
'20': new Set(),
'21': new Set(),
'22': new Set(),
};
let solveSeeds = new Map();
for (let rowIndex = 0; rowIndex < 9; rowIndex++) {
for (let colIndex = 0; colIndex < 9; colIndex++) {
const cell = +board[rowIndex][colIndex];
const groupId = calcGroupId(rowIndex, colIndex);
if (Number.isInteger(cell)) {
row[rowIndex].add(cell);
col[colIndex].add(cell);
cellGroup[groupId].add(cell);
solveSeeds = clearSolveSeeds(rowIndex, colIndex, groupId, cell, solveSeeds);
} else {
const solveId = `${rowIndex}${colIndex}`;
const seeds = calcSeed(rowIndex, colIndex, groupId);
solveSeeds.set(solveId, new Set(seeds));
}
}
}
return clearSolves();
function clearSolves(solves = solveSeeds) {
let cache = null;
for (const [[row, col], seed] of solves) {
if (seed.size === 1) {
const cell = [...seed][0];
const groupId = calcGroupId(row, col);
board[row][col] = cell.toString();
solves.delete(`${row}${col}`);
cache = clearSolveSeeds(row, col, groupId, cell, solves);
}
}
if (cache) return clearSolves(cache);
if (solves.size) {
console.log(solves);
// return solveSudoku(board);
return board;
} else {
// return board;
}
}
function calcSeed(rowIndex, colIndex, groupId) {
const seedSet = new Set(seeds);
[ ...row[rowIndex],
...col[colIndex],
...cellGroup[groupId]
].forEach(c => seedSet.delete(c));
return seedSet;
}
function clearSolveSeeds(rowIndex, colIndex, groupId, cell, solveSeeds) {
rowIndex = rowIndex.toString();
colIndex = colIndex.toString();
groupId = groupId.toString();
for (const [[row, col], seed] of solveSeeds) {
if (row === rowIndex
|| col === colIndex
|| groupId === calcGroupId(row, col)) {
seed.delete(cell);
solveSeeds.set(`${row}${col}`, seed);
}
}
return solveSeeds;
}
function calcGroupId(rowIndex, colIndex) {
return `${floor(rowIndex / 3)}${floor(colIndex / 3)}`;
}
};
// var sudoku = [
// ['5', '3', '.', '.', '7', '.', '.', '.', '.'],
// ['6', '.', '.', '1', '9', '5', '.', '.', '.'],
// ['.', '9', '8', '.', '.', '.', '.', '6', '.'],
// ['8', '.', '.', '.', '6', '.', '.', '.', '3'],
// ['4', '.', '.', '8', '.', '3', '.', '.', '1'],
// ['7', '.', '.', '.', '2', '.', '.', '.', '6'],
// ['.', '6', '.', '.', '.', '.', '2', '8', '.'],
// ['.', '.', '.', '4', '1', '9', '.', '.', '5'],
// ['.', '.', '.', '.', '8', '.', '.', '7', '9']];
var sudoku = [
['.','.','9','7','4','8','.','.','.'],
['7','.','.','.','.','.','.','.','.'],
['.','2','.','1','.','9','.','.','.'],
['.','.','7','.','.','.','2','4','.'],
['.','6','4','.','1','.','5','9','.'],
['.','9','8','.','.','.','3','.','.'],
['.','.','.','8','.','3','.','2','.'],
['.','.','.','.','.','.','.','.','6'],
['.','.','.','2','7','5','9','.','.']];
console.log(solveSudoku(sudoku).map(row => row.map(c => c.toString().padStart(12))).join('\n'))