Struct puzzle_solver::Puzzle
[−]
[src]
pub struct Puzzle { /* fields omitted */ }
The puzzle to be solved.
Methods
impl Puzzle
[src]
fn new() -> Self
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();
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]);
fn new_vars_with_candidates_1d(
&mut self,
n: usize,
candidates: &[Val]
) -> Vec<VarToken>
&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]);
fn new_vars_with_candidates_2d(
self: &mut Puzzle,
width: usize,
height: usize,
candidates: &[Val]
) -> Vec<Vec<VarToken>>
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]);
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);
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]); }
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]);
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]);
fn add_constraint<T>(&mut self, constraint: T) where
T: Constraint + 'static,
T: Constraint + 'static,
Add a constraint to the puzzle solution.
fn all_different<'a, I>(&mut self, vars: I) where
I: IntoIterator<Item = &'a VarToken>,
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);
fn equals<L, R>(&mut self, lhs: L, rhs: R) where
L: Into<LinExpr>,
R: Into<LinExpr>,
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);
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]);
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());
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());
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);
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.
fn num_guesses(&self) -> u32
Get the number of guesses taken to solve the last puzzle.