This Racket program solves any legal sudoku puzzle with any number of pre-filled cells. It supports boards of any size that follow the formula:
For example, a standard 9x9 sudoku board corresponds to
The program does not use constraint solvers or Racket mutations such as the set!
form. It also verifies that the input puzzle adheres to sudoku rules.
Ensure the three *.rkt
files are in the same directory:
run.rkt
– Main file for executing commands.puzzles.rkt
– Stores example and user-defined puzzles.racket-sudoku-solver.rkt
– Contains all functions for solving puzzles.
This solver comes with a built-in demo that solves the 9 provided example puzzles. To run the demo, simply execute the run.rkt
file.
- Open
puzzles.rkt
. - Use
0
to represent empty cells. - All cell values must be in decimal format, even for larger puzzles like Hexadoku.
- Insert your sudoku puzzle in the following format:
(define userSudoku '(
0 2 0 0 0 8 0 6 5
0 0 1 3 4 0 0 0 0
0 9 8 2 0 5 0 0 0
0 0 0 0 0 0 8 0 0
0 0 0 9 5 0 0 0 2
2 7 0 8 1 3 5 0 6
0 4 0 6 0 0 9 0 8
1 8 7 0 0 9 6 2 0
9 6 0 4 0 2 0 0 0
))
-
Inside
run.rkt
, use thesolve
function to solve your puzzle:(solve userSudoku)
-
If a solution exists, it will be printed to the standard output.
-
An example of running a user-defined puzzle can be found at the end of the
run.rkt
file.
Now you're ready to solve sudoku puzzles of any size using Racket!