Explorar el Código

Changed group.

Steve Thielemann hace 9 meses
padre
commit
6e2d8b03ea
Se han modificado 4 ficheros con 94 adiciones y 144 borrados
  1. 1 5
      src/main.rs
  2. 76 126
      sudoku/src/group.rs
  3. 1 1
      sudoku/src/ksudoku.rs
  4. 16 12
      sudoku/src/sudoku.rs

+ 1 - 5
src/main.rs

@@ -1,10 +1,7 @@
-
 use clap::Parser;
 
-use sudoku::sudoku::*;
 use sudoku::ksudoku::load_ksudoku;
-
-
+use sudoku::sudoku::*;
 
 // Command line arguments - via clap
 
@@ -30,5 +27,4 @@ fn main() {
         s.display();
         s.display_possible();
     }
-
 }

+ 76 - 126
sudoku/src/group.rs

@@ -12,16 +12,18 @@ const WIDTH: u8 = 9;
 /// Define a Group of sudoku positions.
 /// This defines rows, columns, and cells for analysis.
 #[derive(PartialEq)]
+pub struct Group( pub [u8; WIDTH as usize]);
+
+/*
 pub struct Group {
     pub items: [u8; WIDTH as usize],
 }
+ */
 
-struct Group2([u8; WIDTH as usize]);
 
 /// Group of positions for rows.
 const GROUP_ROW: [Group; 9] = [
-    Group {
-        items: [
+    Group([      
             pos1(1, 1),
             pos1(2, 1),
             pos1(3, 1),
@@ -31,10 +33,8 @@ const GROUP_ROW: [Group; 9] = [
             pos1(7, 1),
             pos1(8, 1),
             pos1(9, 1),
-        ],
-    },
-    Group {
-        items: [
+]),
+    Group ([
             pos1(1, 2),
             pos1(2, 2),
             pos1(3, 2),
@@ -44,10 +44,9 @@ const GROUP_ROW: [Group; 9] = [
             pos1(7, 2),
             pos1(8, 2),
             pos1(9, 2),
-        ],
-    },
-    Group {
-        items: [
+]),
+   
+    Group ([
             pos1(1, 3),
             pos1(2, 3),
             pos1(3, 3),
@@ -57,10 +56,8 @@ const GROUP_ROW: [Group; 9] = [
             pos1(7, 3),
             pos1(8, 3),
             pos1(9, 3),
-        ],
-    },
-    Group {
-        items: [
+]),
+    Group ([
             pos1(1, 4),
             pos1(2, 4),
             pos1(3, 4),
@@ -70,10 +67,8 @@ const GROUP_ROW: [Group; 9] = [
             pos1(7, 4),
             pos1(8, 4),
             pos1(9, 4),
-        ],
-    },
-    Group {
-        items: [
+]),
+    Group([
             pos1(1, 5),
             pos1(2, 5),
             pos1(3, 5),
@@ -83,10 +78,8 @@ const GROUP_ROW: [Group; 9] = [
             pos1(7, 5),
             pos1(8, 5),
             pos1(9, 5),
-        ],
-    },
-    Group {
-        items: [
+]),
+    Group([
             pos1(1, 6),
             pos1(2, 6),
             pos1(3, 6),
@@ -96,10 +89,8 @@ const GROUP_ROW: [Group; 9] = [
             pos1(7, 6),
             pos1(8, 6),
             pos1(9, 6),
-        ],
-    },
-    Group {
-        items: [
+]),
+    Group([
             pos1(1, 7),
             pos1(2, 7),
             pos1(3, 7),
@@ -109,10 +100,8 @@ const GROUP_ROW: [Group; 9] = [
             pos1(7, 7),
             pos1(8, 7),
             pos1(9, 7),
-        ],
-    },
-    Group {
-        items: [
+]),
+    Group ([
             pos1(1, 8),
             pos1(2, 8),
             pos1(3, 8),
@@ -122,10 +111,8 @@ const GROUP_ROW: [Group; 9] = [
             pos1(7, 8),
             pos1(8, 8),
             pos1(9, 8),
-        ],
-    },
-    Group {
-        items: [
+    ]),
+    Group ([
             pos1(1, 9),
             pos1(2, 9),
             pos1(3, 9),
@@ -135,14 +122,12 @@ const GROUP_ROW: [Group; 9] = [
             pos1(7, 9),
             pos1(8, 9),
             pos1(9, 9),
-        ],
-    },
+    ])
 ];
 
 /// Group of positions for columns.
 const GROUP_COLUMN: [Group; 9] = [
-    Group {
-        items: [
+    Group([
             pos1(1, 1),
             pos1(1, 2),
             pos1(1, 3),
@@ -152,10 +137,8 @@ const GROUP_COLUMN: [Group; 9] = [
             pos1(1, 7),
             pos1(1, 8),
             pos1(1, 9),
-        ],
-    },
-    Group {
-        items: [
+    ]),
+    Group ([
             pos1(2, 1),
             pos1(2, 2),
             pos1(2, 3),
@@ -165,10 +148,8 @@ const GROUP_COLUMN: [Group; 9] = [
             pos1(2, 7),
             pos1(2, 8),
             pos1(2, 9),
-        ],
-    },
-    Group {
-        items: [
+    ]),
+    Group ([
             pos1(3, 1),
             pos1(3, 2),
             pos1(3, 3),
@@ -178,10 +159,8 @@ const GROUP_COLUMN: [Group; 9] = [
             pos1(3, 7),
             pos1(3, 8),
             pos1(3, 9),
-        ],
-    },
-    Group {
-        items: [
+    ]),
+    Group ([
             pos1(4, 1),
             pos1(4, 2),
             pos1(4, 3),
@@ -191,10 +170,8 @@ const GROUP_COLUMN: [Group; 9] = [
             pos1(4, 7),
             pos1(4, 8),
             pos1(4, 9),
-        ],
-    },
-    Group {
-        items: [
+    ]),
+    Group ([
             pos1(5, 1),
             pos1(5, 2),
             pos1(5, 3),
@@ -204,10 +181,8 @@ const GROUP_COLUMN: [Group; 9] = [
             pos1(5, 7),
             pos1(5, 8),
             pos1(5, 9),
-        ],
-    },
-    Group {
-        items: [
+    ]),
+    Group ([
             pos1(6, 1),
             pos1(6, 2),
             pos1(6, 3),
@@ -217,10 +192,8 @@ const GROUP_COLUMN: [Group; 9] = [
             pos1(6, 7),
             pos1(6, 8),
             pos1(6, 9),
-        ],
-    },
-    Group {
-        items: [
+    ]),
+    Group ([
             pos1(7, 1),
             pos1(7, 2),
             pos1(7, 3),
@@ -230,10 +203,8 @@ const GROUP_COLUMN: [Group; 9] = [
             pos1(7, 7),
             pos1(7, 8),
             pos1(7, 9),
-        ],
-    },
-    Group {
-        items: [
+    ]),
+    Group ([
             pos1(8, 1),
             pos1(8, 2),
             pos1(8, 3),
@@ -243,10 +214,8 @@ const GROUP_COLUMN: [Group; 9] = [
             pos1(8, 7),
             pos1(8, 8),
             pos1(8, 9),
-        ],
-    },
-    Group {
-        items: [
+    ]),
+    Group ([
             pos1(9, 1),
             pos1(9, 2),
             pos1(9, 3),
@@ -256,14 +225,12 @@ const GROUP_COLUMN: [Group; 9] = [
             pos1(9, 7),
             pos1(9, 8),
             pos1(9, 9),
-        ],
-    },
+    ]),
 ];
 
 /// Group of positions for cells (3x3 grid).
 const GROUP_CELL: [Group; 9] = [
-    Group {
-        items: [
+    Group ([
             pos1(1, 1),
             pos1(2, 1),
             pos1(3, 1),
@@ -273,10 +240,8 @@ const GROUP_CELL: [Group; 9] = [
             pos1(1, 3),
             pos1(2, 3),
             pos1(3, 3),
-        ],
-    },
-    Group {
-        items: [
+        ]),
+    Group ([
             pos1(4, 1),
             pos1(5, 1),
             pos1(6, 1),
@@ -286,10 +251,8 @@ const GROUP_CELL: [Group; 9] = [
             pos1(4, 3),
             pos1(5, 3),
             pos1(6, 3),
-        ],
-    },
-    Group {
-        items: [
+        ]),
+    Group ([
             pos1(7, 1),
             pos1(8, 1),
             pos1(9, 1),
@@ -299,10 +262,8 @@ const GROUP_CELL: [Group; 9] = [
             pos1(7, 3),
             pos1(8, 3),
             pos1(9, 3),
-        ],
-    },
-    Group {
-        items: [
+        ]),
+    Group ([
             pos1(1, 4),
             pos1(2, 4),
             pos1(3, 4),
@@ -312,10 +273,8 @@ const GROUP_CELL: [Group; 9] = [
             pos1(1, 6),
             pos1(2, 6),
             pos1(3, 6),
-        ],
-    },
-    Group {
-        items: [
+        ]),
+    Group ([
             pos1(4, 4),
             pos1(5, 4),
             pos1(6, 4),
@@ -325,10 +284,8 @@ const GROUP_CELL: [Group; 9] = [
             pos1(4, 6),
             pos1(5, 6),
             pos1(6, 6),
-        ],
-    },
-    Group {
-        items: [
+        ]),
+    Group ([
             pos1(7, 4),
             pos1(8, 4),
             pos1(9, 4),
@@ -338,10 +295,8 @@ const GROUP_CELL: [Group; 9] = [
             pos1(7, 6),
             pos1(8, 6),
             pos1(9, 6),
-        ],
-    },
-    Group {
-        items: [
+        ]),
+    Group ([
             pos1(1, 7),
             pos1(2, 7),
             pos1(3, 7),
@@ -351,10 +306,8 @@ const GROUP_CELL: [Group; 9] = [
             pos1(1, 9),
             pos1(2, 9),
             pos1(3, 9),
-        ],
-    },
-    Group {
-        items: [
+        ]),
+    Group ([
             pos1(4, 7),
             pos1(5, 7),
             pos1(6, 7),
@@ -364,10 +317,8 @@ const GROUP_CELL: [Group; 9] = [
             pos1(4, 9),
             pos1(5, 9),
             pos1(6, 9),
-        ],
-    },
-    Group {
-        items: [
+        ]),
+    Group ([
             pos1(7, 7),
             pos1(8, 7),
             pos1(9, 7),
@@ -377,26 +328,25 @@ const GROUP_CELL: [Group; 9] = [
             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() {
             write!(f, "Group {{ {}[{},{}], {}[{},{}], {}[{},{}], {}[{},{}], {}[{},{}], {}[{},{}], {}[{},{}], {}[{},{}], {}[{},{}] }}",
-                self.items[0], xy(self.items[0]).0, xy(self.items[0]).1,
-                self.items[1], xy(self.items[1]).0, xy(self.items[1]).1,
-                self.items[2], xy(self.items[2]).0, xy(self.items[2]).1,
-                self.items[3], xy(self.items[3]).0, xy(self.items[3]).1,
-                self.items[4], xy(self.items[4]).0, xy(self.items[4]).1,
-                self.items[5], xy(self.items[5]).0, xy(self.items[5]).1,
-                self.items[6], xy(self.items[6]).0, xy(self.items[6]).1,
-                self.items[7], xy(self.items[7]).0, xy(self.items[7]).1,
-                self.items[8], xy(self.items[8]).0, xy(self.items[8]).1,
+                self.0[0], xy(self.0[0]).0, xy(self.0[0]).1,
+                self.0[1], xy(self.0[1]).0, xy(self.0[1]).1,
+                self.0[2], xy(self.0[2]).0, xy(self.0[2]).1,
+                self.0[3], xy(self.0[3]).0, xy(self.0[3]).1,
+                self.0[4], xy(self.0[4]).0, xy(self.0[4]).1,
+                self.0[5], xy(self.0[5]).0, xy(self.0[5]).1,
+                self.0[6], xy(self.0[6]).0, xy(self.0[6]).1,
+                self.0[7], xy(self.0[7]).0, xy(self.0[7]).1,
+                self.0[8], xy(self.0[8]).0, xy(self.0[8]).1,
             )
         } else {
-            f.debug_struct("Group").field("items", &self.items).finish()
+            f.debug_struct("Group").field("items", &self).finish()
         }
     }
 }
@@ -466,19 +416,19 @@ mod tests {
 
 impl Group {
     pub fn new() -> Self {
-        Group {
-            items: [0; WIDTH as usize],
-        }
+        Group (
+             [0; WIDTH as usize],
+            )
     }
 
     pub fn for_column(&mut self, x: u8, _y: u8) {
         for y in 0..WIDTH {
-            self.items[y as usize] = pos(x, y);
+            self.0[y as usize] = pos(x, y);
         }
     }
     pub fn for_row(&mut self, _x: u8, y: u8) {
         for x in 0..WIDTH {
-            self.items[x as usize] = pos(x, y);
+            self.0[x as usize] = pos(x, y);
         }
     }
     pub fn for_block(&mut self, x: u8, y: u8) {
@@ -489,7 +439,7 @@ impl Group {
             let ix = i % 3;
             let iy = i / 3;
             // println!("i = {}, sb.x = {} sb.y = {}, ix = {} iy = {}", i, sb_x, sb_y, ix, iy);
-            self.items[i as usize] = pos(sb_x + ix, sb_y + iy);
+            self.0[i as usize] = pos(sb_x + ix, sb_y + iy);
         }
     }
 
@@ -501,7 +451,7 @@ impl Group {
 
     pub fn display(&self) {
         for i in 0..WIDTH {
-            let v = self.items[i as usize];
+            let v = self.0[i as usize];
             print!("{} [{},{}] ", v, xy(v).0, xy(v).1);
         }
         println!("");

+ 1 - 1
sudoku/src/ksudoku.rs

@@ -1,7 +1,7 @@
 // Read a Ksudoku's XML save file.
 
 use serde_derive::{Deserialize, Serialize};
-use serde_xml_rs::{from_reader, from_str, to_string};
+use serde_xml_rs::{from_reader}; // , from_str, to_string};
 
 use std::error::Error;
 use std::fs::File;

+ 16 - 12
sudoku/src/sudoku.rs

@@ -141,19 +141,19 @@ impl Sudoku {
         // Ok, update the possible
         let mut g = for_row(y);
         // g.for_row(x, y);
-        for g in g.items {
+        for g in g.0 {
             // remove value from these sets.
             self.possible[g as usize].take(&value);
         }
         g = for_column(x);
         // g.for_column(x, y);
-        for g in g.items {
+        for g in g.0 {
             // remove value from these sets.
             self.possible[g as usize].take(&value);
         }
         g = for_cell(which_cell(x, y));
         // g.for_block(x, y);
-        for g in g.items {
+        for g in g.0 {
             // remove value from these sets.
             self.possible[g as usize].take(&value);
         }
@@ -165,19 +165,19 @@ impl Sudoku {
         // Ok, update the possible
         let mut g = Group::new();
         g.for_row(x, y);
-        for g in g.items {
+        for g in g.0 {
             // remove value from these sets.
             self.possible[g as usize].take(&value);
         }
 
         g.for_column(x, y);
-        for g in g.items {
+        for g in g.0 {
             // remove value from these sets.
             self.possible[g as usize].take(&value);
         }
 
         g.for_block(x, y);
-        for g in g.items {
+        for g in g.0 {
             // remove value from these sets.
             self.possible[g as usize].take(&value);
         }
@@ -261,7 +261,7 @@ impl Sudoku {
             values.clear();
             for gidx in 0..WIDTH {
                 // println!("possible: {:?}", this.possible[grp.items[gidx as usize] as usize]);
-                values.extend(&this.possible[grp.items[gidx as usize] as usize]);
+                values.extend(&this.possible[grp.0[gidx as usize] as usize]);
                 // println!("now     : {:?}", this.possible[grp.items[gidx as usize] as usize]);
             }
 
@@ -272,9 +272,9 @@ impl Sudoku {
                 let mut count = 0;
                 let mut pos = 0;
                 for gidx in 0..WIDTH {
-                    if this.possible[grp.items[gidx as usize] as usize].contains(&v) {
+                    if this.possible[grp.0[gidx as usize] as usize].contains(&v) {
                         count += 1;
-                        pos = grp.items[gidx as usize];
+                        pos = grp.0[gidx as usize];
                         if count > 1 {
                             break;
                         }
@@ -303,17 +303,21 @@ impl Sudoku {
             group_process(self, &g);
         }
 
+        if found_something {
+            return found_something;
+        }
+
         println!("Looking for pairs...");
         // PAIR processing.
         for i in 0..WIDTH {
             g.for_iter(i);
 
             for gidx in 0..WIDTH - 1 {
-                let gpos = g.items[gidx as usize];
+                let gpos = g.0[gidx as usize];
                 if self.possible[gpos as usize].len() == 2 {
                     // Found a pair
                     for fidx in gidx + 1..WIDTH {
-                        let fpos = g.items[fidx as usize];
+                        let fpos = g.0[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])
@@ -335,7 +339,7 @@ impl Sudoku {
                                         continue;
                                     }
                                     // Ok, these aren't the ones to save, so:
-                                    let rpos = g.items[remove as usize];
+                                    let rpos = g.0[remove as usize];
                                     if self.possible[rpos as usize].take(&values[0]).is_some() {
                                         found_something = true;
                                         pair_removed = true;