feat: implement deductive Sudoku solver

Add a command-line Sudoku solver that reads a whitespace-separated puzzle file,
uses zeroes as empty cells, and prints a solved grid. The parser accepts any
square Sudoku size whose block shape is also square, including 9x9 with 3x3
blocks and 16x16 with 4x4 blocks.

The solver represents candidate sets as u128 bit masks and precomputes row,
column, block, and peer relationships for each cell. It repeatedly applies
human-style deterministic deductions by placing naked singles and hidden singles.
When those deductions stall, it falls back to a constrained trial search: choose
the unsolved cell with the fewest candidates, try high-impact values first, and
resume deduction after each assumption.

The public library API keeps parsing, solving, and formatting testable outside
the CLI. The README documents usage and the top-down project decomposition so
future changes have an architectural map.

Known limitation: the solver returns one valid completion for puzzles with
multiple solutions; it does not currently prove uniqueness.

Test Plan:
- cargo clippy
- cargo clippy --benches
- cargo clippy --tests
- cargo test
- cargo +nightly fmt
- cargo clippy
- cargo clippy --benches
- cargo clippy --tests
- cargo run -- /tmp/sudoku-ai-smoke.txt
- timeout 5 cargo run -- /tmp/sudoku-ai-underconstrained.txt

Refs: none
This commit is contained in:
2026-04-25 20:57:44 +02:00
parent 87a53c09bc
commit 06918ea670
3 changed files with 738 additions and 1 deletions
+50
View File
@@ -0,0 +1,50 @@
# sudoku-ai
`sudoku-ai` solves plain-text Sudoku files from the command line:
```sh
./sudoku-ai <sudoku_file>
```
Input is a whitespace-separated grid. Use `0` for empty cells and decimal
values for filled cells:
```text
0 2 3 0 0 0 4 5 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
```
The grid width must equal the grid height, and the size must have square
blocks. That supports layouts such as 4x4 with 2x2 blocks, 9x9 with 3x3
blocks, and 16x16 with 4x4 blocks.
## Project Decomposition
- Command-line interface: `src/main.rs`
- Parses the `./sudoku-ai <sudoku_file>` argument shape.
- Reads puzzle text from disk.
- Prints the solved grid or a user-facing error.
- Solver library: `src/lib.rs`
- Plain-text parser
- Ignores blank lines.
- Validates square dimensions, block shape, and value ranges.
- Constraint model
- Stores each candidate set in a `u128` bit mask.
- Precomputes row, column, and block units for each cell.
- Precomputes peer cells for fast candidate elimination.
- Deduction engine
- Propagates placed values to all peers.
- Fills naked singles when a cell has one candidate.
- Fills hidden singles when a unit has one possible place for a value.
- Search engine
- Runs deduction until it stalls.
- Chooses the unsolved cell with the fewest candidates.
- Tries high-impact candidates first, then resumes deduction after each
assumption.