Struct Puzzle

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

The puzzle to be solved.

Implementations§

Source§

impl Puzzle

Source

pub fn new() -> Self

Allocate a new puzzle.

§Examples
puzzle_solver::Puzzle::new();
Source

pub fn new_var(&mut self) -> VarToken

Allocate a new puzzle variable, without inserting any candidates.

§Examples
let mut puzzle = puzzle_solver::Puzzle::new();
puzzle.new_var();
Source

pub fn new_var_with_candidates(&mut self, candidates: &[Val]) -> VarToken

Allocate a new puzzle variable, initialising it with potential candidates.

§Examples
let mut send_more_money = puzzle_solver::Puzzle::new();
send_more_money.new_var_with_candidates(&[0,1,2,3,4,5,6,7,8,9]);
Source

pub fn new_vars_with_candidates_1d( &mut self, n: usize, candidates: &[Val], ) -> Vec<VarToken>

Allocate a 1d vector of puzzle variables, each initialised to have the same set of potential candidates.

§Examples
let mut send_more_money = puzzle_solver::Puzzle::new();
send_more_money.new_vars_with_candidates_1d(8, &[0,1,2,3,4,5,6,7,8,9]);
Source

pub fn new_vars_with_candidates_2d( self: &mut Puzzle, width: usize, height: usize, candidates: &[Val], ) -> Vec<Vec<VarToken>>

Allocate a 2d array of puzzle variables, each initialised to have the same set of potential candidates.

§Examples
let mut magic_square = puzzle_solver::Puzzle::new();
magic_square.new_vars_with_candidates_2d(3, 3, &[1,2,3,4,5,6,7,8,9]);
Source

pub fn set_value(&mut self, var: VarToken, value: Val)

Set a variable to a known value.

This is useful when the variable is given as part of the problem. After this operation, any subsequent attempts to set the value will panic.

§Examples
let mut magic_square = puzzle_solver::Puzzle::new();
let vars = magic_square.new_vars_with_candidates_2d(3, 3,
        &[1,2,3,4,5,6,7,8,9]);

magic_square.set_value(vars[1][1], 5);
Source

pub fn insert_candidates(&mut self, var: VarToken, candidates: &[Val])

Add candidates to a variable.

§Examples
let mut send_more_money = puzzle_solver::Puzzle::new();
for _ in 0..9 {
    let var = send_more_money.new_var();
    send_more_money.insert_candidates(var, &[0,1,2,3,4,5,6,7,8,9]);
}
Source

pub fn remove_candidates(&mut self, var: VarToken, candidates: &[Val])

Remove candidates from a variable.

§Examples
let mut send_more_money = puzzle_solver::Puzzle::new();
let vars = send_more_money.new_vars_with_candidates_1d(8,
        &[0,1,2,3,4,5,6,7,8,9]);

let s = vars[0];
let m = vars[4];
send_more_money.remove_candidates(s, &[0]);
send_more_money.remove_candidates(m, &[0]);
Source

pub fn intersect_candidates(&mut self, var: VarToken, candidates: &[Val])

Set the variable’s candidates to the intersection with the given list.

§Examples
let mut send_more_money = puzzle_solver::Puzzle::new();
let vars = send_more_money.new_vars_with_candidates_1d(8,
        &[0,1,2,3,4,5,6,7,8,9]);

let m = vars[4];
send_more_money.intersect_candidates(m, &[0,1]);
Source

pub fn add_constraint<T>(&mut self, constraint: T)
where T: Constraint + 'static,

Add a constraint to the puzzle solution.

Source

pub fn all_different<'a, I>(&mut self, vars: I)
where I: IntoIterator<Item = &'a VarToken>,

Add an All Different constraint.

§Examples
let mut send_more_money = puzzle_solver::Puzzle::new();
let vars = send_more_money.new_vars_with_candidates_1d(8,
        &[0,1,2,3,4,5,6,7,8,9]);

send_more_money.all_different(&vars);
Source

pub fn equals<L, R>(&mut self, lhs: L, rhs: R)
where L: Into<LinExpr>, R: Into<LinExpr>,

Add an Equality constraint.

§Examples
let mut magic_square = puzzle_solver::Puzzle::new();
let vars = magic_square.new_vars_with_candidates_2d(3, 3,
        &[1,2,3,4,5,6,7,8,9]);

magic_square.equals(vars[0][0] + vars[0][1] + vars[0][2], 15);
Source

pub fn unify(&mut self, var1: VarToken, var2: VarToken)

Add a Unify constraint.

§Examples
let mut send_more_money = puzzle_solver::Puzzle::new();
let carry = send_more_money.new_vars_with_candidates_1d(4, &[0,1]);
let vars = send_more_money.new_vars_with_candidates_1d(8,
        &[0,1,2,3,4,5,6,7,8,9]);

let m = vars[4];
send_more_money.unify(m, carry[3]);
Source

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

Find any solution to the given puzzle.

§Examples
let mut puzzle = puzzle_solver::Puzzle::new();
puzzle.new_var_with_candidates(&[1,2]);
puzzle.new_var_with_candidates(&[3,4]);

let solution = puzzle.solve_any();
assert!(solution.is_some());
Source

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

Find the solution to the given puzzle, verifying that it is unique.

§Examples
let mut puzzle = puzzle_solver::Puzzle::new();
puzzle.new_var_with_candidates(&[1,2]);
puzzle.new_var_with_candidates(&[3,4]);

let solution = puzzle.solve_unique();
assert!(solution.is_none());
Source

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

Find all solutions to the given puzzle.

§Examples
let mut puzzle = puzzle_solver::Puzzle::new();
puzzle.new_var_with_candidates(&[1,2]);
puzzle.new_var_with_candidates(&[3,4]);

let solutions = puzzle.solve_all();
assert_eq!(solutions.len(), 4);
Source

pub fn step(&mut self) -> Option<PuzzleSearch<'_>>

Take any obvious non-choices, using the constraints to eliminate candidates. Stops when it must start guessing. Primarily for testing.

Returns the intermediate puzzle search state, or None if a contradiction was found.

Source

pub fn num_guesses(&self) -> u32

Get the number of guesses taken to solve the last puzzle.

Auto Trait Implementations§

§

impl !Freeze for Puzzle

§

impl !RefUnwindSafe for Puzzle

§

impl !Send for Puzzle

§

impl !Sync for Puzzle

§

impl Unpin for Puzzle

§

impl !UnwindSafe for Puzzle

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.