pub struct Puzzle { /* private fields */ }
Expand description
The puzzle to be solved.
Implementations§
Source§impl Puzzle
impl Puzzle
Sourcepub fn new_var(&mut self) -> VarToken
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();
Sourcepub fn new_var_with_candidates(&mut self, candidates: &[Val]) -> VarToken
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]);
Sourcepub fn new_vars_with_candidates_1d(
&mut self,
n: usize,
candidates: &[Val],
) -> Vec<VarToken>
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]);
Sourcepub fn new_vars_with_candidates_2d(
self: &mut Puzzle,
width: usize,
height: usize,
candidates: &[Val],
) -> Vec<Vec<VarToken>>
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]);
Sourcepub fn set_value(&mut self, var: VarToken, value: Val)
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);
Sourcepub fn insert_candidates(&mut self, var: VarToken, candidates: &[Val])
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]);
}
Sourcepub fn remove_candidates(&mut self, var: VarToken, candidates: &[Val])
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]);
Sourcepub fn intersect_candidates(&mut self, var: VarToken, candidates: &[Val])
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]);
Sourcepub fn add_constraint<T>(&mut self, constraint: T)where
T: Constraint + 'static,
pub fn add_constraint<T>(&mut self, constraint: T)where
T: Constraint + 'static,
Add a constraint to the puzzle solution.
Sourcepub fn all_different<'a, I>(&mut self, vars: I)where
I: IntoIterator<Item = &'a VarToken>,
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);
Sourcepub fn equals<L, R>(&mut self, lhs: L, rhs: R)
pub fn equals<L, R>(&mut self, lhs: L, rhs: R)
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);
Sourcepub fn unify(&mut self, var1: VarToken, var2: VarToken)
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]);
Sourcepub fn solve_any(&mut self) -> Option<Solution>
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());
Sourcepub fn solve_unique(&mut self) -> Option<Solution>
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());
Sourcepub fn solve_all(&mut self) -> Vec<Solution>
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);
Sourcepub fn step(&mut self) -> Option<PuzzleSearch<'_>>
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.
Sourcepub fn num_guesses(&self) -> u32
pub fn num_guesses(&self) -> u32
Get the number of guesses taken to solve the last puzzle.