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-gen

v1.0.2

Published

Sudoku puzzle generator

Downloads

1,208

Readme

SudokuGen

A fast sudoku puzzle generator.

Installation

npm install sudoku-gen

Usage

You can generate a sudoku using the getSudoku command, passing an optional difficulty level:

import { getSudoku } from 'sudoku-gen';

// Get a sudoku of specific difficulty (easy, medium, hard, expert)
const sudoku = getSudoku('easy');

// Get a sudoku of random difficulty
const sudoku = getSudoku();

getSudoku returns an object:

{
  puzzle: '41--75-----53--7--2-36-81--7-9--25-1-3--9-47--2-1-7---6587--9-----26-8--1925---47',
  solution: '416975238985321764273648159769432581531896472824157396658714923347269815192583647',
  difficulty: 'easy',
};

puzzle and solution are both 81 character strings representing the 81 cells in a sudoku grid:

Numbered grid

puzzle contains placeholder dashes (-) for spaces which need to be filled in by the player. solution contains the entire grid so you can check the player's progress. The example code above would map to the following puzzle/solution:

Example puzzle and solution

How it works

Most sudoku generators start with a completed sudoku grid and remove numbers one at a time, using a backtracking algorithm to stop once the puzzle becomes unsolvable. This process is too slow to be performed in real-time, so usually requires a background task and database for generating and storing puzzles as they're created.

SudokuGen works differently. It starts with a known, solvable "seed" puzzle and performs various transformations to turn it into a brand new puzzle. This makes it extremely fast, with no requirement for a back end, whilst maintaining quality.

Each seed gives over 2.4 trillion unique puzzles. To put that in context, if you played sudoku 24/7 and took 3 minutes to solve each puzzle, it would take until your 13,915,534th birthday to exhaust a single seed :birthday:

Transformations

The following transformations are used ("!" = factorial):

  • Rotate board - 4 permutations (0°, 90°, 180°, 270°).
  • Shuffle column groups ("stacks") - 6 permutations (3!).
  • Shuffle row groups ("bands") - 6 permutations (3!).
  • Shuffle columns - 216 permutations (3! x 3! x 3!).
  • Shuffle rows - 216 permutations (3! x 3! x 3!).
  • Swap numbers - 362,880 permutations (9!).

Total permutations per seed = 4 x 6 x 6 x 216 x 216 x 362,880 = 2,437,996,216,320.