Solver

Struct Solver 

Source
pub struct Solver { /* private fields */ }
Expand description

A Sudoku solver using logical strategies and optional backtracking.

Implementations§

Source§

impl Solver

Source

pub fn new() -> Self

Creates a new solver with default strategies.

Examples found in repository?
examples/generate.rs (line 4)
3fn main() {
4    let mut solver = Solver::new();
5
6    for difficulty in [
7        Difficulty::Easy,
8        Difficulty::Medium,
9        Difficulty::Hard,
10        Difficulty::Expert,
11    ] {
12        let puzzle = solver.generate(9, difficulty).unwrap();
13        println!("{difficulty:?}:\n{puzzle}");
14    }
15}
More examples
Hide additional examples
examples/solve.rs (line 12)
3fn main() {
4    let puzzle = Sudoku::from_string(
5        "530070000600195000098000060800060003400803001700020006060000280000419005000080079",
6        9,
7    )
8    .unwrap();
9
10    println!("Puzzle:\n{puzzle}");
11
12    let mut solver = Solver::new();
13    let (solution, stats) = solver.solve_with_stats(puzzle).unwrap();
14
15    println!("Solution:\n{solution}");
16    println!(
17        "Stats: {} iterations, {} backtracks",
18        stats.iterations, stats.backtracks
19    );
20}
examples/basic.rs (line 29)
3fn main() {
4    // Create empty 9x9 sudoku
5    let mut sudoku = Sudoku::new(9);
6
7    // Set some values
8    sudoku.set(0, 0, 5).unwrap();
9    sudoku.set(0, 1, 3).unwrap();
10    sudoku.set(0, 4, 7).unwrap();
11
12    // Check validity
13    println!("Valid: {}", sudoku.is_valid());
14    println!("Complete: {}", sudoku.is_complete());
15
16    // Get candidates for a cell
17    let candidates = sudoku.candidates(0, 2);
18    println!("Candidates at (0,2): {:?}", candidates);
19
20    // Check cell state
21    match sudoku.get(0, 0) {
22        Some(Cell::Filled(v)) => println!("Cell (0,0) = {v}"),
23        Some(Cell::Given(v)) => println!("Cell (0,0) = {v} (given)"),
24        Some(Cell::Empty) => println!("Cell (0,0) is empty"),
25        None => println!("Out of bounds"),
26    }
27
28    // Get hint
29    let solver = Solver::new();
30    if let Some((r, c, val)) = solver.hint(&sudoku) {
31        println!("Hint: Place {val} at ({r}, {c})");
32    }
33}
Source

pub fn with_strategies(strategies: Vec<Box<dyn Strategy>>) -> Self

Creates a solver with custom strategies.

Source

pub fn max_iterations(self, n: usize) -> Self

Sets maximum iterations for logical solving.

Source

pub fn use_backtracking(self, enabled: bool) -> Self

Enables or disables backtracking.

Source

pub fn solve(&mut self, sudoku: Sudoku) -> Result<Sudoku, String>

Solves the puzzle, returning the solution.

Source

pub fn solve_with_stats( &mut self, sudoku: Sudoku, ) -> Result<(Sudoku, Stats), String>

Solves the puzzle, returning solution and statistics.

Examples found in repository?
examples/solve.rs (line 13)
3fn main() {
4    let puzzle = Sudoku::from_string(
5        "530070000600195000098000060800060003400803001700020006060000280000419005000080079",
6        9,
7    )
8    .unwrap();
9
10    println!("Puzzle:\n{puzzle}");
11
12    let mut solver = Solver::new();
13    let (solution, stats) = solver.solve_with_stats(puzzle).unwrap();
14
15    println!("Solution:\n{solution}");
16    println!(
17        "Stats: {} iterations, {} backtracks",
18        stats.iterations, stats.backtracks
19    );
20}
Source

pub fn hint(&self, sudoku: &Sudoku) -> Option<(usize, usize, u8)>

Returns a hint: (row, col, value) for the next logical move.

Examples found in repository?
examples/basic.rs (line 30)
3fn main() {
4    // Create empty 9x9 sudoku
5    let mut sudoku = Sudoku::new(9);
6
7    // Set some values
8    sudoku.set(0, 0, 5).unwrap();
9    sudoku.set(0, 1, 3).unwrap();
10    sudoku.set(0, 4, 7).unwrap();
11
12    // Check validity
13    println!("Valid: {}", sudoku.is_valid());
14    println!("Complete: {}", sudoku.is_complete());
15
16    // Get candidates for a cell
17    let candidates = sudoku.candidates(0, 2);
18    println!("Candidates at (0,2): {:?}", candidates);
19
20    // Check cell state
21    match sudoku.get(0, 0) {
22        Some(Cell::Filled(v)) => println!("Cell (0,0) = {v}"),
23        Some(Cell::Given(v)) => println!("Cell (0,0) = {v} (given)"),
24        Some(Cell::Empty) => println!("Cell (0,0) is empty"),
25        None => println!("Out of bounds"),
26    }
27
28    // Get hint
29    let solver = Solver::new();
30    if let Some((r, c, val)) = solver.hint(&sudoku) {
31        println!("Hint: Place {val} at ({r}, {c})");
32    }
33}
Source

pub fn count_solutions(&self, sudoku: Sudoku, max: usize) -> usize

Counts solutions up to a maximum.

Source

pub fn generate( &mut self, size: usize, difficulty: Difficulty, ) -> Result<Sudoku, String>

Generates a puzzle of the given size and difficulty.

Examples found in repository?
examples/generate.rs (line 12)
3fn main() {
4    let mut solver = Solver::new();
5
6    for difficulty in [
7        Difficulty::Easy,
8        Difficulty::Medium,
9        Difficulty::Hard,
10        Difficulty::Expert,
11    ] {
12        let puzzle = solver.generate(9, difficulty).unwrap();
13        println!("{difficulty:?}:\n{puzzle}");
14    }
15}
Source

pub fn step(&self, sudoku: &mut Sudoku) -> bool

Applies one strategy step. Returns true if progress was made.

Trait Implementations§

Source§

impl Default for Solver

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

§

impl Freeze for Solver

§

impl !RefUnwindSafe for Solver

§

impl Send for Solver

§

impl Sync for Solver

§

impl Unpin for Solver

§

impl !UnwindSafe for Solver

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V