Readme

Example for sudoku solvers

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
import { print, Sudoku } from "https://esm.town/v/saolsen/sudoku";
import { solve as brute_force_solve } from "https://esm.town/v/saolsen/sudoku_solver_brute_force";
import { solve as exact_cover_solve } from "https://esm.town/v/saolsen/sudoku_solver_exact_cover";
// This is one with very few starting constraints which
// makes it more likely they will do some backtracking.
// There are multiple valid solutions and the different
// solvers usually find different ones.
const sudoku: Sudoku = [
[0, 0, 0, 0, 0, 0, 1, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 6],
[0, 0, 0, 0, 0, 7, 0, 3, 8],
[0, 0, 0, 2, 9, 0, 4, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 2, 0],
[0, 0, 2, 0, 0, 5, 0, 0, 0],
[0, 0, 0, 3, 1, 0, 0, 0, 0],
[0, 1, 6, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 9, 0, 0, 6, 0, 0],
];
const bfs = brute_force_solve(sudoku);
console.assert(JSON.stringify(bfs) !== null);
console.log("BFS solution");
print(bfs);
const ecs = exact_cover_solve(sudoku);
console.assert(JSON.stringify(ecs) !== null);
console.log("ECS solution");
print(ecs);
Val Town is a social website to write and deploy JavaScript.
Build APIs and schedule functions from your browser.
Comments
Nobody has commented on this val yet: be the first!
March 7, 2024