puzzle_solver/
lib.rs

1//! This crate searches for the solutions to logic puzzles.
2//! The puzzle rules are expressed as constraints.
3
4extern crate bit_set;
5extern crate num_rational;
6extern crate num_traits;
7
8use std::collections::HashMap;
9use std::ops;
10use num_rational::Rational32;
11
12pub use constraint::Constraint;
13pub use puzzle::Puzzle;
14pub use puzzle::PuzzleSearch;
15
16/// A puzzle variable token.
17#[derive(Copy,Clone,Debug,Eq,Hash,PartialEq)]
18pub struct VarToken(usize);
19
20/// The type of a puzzle variable's value (i.e. the candidate type).
21pub type Val = i32;
22
23/// The type of the coefficients in a linear expression.
24pub type Coef = Rational32;
25
26/// A linear expression.
27///
28/// ```text
29///   constant + coef1 * var1 + coef2 * var2 + ...
30/// ```
31#[derive(Clone)]
32pub struct LinExpr {
33    constant: Coef,
34
35    // The non-zero coefficients in the linear expression.  If, after
36    // some manipulations, the coefficient is 0, then it must be
37    // removed from the map.
38    coef: HashMap<VarToken, Coef>,
39}
40
41/// A result during a puzzle solution search (Err = contradiction).
42pub type PsResult<T> = Result<T, ()>;
43
44/// A dictionary mapping puzzle variables to the solution value.
45#[derive(Debug)]
46pub struct Solution {
47    vars: Vec<Val>,
48}
49
50pub mod constraint;
51
52mod linexpr;
53mod puzzle;
54
55impl ops::Index<VarToken> for Solution {
56    type Output = Val;
57    fn index(&self, var: VarToken) -> &Val {
58        let VarToken(idx) = var;
59        &self.vars[idx]
60    }
61}