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

Added clap argparser.

Next to do: pairs.
Steve Thielemann 10 hónapja
szülő
commit
05eaba7d96
2 módosított fájl, 91 hozzáadás és 1 törlés
  1. 1 0
      Cargo.toml
  2. 90 1
      src/main.rs

+ 1 - 0
Cargo.toml

@@ -6,6 +6,7 @@ edition = "2021"
 # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
 
 [dependencies]
+clap = { version = "4.4.18", features = ["derive"] }
 serde = "1.0.196"
 serde-xml-rs = "0.6.0"
 serde_derive = "1.0.196"

+ 90 - 1
src/main.rs

@@ -1,6 +1,8 @@
 use serde_derive::{Deserialize, Serialize};
 use serde_xml_rs::{from_reader, from_str, to_string};
 
+use clap::Parser;
+
 use std::fmt;
 use std::fs::File;
 
@@ -200,6 +202,74 @@ impl Sudoku {
             }
         }
 
+        let mut g = Group::new();
+        let mut values: HashSet<u8> = HashSet::new();
+
+        let mut group_process = |this: &mut Self, grp: &Group| {
+            // Collect all the possible values within the group.
+            values.clear();
+            for gidx in 0..SIZE {
+                // println!("possible: {:?}", this.possible[grp.items[gidx as usize] as usize]);
+                values.extend(&this.possible[grp.items[gidx as usize] as usize]);
+                // println!("now     : {:?}", this.possible[grp.items[gidx as usize] as usize]);
+            }
+
+            // println!("values {:?}", values);
+
+            // Now, check for singles.
+            for v in values.iter() {
+                let mut count = 0;
+                let mut pos = 0;
+                for gidx in 0..SIZE {
+                    if this.possible[grp.items[gidx as usize] as usize].contains(&v) {
+                        count += 1;
+                        pos = grp.items[gidx as usize];
+                        if count > 1 {
+                            break;
+                        }
+                    }
+                }
+                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);
+                    this.set(xy(pos).0, xy(pos).1, *v);
+                    found_something = true;
+                }
+            }
+        };
+
+        // Change to 0..SIZE ...  Keep it simple.
+        for i in 0..SIZE {
+            g.for_column(i, 1);
+            group_process(self, &g);
+            g.for_row(1, i);
+            group_process(self, &g);
+            g.for_iter(i);
+            group_process(self, &g);
+        }
+
+        /*
+        for i in 0..MAX_SIZE {
+            let (x, y) = xy(i);
+
+            if (x % 3 == 0) && (y % 3 == 0) {
+                g.for_block(x, y);
+                group_process(self, &g);
+            }
+
+            if y == 0 {
+                g.for_column(x, y);
+                group_process(self, &g);
+            }
+
+            if x == 0 {
+                g.for_row(x, y);
+                group_process(self, &g);
+            }
+        }
+        */
+
         /*
             // Code currently isn't working.
             // It seems to eat items that I want and need.
@@ -211,6 +281,7 @@ impl Sudoku {
                 let (x, y) = xy(i);
                 let mut values: HashSet<u8> = HashSet::new();
 
+
                 g.for_block(x, y);
 
                 // collect all the possible values within these sets.
@@ -359,6 +430,12 @@ impl Group {
         }
     }
 
+    fn for_iter(&mut self, i: u8) {
+        let sb_x = (i % 3) * 3;
+        let sb_y = (i % 3) * 3;
+        self.for_block(sb_x, sb_y);
+    }
+
     fn display(&self) {
         for i in 0..SIZE {
             let v = self.items[i as usize];
@@ -368,8 +445,18 @@ impl Group {
     }
 }
 
+// Command line arguments - via clap
+
+#[derive(Parser)]
+struct Cli {
+    /// The path to the file to read
+    filename: std::path::PathBuf,
+}
+
 fn main() {
-    let filename = "../puzzle1";
+    let args = Cli::parse();
+
+    let filename = args.filename; // "../puzzle1";
     let fh = File::open(filename).unwrap();
     let puzzle: Ksudoku = from_reader(fh).unwrap();
     println!("Puzzle: {:?}", puzzle);
@@ -385,8 +472,10 @@ fn main() {
         s.display_possible();
     }
 
+    /*
     s.display();
     s.display_possible();
+    */
 
     /*
     let mut g = Group::new();