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 🙏

© 2024 – Pkg Stats / Ryan Hefner

sudoku_solver

v1.0.1

Published

A package that solves any 9 x 9 sudoku grids

Downloads

35

Readme

sudoku-solver

This repository is a sudoku solver written in ES6.

How to use

Install the package from npm

npm install --save sudoku_solver

The module can be used like this

const sudoku = require('sudoku_solver');
const Grid = sudoku.Grid;
const Solver = sudoku.Solver;
const pattern = '8..........36......7..9.2...5...7.......457.....1...3...1....68..85...1..9....4..';

// Creates a Grid
let grid = new Grid(pattern);
// Creates a Solver
let solver = new Solver();
let solved_grid;

// Solves the grid
solved_grid = solver.solve(grid);

The pattern of the grid can be a string, an array of strings or an array of numbers.

Demo

To launch the demo, run

git clone https://github.com/Charles-BARDIN/sudoku-solver.git
npm run demo

The console will log

Grid:
8  .  .  |  .  .  .  |  .  .  .
.  .  3  |  6  .  .  |  .  .  .
.  7  .  |  .  9  .  |  2  .  .
--------- ----------- ----------
.  5  .  |  .  .  7  |  .  .  .
.  .  .  |  .  4  5  |  7  .  .
.  .  .  |  1  .  .  |  .  3  .
--------- ----------- ----------
.  .  1  |  .  .  .  |  .  6  8
.  .  8  |  5  .  .  |  .  1  .
.  9  .  |  .  .  .  |  4  .  .


Solved in 1,340.255ms

8  1  2  |  7  5  3  |  6  4  9
9  4  3  |  6  8  2  |  1  7  5
6  7  5  |  4  9  1  |  2  8  3
--------- ----------- ----------
1  5  4  |  2  3  7  |  8  9  6
3  6  9  |  8  4  5  |  7  2  1
2  8  7  |  1  6  9  |  5  3  4
--------- ----------- ----------
5  2  1  |  9  7  4  |  3  6  8
4  3  8  |  5  2  6  |  9  1  7
7  9  6  |  3  1  8  |  4  5  2

The grid from the demo comes from the most difficult sudoku grid.

Tests

To launch the tests, run

npm install
npm test

Algorithm

The algorithm composed of two parts:

  • assign the values from the pattern to the grid
  • search the values for the cells that are not solved

Vocabulary

Here are the definitions of the terms used in this program:

grid

A 9 x 9 sudoku grid

cell

A grid is composed of 81 cells that can have an integer value between 1 and 9 included

sibling

A sibling of a cell is a cell that belongs to the same row, column or square

square

A grid is composed of 9 squares that are 3 x 3 subgrids

solved

A cell is solved if its value is determinated. A grid is solved if all its cell are solved

Assigning value

In this part of the algorithm, the values from the pattern are assigned to the grid. This is suffiscent to complete the simplest sudoku grids.

To assign a value to a cell, it actually removes all the other possible values of the cell. Each time a value is removed, it checks if it permits to assign a value to a sibling by looking if only one sibling can accept the value

Each time a value is assigned to a cell, it is removed from the possible values of the siblings. If a sibling has only one possible value, the value is assigned to the sibling.

If the algorithm encounter any inconsistencies when assigning a value, it will return null.

Searching for missing values

If the first part of the algorithm have not solved the grid, it will search for missing values.

It checks which cell has the minimum possible values.

Then, it tries to assign the first value using the first part of the algorithm.

If it returns null, it means an inconsistency has been encountered. It then tries with the second value, etc. If it returns a grid, it means this value was assigned with success.

If the grid is not solved, it will use this part of the algorithm to search for the other values. Otherwise it is done.