diff --git a/src/board.rs b/src/board.rs index 2505e79..4a2396c 100644 --- a/src/board.rs +++ b/src/board.rs @@ -28,33 +28,15 @@ impl Board { let self_copy = self.clone(); for (index, ref mut square) in self.squares.iter_mut().enumerate() { if let Square::Marks(ref mut marks) = square { - self_copy - .row_from_index(index) - .filter_map(|sq| match sq { - Square::Value(x) => Some(x), - _ => None, - }) - .for_each(|value| { - marks.set(value as usize, false); - }); - self_copy - .col_from_index(index) - .filter_map(|sq| match sq { - Square::Value(x) => Some(x), - _ => None, - }) - .for_each(|value| { - marks.set(value as usize, false); - }); - self_copy - .box_from_index(index) - .filter_map(|sq| match sq { - Square::Value(x) => Some(x), - _ => None, - }) - .for_each(|value| { - marks.set(value as usize, false); - }); + self_copy.row_from_index(index).values().for_each(|value| { + marks.set(value as usize, false); + }); + self_copy.col_from_index(index).values().for_each(|value| { + marks.set(value as usize, false); + }); + self_copy.box_from_index(index).values().for_each(|value| { + marks.set(value as usize, false); + }); } } } diff --git a/src/iterators.rs b/src/iterators.rs index 234c5a3..1bed6f1 100644 --- a/src/iterators.rs +++ b/src/iterators.rs @@ -1,3 +1,5 @@ +use bitmaps::Bitmap; + use crate::square::Square; fn to_row(squares: &[Square; 81], row: usize) -> [Square; 9] { @@ -71,6 +73,22 @@ impl SquareIter { curr: 0, } } + + pub fn values(self) -> std::iter::FilterMap Option> { + self.filter_map(|sq| match sq { + Square::Value(v) => Some(v), + _ => None, + }) + } + + pub fn marks( + self, + ) -> std::iter::FilterMap Option>> { + self.filter_map(|sq| match sq { + Square::Marks(m) => Some(m), + _ => None, + }) + } } impl Iterator for SquareIter { diff --git a/src/validator.rs b/src/validator.rs index 7cc8b82..71f8941 100644 --- a/src/validator.rs +++ b/src/validator.rs @@ -23,30 +23,21 @@ where 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, - })) { + if !are_unique(board.row(r).values()) { // 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, - })) { + if !are_unique(board.col(c).values()) { // 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, - })) { + if !are_unique(board.boxn(b).values()) { // TODO: Add the index. return BoardState::Broken(0); }