From 64189981f2f178c3a711ae793e0f6b1a05ebb91a Mon Sep 17 00:00:00 2001 From: Drew Galbraith Date: Thu, 20 Jun 2024 22:50:06 -0700 Subject: [PATCH] Update Square::Options to Marks and index at 1. --- src/board.rs | 13 +++++++------ src/square.rs | 10 +++++++--- src/strategy/naked_single.rs | 4 ++-- src/validator.rs | 2 +- 4 files changed, 17 insertions(+), 12 deletions(-) diff --git a/src/board.rs b/src/board.rs index bb3e661..2505e79 100644 --- a/src/board.rs +++ b/src/board.rs @@ -27,7 +27,7 @@ impl Board { pub fn restrict_marks(&mut self) { let self_copy = self.clone(); for (index, ref mut square) in self.squares.iter_mut().enumerate() { - if let Square::Options(ref mut opt) = square { + if let Square::Marks(ref mut marks) = square { self_copy .row_from_index(index) .filter_map(|sq| match sq { @@ -35,7 +35,7 @@ impl Board { _ => None, }) .for_each(|value| { - opt.set((value - 1) as usize, false); + marks.set(value as usize, false); }); self_copy .col_from_index(index) @@ -44,7 +44,7 @@ impl Board { _ => None, }) .for_each(|value| { - opt.set((value - 1) as usize, false); + marks.set(value as usize, false); }); self_copy .box_from_index(index) @@ -53,7 +53,7 @@ impl Board { _ => None, }) .for_each(|value| { - opt.set((value - 1) as usize, false); + marks.set(value as usize, false); }); } } @@ -99,8 +99,9 @@ impl Board { self.squares .iter() .map(|sq| match sq { - Square::Options(opts) => { - opts.into_iter() + Square::Marks(marks) => { + marks + .into_iter() .map(|a| a + 1) .fold(String::new(), |mut a, b| { a.push_str(&b.to_string()); diff --git a/src/square.rs b/src/square.rs index 7a741a0..675ccd7 100644 --- a/src/square.rs +++ b/src/square.rs @@ -3,7 +3,7 @@ use bitmaps::Bitmap; #[derive(Clone, Debug)] pub enum Square { Value(u8), - Options(Bitmap<9>), + Marks(Bitmap<10>), } impl Square { @@ -18,14 +18,18 @@ impl Square { '7' => Square::Value(7), '8' => Square::Value(8), '9' => Square::Value(9), - '.' => Square::Options(Bitmap::mask(9)), + '.' => { + let mut bm = Bitmap::mask(10); + bm.set(0, false); + Square::Marks(bm) + } _ => panic!("Unexpected character in input: {}", c), } } pub fn to_str(&self) -> String { match self { - Square::Options(_) => " ".to_string(), + Square::Marks(_) => " ".to_string(), Square::Value(v) => v.to_string(), } } diff --git a/src/strategy/naked_single.rs b/src/strategy/naked_single.rs index 43c960a..46d6ba7 100644 --- a/src/strategy/naked_single.rs +++ b/src/strategy/naked_single.rs @@ -15,13 +15,13 @@ impl Strategy for NakedSingle { .iter() .enumerate() .filter_map(|(index, square)| match square { - Square::Options(marks) => Some((index, marks)), + Square::Marks(marks) => Some((index, marks)), _ => None, }) .find(|(_index, marks)| marks.len() == 1) .map(|(index, marks)| NakedSingle { index, - value: marks.first_index().unwrap() as u8 + 1, + value: marks.first_index().unwrap() as u8, }) } diff --git a/src/validator.rs b/src/validator.rs index 42ef7f4..7cc8b82 100644 --- a/src/validator.rs +++ b/src/validator.rs @@ -53,7 +53,7 @@ pub fn validate_board(board: &Board) -> BoardState { } if board.squares.iter().any(|sq| match sq { - Square::Options(_) => true, + Square::Marks(_) => true, _ => false, }) { BoardState::InProgress