Struct Rustoku

Source
pub struct Rustoku {
    pub board: Board,
    pub masks: Masks,
    pub candidates: Candidates,
    pub techniques: TechniqueFlags,
}
Expand description

Solver primitive that uses backtracking and bitmasking for constraints.

This struct supports the ability to:

  • Initialize from a 2D array, a flat byte array, or a string representation
  • Solve a Sudoku puzzle using backtracking with Minimum Remaining Values (MRV)
  • Generate a Sudoku puzzle with a unique solution based on the number of clues specified
  • Check if a Sudoku puzzle is solved correctly

§Examples

Solve a Sudoku puzzle:

use rustoku_lib::Rustoku;
let puzzle = "530070000600195000098000060800060003400803001700020006060000280000419005000080079";
let mut rustoku = Rustoku::new_from_str(puzzle).unwrap();
assert!(rustoku.solve_any().is_some());

Generate a Sudoku puzzle:

use rustoku_lib::{Rustoku, generate_board};
let board = generate_board(30).unwrap();
let solution = Rustoku::new(board).unwrap().solve_all();
assert_eq!(solution.len(), 1);

Check if a Sudoku puzzle is solved:

use rustoku_lib::Rustoku;
let puzzle = "534678912672195348198342567859761423426853791713924856961537284287419635345286179";
let rustoku = Rustoku::new_from_str(puzzle).unwrap();
assert!(rustoku.is_solved());

Fields§

§board: Board

The current state of the Sudoku board.

§masks: Masks

Bitmasks that check if a cell is safe in a row, column and box.

§candidates: Candidates

Candidate cache from computing the bitmasks.

§techniques: TechniqueFlags

Techniques used during the initial phase of solving.

Implementations§

Source§

impl Rustoku

Source

pub fn new(initial_board: Board) -> Result<Self, RustokuError>

Constructs a new Rustoku instance from an initial Board.

Source

pub fn new_from_str(s: &str) -> Result<Self, RustokuError>

Constructs a new Rustoku instance from a string representation of the board.

Source

pub fn with_techniques(self, techniques: TechniqueFlags) -> Self

Returns the existing Rustoku instance, with modified techniques.

Source

pub fn solve_until(&mut self, bound: usize) -> Vec<Solution>

Solves the Sudoku puzzle up to a certain bound, returning solutions with their solve paths.

Source

pub fn solve_any(&mut self) -> Option<Solution>

Attempts to solve the Sudoku puzzle using backtracking with MRV (Minimum Remaining Values).

Source

pub fn solve_all(&mut self) -> Vec<Solution>

Finds all possible solutions for the Sudoku puzzle.

Source

pub fn is_solved(&self) -> bool

Checks if the Sudoku puzzle is solved correctly.

Trait Implementations§

Source§

impl Clone for Rustoku

Source§

fn clone(&self) -> Rustoku

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Rustoku

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Copy for Rustoku

Auto Trait Implementations§

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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