Last active
December 21, 2020 01:06
-
-
Save showercurtain/ea82c3a725b64ab4a534e39adcd97b0c to your computer and use it in GitHub Desktop.
This file contains 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
// This code solves a puzzle on websudoku.com | |
// You can make a javascript bookmark by creating a bookmark and entering | |
// javascript:var script = document.createElement('script'), head = document.getElementsByTagName("head")[0];script.src = "https://gist.github.com/showercurtain/ea82c3a725b64ab4a534e39adcd97b0c.js"; head.appendChild(script); | |
// as the url. | |
// It's not working yet | |
function getusable(board,pos) { | |
var unusable = [""]; | |
for (let i = 0;i < 9;i++) { | |
if (!(unusable.includes(board[i+(pos[1]*9)].value))){ | |
unusable.push(board[i+(pos[1]*9)].value); | |
} | |
if (!(unusable.includes(board[(i*9)+pos[0]].value))){ | |
unusable.push(board[(i*9)+pos[0]].value); | |
} | |
pos2 = [(Math.floor(pos[0]/3)*3)+(i%3),(Math.floor(pos[1]/3)*3)+Math.floor(i/3)]; | |
if (!(unusable.includes(board[pos2[0]+(9*pos2[1])].value))){ | |
unusable.push(board[pos2[0]+(9*pos2[1])].value); | |
} | |
} | |
var usable = []; | |
for (let i = 1;i < 10;i++) { | |
if (!(unusable.includes(String(i)))) { | |
usable.push(String(i)); | |
} | |
} | |
return usable; | |
} | |
function getempty(board) { | |
var empty = []; | |
for (let i = 0;i < 9;i++) { | |
for (let j = 0;j < 9;j++) { | |
if (board[i+(9*j)].value === "") { | |
empty.push([i,j]); | |
} | |
} | |
} | |
return empty; | |
} | |
function solve_recursive(board,empty,start) { | |
let usable = getusable(board,empty[start]); | |
if ((empty.length === 1) & (usable.length === 1)) { | |
board[empty[start][0]+(empty[start][1]*9)].value = usable[0]; | |
return true; | |
} | |
for (let i = 0;i < usable.length;i++) { | |
board[empty[start][0]+(empty[start][1]*9)].value = usable[i]; | |
if (solve_recursive(board,empty,start+1)) {return true;} | |
} | |
board[empty[start][0]+(empty[start][1]*9)].value = ""; | |
return false; | |
} | |
function solve() { | |
inps = document.getElementById("puzzle_grid").getElementsByTagName("input") | |
solve_recursive(inps,getempty(inps),0) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment