sudoku

Sudoku solver and puzzle generator.

Summary
sudokuSudoku solver and puzzle generator.
Functions
solveSudokuThis solver uses the above implementation to efficiently solve a Sudoku puzzle.
makeSolutionThis function makes a random solution to a Sudoku puzzle.
makePuzzleThis function makes a puzzle (a partially filled grid), given a solution (completely filled grid) and a number of cells to leave filled.

Functions

solveSudoku

FUNCTION solveSudoku(INOUT grid: multiarray.ArrayNumber2D): Number

This solver uses the above implementation to efficiently solve a Sudoku puzzle.

makeSolution

FUNCTION makeSolution(): multiarray.ArrayNumber2D

This function makes a random solution to a Sudoku puzzle.

It does this by filling in the first row randomly, and then calling the solver.  Due to the way the solver works, this produces a bias which can be observed by looking at the first few numbers in the second row.  The first three numbers will be the lowest numbers that don’t appear in the first three cells of the first row, in order.  An improvement would be to remove this bias.

makePuzzle

FUNCTION makePuzzle(
   solution: multiarray.ArrayNumber2D,
   filled: Number,
   callback: FUNCTION(n: Number)
): multiarray.ArrayNumber2D

This function makes a puzzle (a partially filled grid), given a solution (completely filled grid) and a number of cells to leave filled.

This works by removing pairs of numbers (symmetric around 180 degree rotation), checking the resulting puzzle to see that it still has only one solution, and repeating.  If removing a pair of numbers results in a puzzle with more than one solution, the numbers are left in place and another pair is tried.

This function has considerable opportunity for improvements in efficiency.  Also, it makes no attempt to estimate the “difficulty” of the resulting puzzle.

FUNCTION solveSudoku(INOUT grid: multiarray.ArrayNumber2D): Number
This solver uses the above implementation to efficiently solve a Sudoku puzzle.
FUNCTION makeSolution(): multiarray.ArrayNumber2D
This function makes a random solution to a Sudoku puzzle.
FUNCTION makePuzzle(
   solution: multiarray.ArrayNumber2D,
   filled: Number,
   callback: FUNCTION(n: Number)
): multiarray.ArrayNumber2D
This function makes a puzzle (a partially filled grid), given a solution (completely filled grid) and a number of cells to leave filled.
Close