Apply naked single strategy to solve a NYT easy sudoku.
This commit is contained in:
parent
9901c6832f
commit
322011306a
7 changed files with 163 additions and 3 deletions
63
src/validator.rs
Normal file
63
src/validator.rs
Normal file
|
|
@ -0,0 +1,63 @@
|
|||
use crate::{board::Board, square::Square};
|
||||
|
||||
pub enum BoardState {
|
||||
Finished,
|
||||
Broken(u8),
|
||||
InProgress,
|
||||
}
|
||||
|
||||
fn are_unique<T>(iter: T) -> bool
|
||||
where
|
||||
T: Iterator<Item = u8>,
|
||||
{
|
||||
let mut bitmap: bitmaps::Bitmap<10> = bitmaps::Bitmap::new();
|
||||
|
||||
for item in iter {
|
||||
if bitmap.get(item as usize) {
|
||||
return false;
|
||||
}
|
||||
bitmap.set(item as usize, true);
|
||||
}
|
||||
true
|
||||
}
|
||||
|
||||
pub fn validate_board(board: &Board) -> BoardState {
|
||||
for r in 0..9 {
|
||||
if !are_unique(board.row(r).filter_map(|sq| match sq {
|
||||
Square::Value(x) => Some(x),
|
||||
_ => None,
|
||||
})) {
|
||||
// TODO: Add the index.
|
||||
return BoardState::Broken(0);
|
||||
}
|
||||
}
|
||||
|
||||
for c in 0..9 {
|
||||
if !are_unique(board.col(c).filter_map(|sq| match sq {
|
||||
Square::Value(x) => Some(x),
|
||||
_ => None,
|
||||
})) {
|
||||
// TODO: Add the index.
|
||||
return BoardState::Broken(0);
|
||||
}
|
||||
}
|
||||
|
||||
for b in 0..9 {
|
||||
if !are_unique(board.boxn(b).filter_map(|sq| match sq {
|
||||
Square::Value(x) => Some(x),
|
||||
_ => None,
|
||||
})) {
|
||||
// TODO: Add the index.
|
||||
return BoardState::Broken(0);
|
||||
}
|
||||
}
|
||||
|
||||
if board.squares.iter().any(|sq| match sq {
|
||||
Square::Options(_) => true,
|
||||
_ => false,
|
||||
}) {
|
||||
BoardState::InProgress
|
||||
} else {
|
||||
BoardState::Finished
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue