Browse Source

Updated with const information.

We have tests to confirm the const data is correct.
Steve Thielemann 9 months ago
parent
commit
5630c7bfd8
1 changed files with 94 additions and 0 deletions
  1. 94 0
      src/main.rs

+ 94 - 0
src/main.rs

@@ -51,6 +51,10 @@ const fn pos(x: u8, y: u8) -> u8 {
     x + (y * SIZE as u8)
 }
 
+const fn pos1(x:u8, y:u8) -> u8 {
+    (x-1) + ((y-1) * SIZE as u8)
+}
+
 // Translate post to x,y in board.
 const fn xy(pos: u8) -> (u8, u8) {
     ((pos % SIZE), (pos / SIZE))
@@ -371,10 +375,47 @@ impl Sudoku {
 }
 
 // #[derive(Debug)]
+#[derive(PartialEq)]
 struct Group {
     items: [u8; SIZE as usize],
 }
 
+const ROW : [Group;9] = [
+    Group{ items: [pos1(1,1), pos1(2,1), pos1(3,1), pos1(4,1), pos1(5,1), pos1(6,1), pos1(7,1), pos1(8,1), pos1(9,1)]},
+    Group{ items: [pos1(1,2), pos1(2,2), pos1(3,2), pos1(4,2), pos1(5,2), pos1(6,2), pos1(7,2), pos1(8,2), pos1(9,2)]},
+    Group{ items: [pos1(1,3), pos1(2,3), pos1(3,3), pos1(4,3), pos1(5,3), pos1(6,3), pos1(7,3), pos1(8,3), pos1(9,3)]},
+    Group{ items: [pos1(1,4), pos1(2,4), pos1(3,4), pos1(4,4), pos1(5,4), pos1(6,4), pos1(7,4), pos1(8,4), pos1(9,4)]},
+    Group{ items: [pos1(1,5), pos1(2,5), pos1(3,5), pos1(4,5), pos1(5,5), pos1(6,5), pos1(7,5), pos1(8,5), pos1(9,5)]},
+    Group{ items: [pos1(1,6), pos1(2,6), pos1(3,6), pos1(4,6), pos1(5,6), pos1(6,6), pos1(7,6), pos1(8,6), pos1(9,6)]},
+    Group{ items: [pos1(1,7), pos1(2,7), pos1(3,7), pos1(4,7), pos1(5,7), pos1(6,7), pos1(7,7), pos1(8,7), pos1(9,7)]},
+    Group{ items: [pos1(1,8), pos1(2,8), pos1(3,8), pos1(4,8), pos1(5,8), pos1(6,8), pos1(7,8), pos1(8,8), pos1(9,8)]},
+    Group{ items: [pos1(1,9), pos1(2,9), pos1(3,9), pos1(4,9), pos1(5,9), pos1(6,9), pos1(7,9), pos1(8,9), pos1(9,9)]},
+];
+
+const COLUMN : [Group;9] = [
+    Group{ items: [pos1(1,1), pos1(1,2), pos1(1,3), pos1(1,4), pos1(1,5), pos1(1,6), pos1(1,7), pos1(1,8), pos1(1,9)]},
+    Group{ items: [pos1(2,1), pos1(2,2), pos1(2,3), pos1(2,4), pos1(2,5), pos1(2,6), pos1(2,7), pos1(2,8), pos1(2,9)]},
+    Group{ items: [pos1(3,1), pos1(3,2), pos1(3,3), pos1(3,4), pos1(3,5), pos1(3,6), pos1(3,7), pos1(3,8), pos1(3,9)]},
+    Group{ items: [pos1(4,1), pos1(4,2), pos1(4,3), pos1(4,4), pos1(4,5), pos1(4,6), pos1(4,7), pos1(4,8), pos1(4,9)]},
+    Group{ items: [pos1(5,1), pos1(5,2), pos1(5,3), pos1(5,4), pos1(5,5), pos1(5,6), pos1(5,7), pos1(5,8), pos1(5,9)]},
+    Group{ items: [pos1(6,1), pos1(6,2), pos1(6,3), pos1(6,4), pos1(6,5), pos1(6,6), pos1(6,7), pos1(6,8), pos1(6,9)]},
+    Group{ items: [pos1(7,1), pos1(7,2), pos1(7,3), pos1(7,4), pos1(7,5), pos1(7,6), pos1(7,7), pos1(7,8), pos1(7,9)]},
+    Group{ items: [pos1(8,1), pos1(8,2), pos1(8,3), pos1(8,4), pos1(8,5), pos1(8,6), pos1(8,7), pos1(8,8), pos1(8,9)]},
+    Group{ items: [pos1(9,1), pos1(9,2), pos1(9,3), pos1(9,4), pos1(9,5), pos1(9,6), pos1(9,7), pos1(9,8), pos1(9,9)]},
+];
+
+const CELL : [Group;9] = [
+    Group{ items: [pos1(1,1), pos1(2,1), pos1(3,1), pos1(1,2), pos1(2,2), pos1(3,2), pos1(1,3), pos1(2,3), pos1(3,3)]},
+    Group{ items: [pos1(4,1), pos1(5,1), pos1(6,1), pos1(4,2), pos1(5,2), pos1(6,2), pos1(4,3), pos1(5,3), pos1(6,3)]},
+    Group{ items: [pos1(7,1), pos1(8,1), pos1(9,1), pos1(7,2), pos1(8,2), pos1(9,2), pos1(7,3), pos1(8,3), pos1(9,3)]},
+    Group{ items: [pos1(1,4), pos1(2,4), pos1(3,4), pos1(1,5), pos1(2,5), pos1(3,5), pos1(1,6), pos1(2,6), pos1(3,6)]},
+    Group{ items: [pos1(4,4), pos1(5,4), pos1(6,4), pos1(4,5), pos1(5,5), pos1(6,5), pos1(4,6), pos1(5,6), pos1(6,6)]},
+    Group{ items: [pos1(7,4), pos1(8,4), pos1(9,4), pos1(7,5), pos1(8,5), pos1(9,5), pos1(7,6), pos1(8,6), pos1(9,6)]},
+    Group{ items: [pos1(1,7), pos1(2,7), pos1(3,7), pos1(1,8), pos1(2,8), pos1(3,8), pos1(1,9), pos1(2,9), pos1(3,9)]},
+    Group{ items: [pos1(4,7), pos1(5,7), pos1(6,7), pos1(4,8), pos1(5,8), pos1(6,8), pos1(4,9), pos1(5,9), pos1(6,9)]},
+    Group{ items: [pos1(7,7), pos1(8,7), pos1(9,7), pos1(7,8), pos1(8,8), pos1(9,8), pos1(7,9), pos1(8,9), pos1(9,9)]},        
+];
+
 impl fmt::Debug for Group {
     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
         if f.alternate() {
@@ -395,6 +436,59 @@ impl fmt::Debug for Group {
     }
 }
 
+fn for_column(i:u8) -> &'static Group {
+    &COLUMN[i as usize]
+}
+
+fn for_row(i:u8) -> &'static Group {
+    &ROW[i as usize]
+}
+
+fn for_cell(i:u8) -> &'static Group {
+    &CELL[i as usize]
+}
+
+#[cfg(test)]
+mod tests {
+    use crate::Group;
+    use crate::{for_column, for_row, for_cell};
+
+    #[test]
+    fn check_columns() {
+        let mut g = Group::new();
+
+        for i in 0..9 {
+            println!("Index={}", i);
+            g.for_column(i, 1);
+            let new_g = for_column(i);
+            assert_eq!(g, *new_g);
+        }
+    }
+
+    #[test]
+    fn check_rows() {
+        let mut g = Group::new();
+
+        for i in 0..9 {
+            println!("Index={}", i);
+            g.for_row(1, i);
+            let new_g = for_row(i);
+            assert_eq!(g, *new_g);
+        }
+    }
+
+    #[test]
+    fn check_cells() {
+        let mut g = Group::new();
+        for i in 0..9 {
+            println!("Index={}", i);
+            g.for_block( (i%3)*3, (i/3)*3);
+            let new_g = for_cell(i);
+            assert_eq!(g, *new_g);
+        }
+    }
+}
+
 impl Group {
     fn new() -> Self {
         Group {