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
33
src/strategy/naked_single.rs
Normal file
33
src/strategy/naked_single.rs
Normal file
|
|
@ -0,0 +1,33 @@
|
|||
use crate::{board::Board, square::Square, strategy::Strategy};
|
||||
|
||||
pub struct NakedSingle {
|
||||
index: usize,
|
||||
value: u8,
|
||||
}
|
||||
|
||||
impl Strategy for NakedSingle {
|
||||
fn find_instance(board: &crate::board::Board) -> Option<Self>
|
||||
where
|
||||
Self: Sized,
|
||||
{
|
||||
board
|
||||
.squares
|
||||
.iter()
|
||||
.enumerate()
|
||||
.filter_map(|(index, square)| match square {
|
||||
Square::Options(marks) => Some((index, marks)),
|
||||
_ => None,
|
||||
})
|
||||
.find(|(_index, marks)| marks.len() == 1)
|
||||
.map(|(index, marks)| NakedSingle {
|
||||
index,
|
||||
value: marks.first_index().unwrap() as u8 + 1,
|
||||
})
|
||||
}
|
||||
|
||||
fn apply(&self, mut board: Board) -> Board {
|
||||
board.squares[self.index] = Square::Value(self.value);
|
||||
board.restrict_marks();
|
||||
board
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue