Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/ctamblyn/sudoku-solver
Sudoku solver library for Rust
https://github.com/ctamblyn/sudoku-solver
algorithm rust sudoku sudoku-solver
Last synced: 2 months ago
JSON representation
Sudoku solver library for Rust
- Host: GitHub
- URL: https://github.com/ctamblyn/sudoku-solver
- Owner: ctamblyn
- License: mit
- Created: 2021-05-24T21:51:28.000Z (over 3 years ago)
- Default Branch: main
- Last Pushed: 2023-11-30T16:02:46.000Z (about 1 year ago)
- Last Synced: 2024-09-14T23:52:32.837Z (4 months ago)
- Topics: algorithm, rust, sudoku, sudoku-solver
- Language: Rust
- Homepage:
- Size: 62.5 KB
- Stars: 2
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Sudoku solver library for Rust
![Test results](https://github.com/ctamblyn/sudoku-solver/actions/workflows/quickstart.yml/badge.svg)
[![Crates.io](https://img.shields.io/crates/v/sudoku-solver)](https://crates.io/crates/sudoku-solver)
[![Documentation](https://docs.rs/sudoku-solver/badge.svg)](https://docs.rs/sudoku-solver)This library provides a very simple backtracking algorithm for solving sudoku puzzles.
## Examples
The `solve()` function will yield the first solution found for a given puzzle,
or `None` if no solution exists:```rust
use sudoku_solver::*;fn main() {
let board = Board::from(&[
[0, 2, 0, 0, 0, 0, 0, 0, 0], // row 1
[0, 0, 0, 6, 0, 0, 0, 0, 3], // row 2
[0, 7, 4, 0, 8, 0, 0, 0, 0], // row 3
[0, 0, 0, 0, 0, 3, 0, 0, 2], // row 4
[0, 8, 0, 0, 4, 0, 0, 1, 0], // row 5
[6, 0, 0, 5, 0, 0, 0, 0, 0], // row 6
[0, 0, 0, 0, 1, 0, 7, 8, 0], // row 7
[5, 0, 0, 0, 0, 9, 0, 0, 0], // row 8
[0, 0, 0, 0, 0, 0, 0, 4, 0], // row 9
]);println!("Puzzle:\n{}\n", board);
if let Some(solution) = solve(&board) {
println!("Solution:\n{}\n", solution);
} else {
println!("No solution found.");
}
}
```If a puzzle has multiple solutions and you want to iterate over them, you can use
`SolutionIter`:```rust
use sudoku_solver::*;fn main() {
let board = Board::from(&[
[9, 0, 6, 0, 7, 0, 4, 0, 3], // row 1
[0, 0, 0, 4, 0, 0, 2, 0, 0], // row 2
[0, 7, 0, 0, 2, 3, 0, 1, 0], // row 3
[5, 0, 0, 0, 0, 0, 1, 0, 0], // row 4
[0, 4, 0, 2, 0, 8, 0, 6, 0], // row 5
[0, 0, 3, 0, 0, 0, 0, 0, 5], // row 6
[0, 3, 0, 7, 0, 0, 0, 5, 0], // row 7
[0, 0, 7, 0, 0, 5, 0, 0, 0], // row 8
[4, 0, 5, 0, 1, 0, 7, 0, 8], // row 9
]);for solution in SolutionIter::new(&board) {
println!("Solution:\n{}\n", solution);
}
}
```