A robust Sudoku solver that converts grid constraints into Conjunctive Normal Form (CNF), utilizes the Z3 Theorem Prover to find solutions, and decodes the results back into a readable grid format. It supports standard
To compile the project and generate the executables (Sudoku2cnf and sol2grid):
make allEnsure your Sudoku puzzle is in input.txt (use . or 0 for empty cells), then run:
make runTo remove compiled binaries and temporary SAT files:
make removeThe solver maps every possible cell value to a unique boolean variable.
For a grid of size
The program generates SAT clauses using the following constraints:
-
c1 (Cell Vitality)
Ensures every cell contains at least one value (OR of all possibilities) -
c2 (Cell Uniqueness)
Ensures each cell contains at most one value -
c3 / c4 (Row & Column Constraints)
Ensures each value appears exactly once per row and column using: $$ (\neg a \lor \neg b) $$ -
c5 (Sub-block Constraint)
Ensures no duplicate values inside an$n \times n$ sub-grid -
par (Fixed Clues)
Readsinput.txtand encodes given values as unit clauses
If a solution is found, the solver adds a clause to block that solution:
- Re-running Z3 with this clause:
- If SAT again → multiple solutions
- If UNSAT → unique solution
input.txt → Sudoku2cnf → problem.cnf → Z3 → sat_output.txt → sol2grid → output.txt
- Sudoku2cnf → Converts Sudoku constraints into
problem.cnf - sol2grid → Converts SAT output into readable Sudoku grid
- Makefile → Automates compilation and execution
input.txt→ Sudoku grid (supports0-9andA-F)
output.txt→ First valid solutionsecond_soln.txt→ Second solution (only if multiple solutions exist)
- Unique solution → Exactly one solution exists
- Multiple solution for the sudoku → More than one solution exists
- No solution exists → Puzzle is unsatisfiable