Commit Graph

72 Commits

Author SHA1 Message Date
David Wang
73bf143595 All Different: detect values that can only be assigned to one variable.
queen_6x6: 66 -> 62 guesses.
queen_7x7: 179 -> 167 guesses.
queen_8x8: 662 -> 590 guesses.
sudoku_hardest: 1850 -> 172 guesses.
2017-02-25 09:05:13 +11:00
David Wang
7c3ba0faee All Different: contradiction if more variables than candidates.
sudoku_hardest: 1850 -> 1594 guesses.
2017-02-25 08:45:38 +11:00
David Wang
0477a5bbb0 Only wake affected constraints. 2017-02-25 08:17:07 +11:00
David Wang
6eb25f6af0 Add test: N-queens problem.
queens_4x4: 8 guesses.
queens_5x5: 21 guesses.
queens_6x6: 66 guesses.
queens_7x7: 179 guesses.
queens_8x8: 662 guesses.
2017-02-24 07:40:33 +11:00
David Wang
34e58114ea Add test: Sudoku.
sudoku_hardest: 1850 guesses.
sudoku_wikipedia: no guesswork!
2017-02-24 07:26:56 +11:00
David Wang
6546a936c3 Count the number of guesses taken to solve the puzzle. 2017-02-24 07:16:53 +11:00
David Wang
3911802d89 Add convenience functions for all different.
The constraint creation functions now take an IntoIterator allowing
you to use generic collections, and even perform maps and filters.
This is useful, for example, when the variables are in a 2d array.
2017-02-23 08:06:17 +11:00
David Wang
fcefa40b6c Add all different constraint.
This is a very simple all different constraint, e.g.

- It does not know that if there are more variables than candidates,
  it has reached a contradiction.

- It does not know that, if there are as many variables as candidates,
  if only one variable can take on a candidate value, then all other
  candidates for that variable can be eliminated.
2017-02-23 07:53:26 +11:00
David Wang
460e91e16b Apply puzzle constraints.
For simplicity, we currently wake every constraint whenever there is a
change.  A constraint's on_assigned function must take this into
account.
2017-02-22 08:11:17 +11:00
David Wang
d199b65a22 Add puzzle solver stubs.
We have not implemented any constraints yet, so this simply finds all
combinations of variable assignments.
2017-02-22 07:40:06 +11:00
David Wang
651cc9f6c7 Add constraint trait.
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.
2017-02-21 08:24:59 +11:00
David Wang
a5c274d9b7 Add puzzle search helper type.
The puzzle search type is a puzzle with some variables assigned.  It
will be passed (mutably) to constraints for them to do their thing.

All changes to a variables' candidates must go through this object so
that we can wake up affected constraints.
2017-02-20 08:18:42 +11:00
David Wang
b5851e6876 Add solution type.
When a valid solution has been found, we will create a Solution
structure.  The values assigned to the puzzle variables can be
accessed using the indexing notation: solution[var].
2017-02-20 08:07:00 +11:00
David Wang
859e7c7341 Add convenience functions for creating puzzle variables. 2017-02-19 09:03:01 +11:00
David Wang
9464629ca1 Add operations to manipulate candidates.
We have chosen to store candidates in a BTreeSet rather than a HashSet
so that iteration will be deterministic, not randomised.
2017-02-19 08:58:17 +11:00
David Wang
33ed1a3dbb Add types for a single candidate and a set of candidates.
Candidates are always an i32 (rather than a generic integer type) to
more easily perform arithmetic.  The puzzles we will deal with should
always be small enough such that we do not exceed the limits of i32.

Candidate groups are separated into an enum.  This is to help us keep
it general, as we would like to include a range option in the future.
2017-02-19 08:38:58 +11:00
David Wang
dbd4a9b5c8 Add puzzle variable tokens.
We have chosen to return a variable token for every variable created,
instead of referencing puzzle variables by string or something else.
Puzzle variables should only be used in the puzzle that created it,
though this is neither checked nor enforced.

Eventually, we would also like to overload the standard arithmetic
symbols to ergonomically build constraint expressions.
2017-02-19 08:11:13 +11:00
David Wang
242dd19fca Add puzzle type. 2017-02-19 08:03:29 +11:00
David Wang
c62c26aafd Add dependency: bit-set.
We will be using bit-sets to keep track of which puzzle variables are
used in which constraints, and which constraints need to be woken up.
2017-02-18 16:05:10 +11:00
David Wang
eebe23f2e0 Add Travis CI metadata.
Copied from crates.io/travis-ci Rust guide.
2017-02-18 08:28:20 +11:00
David Wang
d969b43099 Add licence (MIT). 2017-02-18 08:25:56 +11:00
David Wang
11f6cd7395 Initial commit: puzzle solver. 2017-02-18 08:21:54 +11:00