npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

jons-sudoku-solver

v1.0.9

Published

This package uses a wave collapse algorithm to solve Sudoku puzzles.

Downloads

15

Readme

Jon's Sudoku Solver

This package is a work in progress library to solve classic 9x9 Sudoku puzzles.

Most Sudoku solvers implement a backtracking algorithm which guarantees a solution but can take a long time to get there.

This package can do that however before it does, it tries a number of methods human Sudoku solvers would try first, to reduce the work the backtracking algorithm needs to perform.

For many easy and intermediate level puzzles, it can reach a solution without needing to do any backtracking or recursion at all. This should make things a lot faster and less processor hungry.

This is just a quick spare / vacation time project but I am hoping to extend the library to include more sophisticated solvers and perhaps features that might make it a useful starting point to build a Sudoku game.

If you have any feedback, ideas, comments please feel free to let me know. I can't promise I'll be able to get back to you straightaway but I'll do my best!

Install from NPM

npm i jons-sudoku-solver

Usage

The most basic usage in Javascript is to import and use the package as follows.

const { SudokuSolver } = require('jons-sudoku-solver');

testPuzzle = [
  [0, 9, 6, 0, 0, 0, 0, 3, 0],
  [0, 0, 8, 0, 0, 0, 0, 0, 0],
  [0, 5, 0, 2, 0, 4, 0, 9, 0],
  [0, 0, 1, 6, 7, 2, 0, 0, 0],
  [8, 0, 0, 0, 0, 0, 3, 0, 0],
  [0, 0, 9, 0, 0, 0, 0, 0, 0],
  [0, 0, 2, 0, 1, 0, 8, 0, 0],
  [0, 0, 0, 0, 0, 0, 0, 0, 4],
  [0, 0, 0, 0, 0, 8, 1, 6, 5],
];

solvedPuzzle = SudokuSolver.solve(testPuzzle);

console.table(solvedPuzzle);

This code generates the output:

┌─────────┬───┬───┬───┬───┬───┬───┬───┬───┬───┐
│ (index) │ 0 │ 1 │ 2 │ 3 │ 4 │ 5 │ 6 │ 7 │ 8 │
├─────────┼───┼───┼───┼───┼───┼───┼───┼───┼───┤
│    0    │ 4 │ 9 │ 6 │ 1 │ 5 │ 7 │ 2 │ 3 │ 8 │
│    1    │ 2 │ 1 │ 8 │ 3 │ 9 │ 6 │ 5 │ 4 │ 7 │
│    2    │ 7 │ 5 │ 3 │ 2 │ 8 │ 4 │ 6 │ 9 │ 1 │
│    3    │ 5 │ 3 │ 1 │ 6 │ 7 │ 2 │ 4 │ 8 │ 9 │
│    4    │ 8 │ 2 │ 7 │ 5 │ 4 │ 9 │ 3 │ 1 │ 6 │
│    5    │ 6 │ 4 │ 9 │ 8 │ 3 │ 1 │ 7 │ 5 │ 2 │
│    6    │ 9 │ 6 │ 2 │ 4 │ 1 │ 5 │ 8 │ 7 │ 3 │
│    7    │ 1 │ 8 │ 5 │ 7 │ 6 │ 3 │ 9 │ 2 │ 4 │
│    8    │ 3 │ 7 │ 4 │ 9 │ 2 │ 8 │ 1 │ 6 │ 5 │
└─────────┴───┴───┴───┴───┴───┴───┴───┴───┴───┘

The SudokuSolver.solve() method can also take optional parameters to allow the calling application to control which solvers should be included in the solving process. You can also control whether or not you want to use the Backtracking solver to finish off the puzzle if the solvers don't get the job done.

const { SudokuSolver, SudokuSolverStrategy } = require('jons-sudoku-solver');

testPuzzle = [
  [0, 9, 6, 0, 0, 0, 0, 3, 0],
  [0, 0, 8, 0, 0, 0, 0, 0, 0],
  [0, 5, 0, 2, 0, 4, 0, 9, 0],
  [0, 0, 1, 6, 7, 2, 0, 0, 0],
  [8, 0, 0, 0, 0, 0, 3, 0, 0],
  [0, 0, 9, 0, 0, 0, 0, 0, 0],
  [0, 0, 2, 0, 1, 0, 8, 0, 0],
  [0, 0, 0, 0, 0, 0, 0, 0, 4],
  [0, 0, 0, 0, 0, 8, 1, 6, 5],
];

var options = {
  /**
   * An optional array of SudokuSolverStrategy values representing the solvers to include.
   * If an array isn't provided, it will include all solvers by default. To run
   * the backtracking algorithm on its own, specify an empty array i.e. [].
   */
  includeStrategies: [SudokuSolverStrategy.NakedSingles, SudokuSolverStrategy.HiddenSingles],

  /**
   * An optional value whether to complete puzzles using backtracking. If the value is
   * not specified or set to true, backtracking will be used to fill in any unknown
   * cells. If it is explicitly set to false, it will not run backtracking. Note that
   * this might result in outputting incomplete puzzles but it's useful when writing new
   * solver classes.
   */
  includeBacktracking: false,
};

solvedPuzzle = SudokuSolver.solve(testPuzzle, options);

console.table(solvedPuzzle);

If you run the above example you'll see that there aren't enough strategies enabled to solve the puzzle.

┌─────────┬───┬───┬───┬───┬───┬───┬───┬───┬───┐
│ (index) │ 0 │ 1 │ 2 │ 3 │ 4 │ 5 │ 6 │ 7 │ 8 │
├─────────┼───┼───┼───┼───┼───┼───┼───┼───┼───┤
│    0    │ 0 │ 9 │ 6 │ 0 │ 5 │ 0 │ 0 │ 3 │ 8 │
│    1    │ 0 │ 1 │ 8 │ 0 │ 0 │ 0 │ 0 │ 0 │ 0 │
│    2    │ 0 │ 5 │ 0 │ 2 │ 8 │ 4 │ 6 │ 9 │ 1 │
│    3    │ 0 │ 0 │ 1 │ 6 │ 7 │ 2 │ 0 │ 8 │ 9 │
│    4    │ 8 │ 0 │ 0 │ 0 │ 0 │ 0 │ 3 │ 0 │ 0 │
│    5    │ 0 │ 0 │ 9 │ 8 │ 0 │ 0 │ 0 │ 0 │ 0 │
│    6    │ 0 │ 0 │ 2 │ 0 │ 1 │ 0 │ 8 │ 7 │ 3 │
│    7    │ 1 │ 8 │ 0 │ 0 │ 0 │ 0 │ 9 │ 2 │ 4 │
│    8    │ 0 │ 0 │ 0 │ 0 │ 2 │ 8 │ 1 │ 6 │ 5 │
└─────────┴───┴───┴───┴───┴───┴───┴───┴───┴───┘

Missing values are indicated in the output with a 0. This could be useful output if you're developing or debugging solver strategies.

Build from source

# clone the repo
git clone https://github.com/jpmasters/jons-sudoku-solver.git

# cd into the new folder
cd jons-sudoku-solver

# install the dependencies
npm install

# check that the tests run correctly
npm run test