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

sudokutoolcollection

v1.1.3

Published

An es6 compatible sudoku generator and parser with emphasis on usability in big projects

Downloads

95

Readme

SudokuToolCollection

A Sudoku puzzle generator and solver JavaScript library based on the amazing work of [robatron]: https://github.com/robatron/sudoku.js

This is a work in probgress but pretty much completely converted now.

There is an npm package of this library. Install it with:

npm i sudokutoolcollection

Or

yarn add sudokutoolcollection

Intro

Puzzles are represented by a string of digits, 1-9, and '.' as spaces. Each character represents a square, e.g.,

"52...6.........7.13...........4..8..6......5...........418.........3..2...87....."

Represents the following board:

5 2 . | . . 6 | . . .   
. . . | . . . | 7 . 1   
3 . . | . . . | . . .   
------+-------+------
. . . | 4 . . | 8 . .   
6 . . | . . . | . 5 .   
. . . | . . . | . . .   
------+-------+------
. 4 1 | 8 . . | . . .   
. . . | . 3 . | . 2 .   
. . 8 | 7 . . | . . .

(See the included converstion functions to convert between string representations and grids, or objects with coordinates, i.e., two-dimensional arrays.)

Generate a Sudoku puzzle

Generage a Sudoku puzzle of a particular difficulty, e.g,

import SudokuToolCollection from "sudokutoolcollection";
const sudoku = SudokuToolCollection();

sudoku().generator.generate("easy")
"672819345193..4862485..3197824137659761945283359...714.38..1426.174.6.38.463...71"

sudoku().generator.generate("medium")
"8.4.71.9.976.3....5.196....3.7495...692183...4.5726..92483591..169847...753612984"

sudoku().generator.generate("hard")
".17..69..356194.2..89..71.6.65...273872563419.43...685521......798..53..634...59."

Valid difficulties are as follows, and represent the number of given squares:

"easy":         62
"medium":       53
"hard":         44
"very-hard":    35
"insane":       26
"inhuman":      17

You may also enter a custom number of squares to give, e.g.,

import SudokuToolCollection from "sudokutoolcollection";
const sudoku = SudokuToolCollection();

sudoku().generator.generate(60)
"8941376521532687497269548...72.9158.538.4219..19.852..3874.69.52415793689658.34.."

The number of givens must be a number between 17 and 81 inclusive. If it's outside of that range, the number of givens will be set to the closest bound, e.g., 0 will be treated as 17, and 100 as 81.

By default, the puzzles should have unique solutions, unless you set unique to false, e.g.,

import SudokuToolCollection from "sudokutoolcollection";
const sudoku = SudokuToolCollection();

sudoku().generator.generate("easy", false)

Note: Puzzle uniqueness is not yet implemented, so puzzles are not guaranteed to have unique solutions.

Solve a Sudoku puzzle

Solve a Sudoku puzzle given a Sudoku puzzle represented as a string, e.g.,

import SudokuToolCollection from "sudokutoolcollection";
const sudoku = SudokuToolCollection();

sudoku().solver.solve(".17..69..356194.2..89..71.6.65...273872563419.43...685521......798..53..634...59.");
"217386954356194728489257136165948273872563419943712685521439867798625341634871592"
import SudokuToolCollection from "sudokutoolcollection";
const sudoku = SudokuToolCollection();

// Board string → grid:
sudoku().conversions.stringToGrid("23.94.67.8..3259149..76.32.1.....7925.321.4864..68.5317..1....96598721433...9...7")
[
    ["2","3",".","9","4",".","6","7","."],
    ["8",".",".","3","2","5","9","1","4"],
    ["9",".",".","7","6",".","3","2","."],
    ["1",".",".",".",".",".","7","9","2"],
    ["5",".","3","2","1",".","4","8","6"],
    ["4",".",".","6","8",".","5","3","1"],
    ["7",".",".","1",".",".",".",".","9"],
    ["6","5","9","8","7","2","1","4","3"],
    ["3",".",".",".","9",".",".",".","7"]
]

// Board grid → string:
sudoku().conversions.gridToString([
    ["2","3",".","9","4",".","6","7","."],
    ["8",".",".","3","2","5","9","1","4"],
    ["9",".",".","7","6",".","3","2","."],
    ["1",".",".",".",".",".","7","9","2"],
    ["5",".","3","2","1",".","4","8","6"],
    ["4",".",".","6","8",".","5","3","1"],
    ["7",".",".","1",".",".",".",".","9"],
    ["6","5","9","8","7","2","1","4","3"],
    ["3",".",".",".","9",".",".",".","7"]
])
"23.94.67.8..3259149..76.32.1.....7925.321.4864..68.5317..1....96598721433...9...7"

// Board string → coordinated object:
sudoku().conversions.stringToObject("23.94.67.8..3259149..76.32.1.....7925.321.4864..68.5317..1....96598721433...9...7")
{
    A1:"2",
    A2:"3",
    A3:".",
    A4:"9",
    A5:"4",
    A6:".",
    A7:"6",
    A8:"7",
    A9:".",
    [ ... ]
    I1: "3",
    I2: ".",
    I3: ".",
    I4: ".",
    I5: "9",
    I6: ".",
    I7: ".",
    I8: ".",
    I9: "7"
}

// Coordinated board object → string:
sudoku().conversions.objectToString({
    A1:"2",
    A2:"3",
    A3:".",
    A4:"9",
    A5:"4",
    A6:".",
    A7:"6",
    A8:"7",
    A9:".",
    [ ... ]
    I1: "3",
    I2: ".",
    I3: ".",
    I4: ".",
    I5: "9",
    I6: ".",
    I7: ".",
    I8: ".",
    I9: "7"
})
"23.94.67.8..3259149..76.32.1.....7925.321.4864..68.5317..1....96598721433...9...7"

Get candidates

Get a grid of squares and their candidate values, propagating constraints, i.e., candidates restrict their peer candidates.

import SudokuToolCollection from "sudokutoolcollection";
const sudoku = SudokuToolCollection();

sudoku().getCandidates.get("4.25..389....4.265..523.147..1652.7.6..1945322543876915....3.1....4..9.....8....3")
[
    ["4",     "167",    "2",    "5",  "167",  "16",   "3",   "8",  "9"  ],
    ["13789", "13789",  "3789", "79", "4",    "189",  "2",   "6",  "5"  ],
    ["89",    "689",    "5",    "2",  "3",    "689",  "1",   "4",  "7"  ],
    ["389",   "389",    "1",    "6",  "5",    "2",    "48",  "7",  "48" ],
    ["6",     "78",     "78",   "1",  "9",    "4",    "5",   "3",  "2"  ],
    ["2",     "5",      "4",    "3",  "8",    "7",    "6",   "9",  "1"  ],
    ["5",     "246789", "6789", "79", "267",  "3",    "478", "1",  "468"],
    ["1378",  "123678", "3678", "4",  "1267", "156",  "9",   "25", "68" ],
    ["179",   "124679", "679",  "8",  "1267", "1569", "47",  "25", "3"  ]
]

Print a board to the console

import SudokuToolCollection from "sudokutoolcollection";

const sudoku = SudokuToolCollection();
sudoku().print_board(".17..69..356194.2..89..71.6.65...273872563419.43...685521......798..53..634...59.");
. 1 7   . . 6   9 . .   
3 5 6   1 9 4   . 2 .   
. 8 9   . . 7   1 . 6   

. 6 5   . . .   2 7 3   
8 7 2   5 6 3   4 1 9   
. 4 3   . . .   6 8 5   

5 2 1   . . .   . . .   
7 9 8   . . 5   3 . .   
6 3 4   . . .   5 9 .  

References:

Please chack Rob McGuire-Dales page for all the inspiration he took.

I merely transported his work to a typescript based ES6 component.

Link: https://github.com/robatron/sudoku.js

License:

The MIT License (MIT)

Copyright (c) 2014 Rob McGuire-Dale Copyright (c) 2020 Markus Flür

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.