Last active
September 21, 2018 14:59
-
-
Save ryan-lingle/e1db351f1974d334fa79e0eb966354e7 to your computer and use it in GitHub Desktop.
add to your browsers developer console on https://www.websudoku.com/?level=1 and it will solve the puzzle
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function sudokuSolver(n, t) { | |
let start = t; | |
let count = n; | |
const tableIndex = buildTableIndexes(); | |
let table = []; | |
document.querySelectorAll("#puzzle_grid tr").forEach((row) => { | |
let cells = []; | |
for (var i = 0; i < row.cells.length; i++) { | |
cells.push(row.cells[i]) | |
} | |
table.push(cells); | |
}) | |
let value; | |
let tableValues = table.map((row) => { | |
rowValues = row.map((cell) => { | |
value = cell.firstChild.value | |
if (value == "") { | |
return null | |
} else { | |
return Number(value); | |
} | |
}) | |
return rowValues | |
}) | |
let pv; | |
tableValues = tableValues.map((row, rowIndex) => { | |
newRow = row.map((cell, cellIndex) => { | |
if (cell == null) { | |
pv = onePossibleValue(rowIndex, cellIndex) | |
if (pv) { | |
table[rowIndex][cellIndex].firstChild.value = pv; | |
return pv | |
} else { | |
return null | |
} | |
} | |
}) | |
return newRow; | |
}) | |
function onePossibleValue(row, cell) { | |
let rowValues = tableValues[row]; | |
let columnValues = tableValues.map((row) => { | |
return row[cell] | |
}) | |
let boxValues = getBoxValues([row, cell]); | |
let psbl = []; | |
[1, 2, 3, 4, 5, 6, 7, 8, 9].forEach((num) => { | |
if (!(rowValues.includes(num)) && !(columnValues.includes(num)) && !(boxValues.includes(num))) { | |
psbl.push(num) | |
} | |
}) | |
if (psbl.length == 1) { | |
return psbl[0] | |
} else { | |
return false | |
} | |
} | |
function getBoxValues(index) { | |
let box; | |
tableIndex.forEach((table, i) => { | |
table.forEach((l) => { | |
if (l[0] == index[0] && l[1] == index[1]){ | |
box = tableIndex[i]; | |
} | |
}) | |
}) | |
boxValues = box.map((cdn) => { | |
return tableValues[cdn[0]][cdn[1]] | |
}) | |
return boxValues; | |
} | |
function buildTableIndexes() { | |
let indexes = [ | |
[0, 1, 2], | |
[3, 4, 5], | |
[6, 7, 8] | |
] | |
let globalBoxIndex = [] | |
indexes.forEach((index) => { | |
indexes.forEach((index2) => { | |
index.forEach((i) => { | |
index2.forEach((l) => { | |
globalBoxIndex.push([i, l]) | |
}) | |
}) | |
}) | |
}) | |
let tableBoxes = []; | |
let counter = 1; | |
while(globalBoxIndex.length) { | |
tableBoxes.push(globalBoxIndex.splice(0,9)) | |
} | |
return tableBoxes; | |
} | |
count++; | |
if (count == 20) { | |
return console.log('Man this puzzle is too hard for me!') | |
} | |
if (tableValues.reduce((acc, val) => acc.concat(val), []).includes(null)) { | |
sudokuSolver(count, start); | |
} else { | |
let end = new Date(Date.now()) | |
let timeDiff = (Math.abs(end.getTime() - start.getTime())/ 1000); | |
console.log(`It took me ${timeDiff} seconds to solve this puzzle. Piece of cake!`) | |
} | |
} | |
let t = new Date(Date.now()) | |
sudokuSolver(0, t); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment