Skip to content

SamirHodzic/sudoku-solver-js

Repository files navigation

sudoku-solver-js

Simple Sudoku solver in javascript.

npm-url build-url Dev Dependencies Dependencies

Check out the Demo to see it in action.

How to use?

$ npm install sudoku-solver-js

or simply include index.js into your HTML file for browser usage.

Example usage:

var SudokuSolver = require('sudoku-solver-js');
var solver = new SudokuSolver();

var puzzle = '001700509573024106800501002700295018009400305652800007465080071000159004908007053';

solver.solve(puzzle)

Output:

'241768539573924186896531742734295618189476325652813497465382971327159864918647253'

Options

var SudokuSolver = require('sudoku-solver-js');
var solver = new SudokuSolver();

var puzzle = '..17..5.9573.241.68..5.1..27..295.18..94..3.56528....7465.8..71...159..49.8..7.53';

// result as 'string'
solver.solve(puzzle, { result: 'string' })

'241768539573924186896531742734295618189476325652813497465382971327159864918647253'

// result as 'array'
solver.solve(puzzle, { result: 'array' })

[2,4,1,7,6,8,5,3,9,5,7,3,9,2,4,1,8,6,8,9,6,5,3,1,7,4,2,7,3,4,2,9,5,6,1,8,1,8,9,4,7,6,3,2,5,6,5,2,8,1,3,4,9,7,4,6,5,3,8,2,9,7,1,3,2,7,1,5,9,8,6,4,9,1,8,6,4,7,2,5,3]

// result as 'chunks' (by 9)
solver.solve(puzzle, { result: 'chunks' })

[
  [2,4,1,7,6,8,5,3,9],
  [5,7,3,9,2,4,1,8,6],
  [8,9,6,5,3,1,7,4,2],
  [7,3,4,2,9,5,6,1,8],
  [1,8,9,4,7,6,3,2,5],
  [6,5,2,8,1,3,4,9,7],
  [4,6,5,3,8,2,9,7,1],
  [3,2,7,1,5,9,8,6,4],
  [9,1,8,6,4,7,2,5,3]
]

Test

$ npm test