Forráskód Böngészése

Added debug flag for output.

Cleaned up bit tests, so they pass.
get/set start at 0, not 1.
I use 1-9 in the solver code.
Steve Thielemann 9 hónapja
szülő
commit
81e9128bc3
2 módosított fájl, 57 hozzáadás és 26 törlés
  1. 17 3
      src/main.rs
  2. 40 23
      sudoku/src/sudoku.rs

+ 17 - 3
src/main.rs

@@ -21,9 +21,15 @@ fn main() {
             )
             .action(ArgAction::SetTrue),
         )
-        .arg(arg!(-b --brute ... "Brute Force Solver").action(ArgAction::SetTrue))
+        .arg(arg!(-b --brute ... "Brute Force solver").action(ArgAction::SetTrue))
+        .arg(arg!(-d --debug ... "Debug output").action(ArgAction::SetTrue))
         .get_matches();
 
+    let mut debug : bool = false;
+    if args.get_flag("debug") {
+        debug = true;
+    }
+
     if args.get_flag("make") {
         s.make();
         s.display();
@@ -35,15 +41,23 @@ fn main() {
         // Ksudoku is stored TLD.
         s.load_from_tld('b', '_', puzzle.as_str());
         s.display();
-        s.display_possible();
 
         if args.get_flag("brute") {
             println!("Solutions: {}", s.bruteforce_solver());
         } else {
-            while s.solve() {
+            if debug {
+                s.display_possible();
+                }
+        
+            while s.solve(debug) {
                 println!("Try it again...");
+                if debug {
                 s.display();
                 s.display_possible();
+                }
+            }
+            if !debug {
+                s.display();
             }
         }
     }

+ 40 - 23
sudoku/src/sudoku.rs

@@ -21,32 +21,36 @@ use rand_chacha::ChaCha20Rng;
 #[derive(Debug, Copy, Clone, PartialEq)]
 pub struct Possible(u16);
 
+/// Set bits number of bits to 1 (true)
 pub const fn set_bits(bits: u8) -> u16 {
     (1 << (bits)) - 1
 }
 
 impl Possible {
+    /// clear all bits
     pub fn clear(&mut self) {
         self.0 = 0;
     }
 
+    /// set bit to state of value.
     pub fn set(&mut self, bit: u8, value: bool) {
-        // print!("{} set {}={}", self.0, bit, value);
-        self.0.set_bit((bit - 1) as usize, value);
-        // println!("{}", self.0);
+        self.0.set_bit(bit as usize, value);
     }
 
+    /// get state of bit.
     pub fn get(&self, bit: u8) -> bool {
-        self.0.get_bit((bit - 1) as usize)
+        self.0.get_bit(bit as usize)
     }
 
+    /// set bits on, given number of bits initially to set.
     pub fn set_bits(&mut self, bits: u8) {
         self.0 = set_bits(bits);
     }
 
+    /// count number of bits set.
     pub fn count_set(&self) -> u8 {
         let mut count = 0;
-        for i in 1..u16::BIT_LENGTH {
+        for i in 0..u16::BIT_LENGTH {
             if self.get(i as u8) {
                 count += 1;
             }
@@ -73,11 +77,11 @@ impl<'a> Iterator for PossibleIterator<'a> {
     type Item = u8;
 
     fn next(&mut self) -> Option<u8> {
-        while (self.index <= u16::BIT_LENGTH as u8) && (!self.possible.get(self.index)) {
+        while (self.index < u16::BIT_LENGTH as u8) && (!self.possible.get(self.index)) {
             self.index += 1;
             // println!("index = {}", self.index);
         }
-        if self.index >= u16::BIT_LENGTH as u8 {
+        if self.index == u16::BIT_LENGTH as u8 {
             None
         } else {
             self.index += 1;
@@ -121,7 +125,9 @@ mod tests {
 
     #[test]
     fn check_bits() {
-        let mut p = Possible(set_bits(5));
+        // Set bits 0-5 (6 bits total)
+        let p = Possible(set_bits(6));
+
         for i in 0..6 {
             let result = p.get(i);
             assert_eq!(result, true);
@@ -166,7 +172,8 @@ let arr: [Vec<u32>; 10] = [(); 10].map(|_| Vec::with_capacity(100));
 impl Sudoku {
     pub fn new() -> Self {
         // let b : HashSet<u8> = HashSet::from_iter(1..=9);
-        let initial: Possible = Possible(set_bits(9));
+        let mut initial: Possible = Possible(set_bits(10));
+        initial.set(0, false);
 
         let s = Sudoku {
             board: [0; MAX_SIZE as usize],
@@ -176,7 +183,9 @@ impl Sudoku {
     }
 
     pub fn clear(&mut self) {
-        let initial = Possible(set_bits(9));
+        let mut initial = Possible(set_bits(10));
+        initial.set(0, false);
+
         self.board = [0; MAX_SIZE as usize];
         self.possible = [initial; MAX_SIZE as usize];
     }
@@ -506,7 +515,7 @@ impl Sudoku {
         }
     }
 
-    pub fn solve(&mut self) -> bool {
+    pub fn solve(&mut self, debug: bool) -> bool {
         // Pass 1: Look for singles in the possible sets.
         let mut found_something = false;
 
@@ -515,7 +524,9 @@ impl Sudoku {
                 // Get the value
                 let value = self.possible[i as usize].iter().next().unwrap();
                 // Found one!
-                println!("Set1 {:?} to {}", xy(i), value);
+                if debug {
+                    println!("Set1 {:?} to {}", xy(i), value);
+                }
                 self.set(xy(i).0, xy(i).1, value);
                 found_something = true;
             }
@@ -555,7 +566,9 @@ impl Sudoku {
                 if count == 1 {
                     // don't need this, it was v!
                     // let value = this.possible[pos as usize].iter().next().cloned().unwrap();
-                    println!("Set2 {:?} to {}", xy(pos), v);
+                    if debug {
+                        println!("Set2 {:?} to {}", xy(pos), v);
+                    }
                     this.set(xy(pos).0, xy(pos).1, v);
                     found_something = true;
                 }
@@ -579,7 +592,9 @@ impl Sudoku {
             return found_something;
         }
 
-        println!("Looking for pairs...");
+        if debug {
+            println!("Looking for pairs...");
+        }
         // PAIR processing.
         for i in 0..WIDTH {
             g.for_iter(i);
@@ -692,15 +707,17 @@ impl Sudoku {
                                 }
 
                                 if pair_removed {
-                                    println!(
-                                        "Pair found! {} {}: {} {:?} and {} {:?} !",
-                                        gidx,
-                                        fidx,
-                                        gpos,
-                                        xy(gpos),
-                                        fpos,
-                                        xy(fpos)
-                                    );
+                                    if debug {
+                                        println!(
+                                            "Pair found! {} {}: {} {:?} and {} {:?} !",
+                                            gidx,
+                                            fidx,
+                                            gpos,
+                                            xy(gpos),
+                                            fpos,
+                                            xy(fpos)
+                                        );
+                                    }
                                 }
                             }
                         }