Explorar o código

Fixed Group.for_iter, so it does them all.

It was iterating over 3 cells, 3 times.  Fixed.
Steve Thielemann hai 10 meses
pai
achega
3711e9ee91
Modificáronse 1 ficheiros con 46 adicións e 119 borrados
  1. 46 119
      src/main.rs

+ 46 - 119
src/main.rs

@@ -249,129 +249,56 @@ impl Sudoku {
             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.
-            // Notably 1 from the 0,7.
-
-            // Look for single possibility in a group
-            let mut g = Group::new();
-            for i in 0..SIZE {
-                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.
-                values.clear();
-                for gidx in 0..SIZE {
-                    for v in g.items[gidx as usize] {
-                        values.insert(v);
-                    }
-                    // values.extend(self.possible[g.items[gidx as usize] as usize].clone());
-                }
-
-                // Ok!  Now, check for singles:
-                for v in values.iter() {
-                    let mut count = 0;
-                    let mut pos = 0;
-                    for gidx in 0..SIZE {
-                        if self.possible[g.items[gidx as usize] as usize].contains(&v) {
-                            count += 1;
-                            pos = g.items[gidx as usize];
-                        }
-                    }
-                    if count == 1 {
-                        let value = self.possible[pos as usize].iter().next().cloned().unwrap();
-                        println!("Set2 {:?} to {}", xy(pos), value);
-                        self.set(xy(pos).0, xy(pos).1, value);
-                        found_something = true;
-                    }
-                }
-
-                g.for_row(x, y);
-
-                // collect all the possible values within these sets.
-                values.clear();
-                for gidx in 0..SIZE {
-                    for v in g.items[gidx as usize] {
-                        values.insert(v);
-                    }
-
-                    // values.extend(self.possible[g.items[gidx as usize] as usize].clone());
-                }
-
-                // Ok!  Now, check for singles:
-                for v in values.iter() {
-                    let mut count = 0;
-                    let mut pos = 0;
-                    for gidx in 0..SIZE {
-                        if self.possible[g.items[gidx as usize] as usize].contains(&v) {
-                            count += 1;
-                            pos = g.items[gidx as usize];
-                        }
-                    }
-                    if count == 1 {
-                        let value = self.possible[pos as usize].iter().next().cloned().unwrap();
-                        println!("Set2 {:?} to {}", xy(pos), value);
-                        self.set(xy(pos).0, xy(pos).1, value);
-                        found_something = true;
-                    }
-                }
-
-                g.for_column(x, y);
-
-                // collect all the possible values within these sets.
-                values.clear();
-                for gidx in 0..SIZE {
-                    for v in g.items[gidx as usize] {
-                        values.insert(v);
-                    }
-
-                    // values.extend(self.possible[g.items[gidx as usize] as usize].clone());
-                }
+        println!("Looking for pairs...");
+        // PAIR processing.
+        for i in 0..SIZE {
+            g.for_iter(i);
 
-                // Ok!  Now, check for singles:
-                for v in values.iter() {
-                    let mut count = 0;
-                    let mut pos = 0;
-                    for gidx in 0..SIZE {
-                        if self.possible[g.items[gidx as usize] as usize].contains(&v) {
-                            count += 1;
-                            pos = g.items[gidx as usize];
+            for gidx in 0..SIZE - 1 {
+                let gpos = g.items[gidx as usize];
+                if self.possible[gpos as usize].len() == 2 {
+                    // Found a pair
+                    for fidx in gidx + 1..SIZE {
+                        let fpos = g.items[fidx as usize];
+                        if self.possible[fpos as usize].len() == 2 {
+                            // Ok, there's another pair
+                            if self.possible[gpos as usize].is_subset(&self.possible[fpos as usize])
+                            {
+                                // Ok, they have the same values!
+                                println!(
+                                    "Pair found! {} {}: {} {:?} and {} {:?}",
+                                    gidx,
+                                    fidx,
+                                    gpos,
+                                    xy(gpos),
+                                    fpos,
+                                    xy(fpos)
+                                );
+                                // Ok, remove the items in the pair from the cell.
+                                // Don't touch the gpos/fpos records.  Keep those!
+                                let mut values: [u8; 2] = [0,0];
+                                let mut vpos = 0;
+                                for z in self.possible[gpos as usize].iter() {
+                                    values[vpos] = *z;
+                                    vpos +=1;
+                                }
+
+                                for remove in 0..SIZE {
+                                    if (gidx == remove) || (fidx == remove) {
+                                        continue;
+                                    }
+                                    // Ok, these aren't the ones to save, so:
+                                    let rpos = g.items[remove as usize];
+                                    self.possible[rpos as usize].take(&values[0]);
+                                    self.possible[rpos as usize].take(&values[1]);
+                                }
+                            }
                         }
                     }
-                    if count == 1 {
-                        let value = self.possible[pos as usize].iter().next().cloned().unwrap();
-                        println!("Set2 {:?} to {}", xy(pos), value);
-                        self.set(xy(pos).0, xy(pos).1, value);
-                        found_something = true;
-                    }
                 }
             }
-        */
+        }
+
         found_something
     }
 }
@@ -432,7 +359,7 @@ impl Group {
 
     fn for_iter(&mut self, i: u8) {
         let sb_x = (i % 3) * 3;
-        let sb_y = (i % 3) * 3;
+        let sb_y = (i / 3) * 3;
         self.for_block(sb_x, sb_y);
     }