sudoku-solver/src/strategy/naked_single.rs

34 lines
879 B
Rust

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
}
}