Tools for generating solutions and starting grids for Sudoku.
sudokulib is a collection of tools that are useful for generating solutions to Sudoku puzzles. It uses a recursive backtracking algorithm (originally written by Jeremy Brown, Cel Destept), and is capable of generating a 3x3 Sudoku in ~0.015 seconds. It can also generate 4x4 Sudokus in ~0.100 seconds (sometimes longer).
Some 5x5 grids have been generated in a matter of seconds using this library, but I’ve also seen the program run for hours without successfully generating a 5x5. Anything beyond 5x5 always seems to take a long time.
You can solve Sudoku puzzles using this library by calling the init_grid method of an instance of Sudoku. This method takes two types of values. Where n is the grid size:
- a 1-dimensional list with n^4 values
- a multi-dimensional list with n^2 lists with n^2 values each. Each list represents a row in the grid
The values in the list(s) provided to init_grid must be numeric and greater than 0 but less than n^2 in order to appear in the starting grid. Any other values will be ignored, and the puzzle will be solved with no consideration for such values.
The library also provides utilities for generating starting grids, so you can play Sudoku instead of just generating solutions. There are several difficulty levels to choose from.
Also included in the distribution is a sample class for generating “jigsaw” variations of Sudoku. This class will occasionally generate solvable grids within a second or two, but it takes much longer more often than not.