-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathalgorithm.js
366 lines (319 loc) · 11.3 KB
/
algorithm.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
// Helper functions
var loopId = null;
var blockIndex = null;
var blockValuesPresent = null;
var blockSizes = null;
var nothingChanged = false;
var next = null;
var shiftHeld = false;
function excludeValue(cell, value) {
if (cell.textContent != "") {
return;
}
var possibleValues = cell.getAttribute("data-possiblevalues");
possibleValues = possibleValues.replace(value, "");
if (possibleValues != cell.getAttribute("data-possiblevalues")) {
nothingChanged = false;
}
cell.setAttribute("data-possiblevalues", possibleValues);
if (possibleValues.length == 1) {
setCell(cell, possibleValues);
} else if (possibleValues.length == 0) {
// Shouldn't happen
throw new Error("Cell at " + cell.getAttribute("data-x") + ", " + cell.getAttribute("data-y") + " has no possible values");
}
}
function setCell(cell, value) {
cell.innerHTML = value;
var block = cell.getAttribute("data-block");
blockValuesPresent[block] += value;
for (var i = 0; i < blockIndex[block].length; i++) {
excludeValue(blockIndex[block][i], value);
}
nothingChanged = false;
}
// All the characters in str in an array
function stringToArray(str) {
var ret = [];
for (var i = 0; i < str.length; i++) {
ret.push(str[i]);
}
return ret;
}
function cellsAreNeighbors(cell1, cell2) {
var x1 = parseInt(cell1.getAttribute("data-x"));
var x2 = parseInt(cell2.getAttribute("data-x"));
var y1 = parseInt(cell1.getAttribute("data-y"));
var y2 = parseInt(cell2.getAttribute("data-y"));
return Math.abs(x1 - x2) <= 1 && Math.abs(y1 - y2) <= 1;
}
// Like array.includes, but uses == instead of ===
function weakIncludes(arr, value) {
return arr.some(item => item == value);
}
// Return all items in an array, without duplicates
// https://stackoverflow.com/a/1584377/3141917
function arrayUnique(array) {
var a = array.concat();
for (var i = 0; i < a.length; i++) {
for (var j = i + 1; j < a.length; j++) {
if (a[i] === a[j]) {
a.splice(j--, 1);
}
}
}
return a;
}
// UI functions
function checkShiftDown(event) {
if (event.key == "Shift") {
shiftHeld = true;
}
}
function checkShiftUp(event) {
if (event.key == "Shift") {
shiftHeld = false;
}
}
function rapidSolveOver(element) {
if (shiftHeld) {
element.style.width = window.getComputedStyle(element).width;
element.innerHTML = "WARP SPEED!";
}
}
function rapidSolveOut(element) {
element.style.width = null;
element.innerHTML = "Rapid Auto Solve";
}
function rapidSolve() {
if (shiftHeld) {
solve(1, 1);
} else {
solve(100, 100);
}
}
function stop() {
if (next != null) {
clearTimeout(next);
next = null;
}
if (loopId != null) {
clearInterval(loopId);
loopId = null;
}
var enable = ["resetButton", "loadButton", "nextButton", "autoButton"];
for (var i = 0; i < enable.length; i++) {
document.getElementById(enable[i]).disabled = false;
}
document.getElementById("stopButton").disabled = true;
}
// Algorithm
function solve(stepDelay, levelDelay) {
var disable = ["resetButton", "loadButton", "nextButton", "autoButton"];
for (var i = 0; i < disable.length; i++) {
document.getElementById(disable[i]).disabled = true;
}
document.getElementById("stopButton").disabled = false;
var displayNone = [ "wintext", "autofail", "autosolved", "autoerror" ];
for (var i = 0; i < displayNone.length; i++) {
document.getElementById(displayNone[i]).style.display = "none";
}
blockValuesPresent = {};
blockSizes = {};
blockIndex = {};
// Setup part 1: initialize block index
// The block index keeps track of what values are already present in each block, as well as the size of that block.
for (var x = 0; x < currentLevel.xSize; x++) {
for (var y = 0; y < currentLevel.ySize; y++) {
var cell = getCell(x, y);
var block = cell.getAttribute("data-block");
if (block in blockSizes) {
blockSizes[block]++;
blockIndex[block].push(cell);
} else {
blockSizes[block] = 1;
blockValuesPresent[block] = "";
blockIndex[block] = [ cell ];
}
}
}
// Setup part 2: add cell value to block index (for non-empty cells), and initialize data-possiblevalues attribute (for empty cells)
// This attribute represents what values the cell could possibly have.
for (var x = 0; x < currentLevel.xSize; x++) {
for (var y = 0; y < currentLevel.ySize; y++) {
var cell = getCell(x, y);
var block = cell.getAttribute("data-block");
if (cell.textContent == "") {
cell.setAttribute("data-possiblevalues", "123456789".substring(0, blockSizes[block]));
} else {
blockValuesPresent[block] += cell.innerHTML;
}
}
}
// Setup part 3: fill in possiblevalues based on block index
for (var x = 0; x < currentLevel.xSize; x++) {
for (var y = 0; y < currentLevel.ySize; y++) {
var cell = getCell(x, y);
var block = cell.getAttribute("data-block");
if (cell.textContent == "") {
for (var i = 0; i < blockValuesPresent[block].length; i++) {
excludeValue(cell, blockValuesPresent[block][i]);
}
}
}
}
loopId = setInterval(function() {
try {
nothingChanged = true;
analyze();
var solved = true;
solvedCheck:
for (var x = 0; x < currentLevel.xSize; x++) {
for (var y = 0; y < currentLevel.ySize; y++) {
if (getCell(x, y).textContent == "") {
solved = false;
break solvedCheck;
}
}
}
if (solved) {
document.getElementById("autosolved").style.display = null;
stop();
if (levelDelay != -1) {
document.getElementById("stopButton").disabled = false;
next = setTimeout(function() {
nextLevel();
solve(stepDelay, levelDelay);
}, levelDelay);
}
} else if (nothingChanged) {
document.getElementById("autofail").style.display = null;
stop();
}
} catch (e) {
document.getElementById("autofail").style.display = null;
document.getElementById("autoerror").style.display = null;
stop();
throw e;
}
}, stepDelay);
}
// Techniques:
// Implemented:
// Cells that can only have one digit because of neighbors
// Cells that can only have one digit because of block
// Cells that can not have a digit because if it did, an adjacent block could not have that digit
// Only cell in block that can hold a digit
function analyze() {
// Go through empty cells and determine which cells cannot have a certain digit
for (var x = 0; x < currentLevel.xSize; x++) {
cellCheck:
for (var y = 0; y < currentLevel.ySize; y++) {
var cell = getCell(x, y);
if (cell.textContent != "") {
continue cellCheck;
}
// Technique 1: neighbor exclusion
// As literally stated in the rules, a cell cannot have a digit that any of its neighbors have.
for (var xOffset = -1; xOffset <= 1; xOffset++) {
neighborCheck:
for (var yOffset = -1; yOffset <= 1; yOffset++) {
var neighborX = x + xOffset;
var neighborY = y + yOffset;
if (neighborX < 0 || neighborX >= currentLevel.xSize || neighborY < 0 || neighborY >= currentLevel.ySize || (xOffset == 0 && yOffset == 0)) {
continue neighborCheck;
}
var neighborCell = getCell(neighborX, neighborY);
if (neighborCell.textContent != "") {
excludeValue(cell, neighborCell.textContent);
}
}
}
var block = cell.getAttribute("data-block");
// Technique 2: block exclusion
// As also literally stated in the rules, a cell cannot have a digit if any other cell in its block also has that digit.
for (var i = 0; i < blockValuesPresent[block].length; i++) {
excludeValue(cell, blockValuesPresent[block][i]);
}
// Technique 3: Cells that can not have a digit because if it did, an adjacent block could not have that digit
// What we need to ask: Does cell border all cells in a certain block that can hold a certain digit? If so, cell cannot have that digit.
var exclusions = {}; // For each neighboring block, an array of digits that all neighbors in that block can hold
for (var xOffset = -1; xOffset <= 1; xOffset++) {
neighborCheck:
for (var yOffset = -1; yOffset <= 1; yOffset++) {
var neighborX = x + xOffset;
var neighborY = y + yOffset;
if (neighborX < 0 || neighborX >= currentLevel.xSize ||
neighborY < 0 || neighborY >= currentLevel.ySize ||
(xOffset == 0 && yOffset == 0)) {
continue neighborCheck;
}
var neighborCell = getCell(neighborX, neighborY);
if (neighborCell.textContent != "") {
continue neighborCheck;
}
var neighborBlock = neighborCell.getAttribute("data-block");
if (neighborBlock == block) {
continue neighborCheck;
}
var neighborCellPVs = stringToArray(neighborCell.getAttribute("data-possiblevalues"));
if (neighborBlock in exclusions) {
exclusions[neighborBlock] = arrayUnique(exclusions[neighborBlock].concat(neighborCellPVs));
} else {
exclusions[neighborBlock] = stringToArray(neighborCell.getAttribute("data-possiblevalues"));
}
}
}
// At this point, exclusions contains, for each neighboring block, all values that can be held by all of the neighboring cells in that block.
// What we want is to remove values that can also be held by non-neighboring cells in those blocks.
for (var exclBlock in exclusions) {
if (exclusions[exclBlock].length != 0) {
// Remove values that can also be held by non-neighboring cells in a block
blockSearch:
for (var checkCellIdx = 0; checkCellIdx < blockIndex[exclBlock].length; checkCellIdx++) {
if (blockIndex[exclBlock][checkCellIdx].textContent != "" || cellsAreNeighbors(cell, blockIndex[exclBlock][checkCellIdx])) {
continue blockSearch;
}
var possibleValues = stringToArray(blockIndex[exclBlock][checkCellIdx].getAttribute("data-possiblevalues"));
exclusions[exclBlock] = exclusions[exclBlock].filter(val => possibleValues.indexOf(val) == -1);
}
// If not all exclusions have been removed, then apply those.
for (var i = 0; i < exclusions[exclBlock].length; i++) {
excludeValue(cell, exclusions[exclBlock][i]);
}
}
}
}
}
// Technique 4: Cells that are the only one in their blocks that can hold a certain digit
for (blockKey in blockIndex) {
digitBlockCheck:
for (var digit = 1; digit <= blockSizes[blockKey]; digit++) {
var cellsThatCanHoldDigit = [];
digitCellCheck:
for (var i = 0; i < blockIndex[blockKey].length; i++) {
var cell = blockIndex[blockKey][i];
if (cell.textContent != "") {
if (cell.textContent == digit) {
continue digitBlockCheck;
} else {
continue digitCellCheck;
}
}
var possibleValues = stringToArray(cell.getAttribute("data-possiblevalues"));
if (weakIncludes(possibleValues, digit)) {
cellsThatCanHoldDigit.push(cell);
}
}
if (cellsThatCanHoldDigit.length == 1) {
var cell = cellsThatCanHoldDigit[0];
var possibleValues = stringToArray(cell.getAttribute("data-possiblevalues"));
for (var i = 0; i < possibleValues.length; i++) {
if (possibleValues[i] != digit) {
excludeValue(cellsThatCanHoldDigit[0], possibleValues[i]);
}
}
}
}
}
}