Skip to content

Instantly share code, notes, and snippets.

@sigh

sigh/simple.html Secret

Created November 19, 2021 10:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sigh/2febe2a3c5f4cc24e30c74284a6c8ec5 to your computer and use it in GitHub Desktop.
Save sigh/2febe2a3c5f4cc24e30c74284a6c8ec5 to your computer and use it in GitHub Desktop.
<html>
<head>
<script src="data/example_puzzles.js"></script>
<script src="js/util.js"></script>
<script src="js/sudoku_builder.js"></script>
<script>
var VERSION_PARAM='';
var controller = {enableDebugOutput: ()=>null};
</script>
<script src="js/debug.js"></script>
</head>
<body>
<textarea id="input" rows=4 cols=50></textarea>
<button id="solve">solve</button>
<div id="status"></div>
<div>
First solution:
<pre id="solution"></pre>
</div>
<div>
State:
<pre id="state"></pre>
</div>
</body>
<script>
const input = document.getElementById('input');
const button = document.getElementById('solve');
const solutionOutput = document.getElementById('solution');
const stateOutput = document.getElementById('state');
const statusOutput = document.getElementById('status');
button.onclick = () => {
const text = input.value;
solutionOutput.textContent = '';
stateOutput.textContent = '';
let constraint;
try {
constraint = SudokuConstraint.fromText(text);
} catch (e) {
statusOutput.textContent = e;
return;
}
statusOutput.textContent = 'Solving...';
window.setTimeout(async () => {
const solver = SudokuBuilder.build(constraint);
const result = await solver.nthSolution(0);
solutionOutput.textContent = result ? toShortSolution(result) : 'No solutions';
await solver.nthSolution(1); // Try to find a second solution to prove uniqueness.
stateOutput.textContent = JSON.stringify(solver.state(), null, ' ');
statusOutput.textContent = '';
}, 0);
};
</script>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment