Each constraint is a little sub-program that attempts to reduce the search space. They should only be run as required, i.e. when the candidates of the variables they use were updated, though this has not yet been implemented. Constraints are traits so that puzzles may implement their own specialised contraints as required. We have split the trait into an "on_assigned" call and an "on_updated" call for clarity. The provided methods simply return true, indicating no contradiction.
36 lines
763 B
Rust
36 lines
763 B
Rust
//! This crate searches for the solutions to logic puzzles.
|
|
//! The puzzle rules are expressed as constraints.
|
|
|
|
extern crate bit_set;
|
|
|
|
use std::ops::Index;
|
|
|
|
pub use constraint::Constraint;
|
|
pub use puzzle::Puzzle;
|
|
pub use puzzle::PuzzleSearch;
|
|
|
|
/// A puzzle variable token.
|
|
#[derive(Copy,Clone,Debug,Eq,PartialEq)]
|
|
pub struct VarToken(usize);
|
|
|
|
/// The type of a puzzle variable's value (i.e. the candidate type).
|
|
pub type Val = i32;
|
|
|
|
/// A dictionary mapping puzzle variables to the solution value.
|
|
#[derive(Debug)]
|
|
pub struct Solution {
|
|
vars: Vec<Val>,
|
|
}
|
|
|
|
pub mod constraint;
|
|
|
|
mod puzzle;
|
|
|
|
impl Index<VarToken> for Solution {
|
|
type Output = Val;
|
|
fn index(&self, var: VarToken) -> &Val {
|
|
let VarToken(idx) = var;
|
|
&self.vars[idx]
|
|
}
|
|
}
|