Bläddra i källkod

Fix variable names in group. Working brute force.

Steve Thielemann 9 månader sedan
förälder
incheckning
c712b8cc6c
2 ändrade filer med 17 tillägg och 8 borttagningar
  1. 4 4
      sudoku/src/group.rs
  2. 13 4
      sudoku/src/sudoku.rs

+ 4 - 4
sudoku/src/group.rs

@@ -349,12 +349,12 @@ impl fmt::Debug for Group {
     }
 }
 
-pub fn for_column(y: u8) -> &'static Group {
-    &GROUP_COLUMN[y as usize]
+pub fn for_column(x: u8) -> &'static Group {
+    &GROUP_COLUMN[x as usize]
 }
 
-pub fn for_row(x: u8) -> &'static Group {
-    &GROUP_ROW[x as usize]
+pub fn for_row(y: u8) -> &'static Group {
+    &GROUP_ROW[y as usize]
 }
 
 pub fn for_cell(i: u8) -> &'static Group {

+ 13 - 4
sudoku/src/sudoku.rs

@@ -349,16 +349,20 @@ impl Sudoku {
         for idx in 0..MAX_SIZE {
             if self.board[idx as usize] == 0 {
                 // Ok, there's a blank here
+
                 let (x,y) = xy(idx);
+                println!("idx={} ({},{})", idx, x, y);
+                self.display();
+
                 'outer:
                 for possible in 1..=9 {
-                    let mut g = for_row(x);
+                    let mut g = for_row(y);
                     for p in g.0 {
                         if self.board[p as usize] == possible {
                             continue 'outer;
                         }
                     }
-                    g = for_column(y);
+                    g = for_column(x);
                     for p in g.0 {
                         if self.board[p as usize] == possible {
                             continue 'outer;
@@ -374,9 +378,14 @@ impl Sudoku {
                     }
 
                     // Ok, it could go here!
+                    println!("({},{})={}", x, y, possible);
+
                     self.board[idx as usize] = possible;
                     if self.puzzle_complete() {
                         *solutions += 1;
+                        println!("**SOLUTION**");
+                        self.display();
+                        println!("***");
                         break;
                     } else {
                         if self.calculate_possible(solutions) {
@@ -384,8 +393,9 @@ impl Sudoku {
                         }
                     }
                 }
+                self.board[idx as usize] = 0;
+                return false
             }
-            self.board[idx as usize] = 0;
         }
 
         false
@@ -399,7 +409,6 @@ impl Sudoku {
 
         let mut solutions :u16 = 0;
         workset.calculate_possible(&mut solutions);
-        
 
         // find number of solutions.
         solutions