|
@@ -152,6 +152,8 @@ pub const fn xy(pos: u8) -> (u8, u8) {
|
|
((pos % WIDTH), (pos / WIDTH))
|
|
((pos % WIDTH), (pos / WIDTH))
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+const DEBUG_OUTPUT: bool = false;
|
|
|
|
+
|
|
/*
|
|
/*
|
|
(0 .. 10)
|
|
(0 .. 10)
|
|
.map(|_| HashSet::<usize>::new())
|
|
.map(|_| HashSet::<usize>::new())
|
|
@@ -341,13 +343,15 @@ impl Sudoku {
|
|
}
|
|
}
|
|
|
|
|
|
fn calculate_possible(&mut self, solutions: &mut u16) -> bool {
|
|
fn calculate_possible(&mut self, solutions: &mut u16) -> bool {
|
|
- for idx in 0..MAX_SIZE {
|
|
|
|
|
|
+ for idx in 0..MAX_SIZE {
|
|
if self.board[idx as usize] == 0 {
|
|
if self.board[idx as usize] == 0 {
|
|
// Ok, there's a blank here
|
|
// Ok, there's a blank here
|
|
|
|
|
|
let (x, y) = xy(idx);
|
|
let (x, y) = xy(idx);
|
|
- println!("idx={} ({},{})", idx, x, y);
|
|
|
|
- self.display();
|
|
|
|
|
|
+ if DEBUG_OUTPUT {
|
|
|
|
+ println!("idx={} ({},{})", idx, x, y);
|
|
|
|
+ self.display();
|
|
|
|
+ }
|
|
|
|
|
|
'outer: for possible in 1..=9 {
|
|
'outer: for possible in 1..=9 {
|
|
let mut g = for_row(y);
|
|
let mut g = for_row(y);
|
|
@@ -372,7 +376,9 @@ impl Sudoku {
|
|
}
|
|
}
|
|
|
|
|
|
// Ok, it could go here!
|
|
// Ok, it could go here!
|
|
- println!("({},{})={}", x, y, possible);
|
|
|
|
|
|
+ if DEBUG_OUTPUT {
|
|
|
|
+ println!("({},{})={}", x, y, possible);
|
|
|
|
+ }
|
|
|
|
|
|
self.board[idx as usize] = possible;
|
|
self.board[idx as usize] = possible;
|
|
if self.puzzle_complete() {
|
|
if self.puzzle_complete() {
|
|
@@ -399,12 +405,12 @@ impl Sudoku {
|
|
let mut workset = Sudoku {
|
|
let mut workset = Sudoku {
|
|
board: self.board,
|
|
board: self.board,
|
|
possible: [Possible(0); MAX_SIZE as usize],
|
|
possible: [Possible(0); MAX_SIZE as usize],
|
|
- }; // self.possible };
|
|
|
|
|
|
+ };
|
|
|
|
|
|
let mut solutions: u16 = 0;
|
|
let mut solutions: u16 = 0;
|
|
workset.calculate_possible(&mut solutions);
|
|
workset.calculate_possible(&mut solutions);
|
|
|
|
|
|
- // find number of solutions.
|
|
|
|
|
|
+ // return number of solutions.
|
|
solutions
|
|
solutions
|
|
}
|
|
}
|
|
|
|
|
|
@@ -491,7 +497,9 @@ impl Sudoku {
|
|
let value = pick_one(self, &mut rng, i);
|
|
let value = pick_one(self, &mut rng, i);
|
|
if value.is_some() {
|
|
if value.is_some() {
|
|
let value = value.unwrap();
|
|
let value = value.unwrap();
|
|
- println!("Set({},{})={}", x, y, value);
|
|
|
|
|
|
+ if DEBUG_OUTPUT {
|
|
|
|
+ println!("Set({},{})={}", x, y, value);
|
|
|
|
+ }
|
|
self.set(x, y, value);
|
|
self.set(x, y, value);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
@@ -554,7 +562,7 @@ impl Sudoku {
|
|
}
|
|
}
|
|
};
|
|
};
|
|
|
|
|
|
- // Change to 0..SIZE ... Keep it simple.
|
|
|
|
|
|
+ // Change to 0..WIDTH ... Keep it simple.
|
|
for i in 0..WIDTH {
|
|
for i in 0..WIDTH {
|
|
let mut g = for_column(i);
|
|
let mut g = for_column(i);
|
|
// g.for_column(i, 1);
|
|
// g.for_column(i, 1);
|
|
@@ -598,7 +606,7 @@ impl Sudoku {
|
|
|
|
|
|
let mut pair_removed = false;
|
|
let mut pair_removed = false;
|
|
|
|
|
|
- // It isn't currently possible to tell if anything was removed...
|
|
|
|
|
|
+ // Check to see if anything was removed.
|
|
|
|
|
|
for remove in 0..WIDTH {
|
|
for remove in 0..WIDTH {
|
|
if (gidx == remove) || (fidx == remove) {
|
|
if (gidx == remove) || (fidx == remove) {
|
|
@@ -612,21 +620,11 @@ impl Sudoku {
|
|
pair_removed = true;
|
|
pair_removed = true;
|
|
}
|
|
}
|
|
|
|
|
|
- /* ).is_some() {
|
|
|
|
- found_something = true;
|
|
|
|
- pair_removed = true;
|
|
|
|
- };
|
|
|
|
- */
|
|
|
|
if self.possible[rpos as usize].get(values[1]) {
|
|
if self.possible[rpos as usize].get(values[1]) {
|
|
self.possible[rpos as usize].set(values[1], false);
|
|
self.possible[rpos as usize].set(values[1], false);
|
|
found_something = true;
|
|
found_something = true;
|
|
pair_removed = true;
|
|
pair_removed = true;
|
|
}
|
|
}
|
|
- /*.is_some() {
|
|
|
|
- found_something = true;
|
|
|
|
- pair_removed = true;
|
|
|
|
- };
|
|
|
|
- */
|
|
|
|
}
|
|
}
|
|
|
|
|
|
if pair_removed {
|
|
if pair_removed {
|
|
@@ -655,16 +653,27 @@ impl Sudoku {
|
|
if (remove == xy(gpos).1) || (remove == xy(fpos).1) {
|
|
if (remove == xy(gpos).1) || (remove == xy(fpos).1) {
|
|
continue;
|
|
continue;
|
|
}
|
|
}
|
|
- self.possible[pos(column, remove) as usize]
|
|
|
|
- .set(values[0], false);
|
|
|
|
|
|
+
|
|
|
|
+ if self.possible[pos(column, remove) as usize]
|
|
|
|
+ .get(values[0])
|
|
|
|
+ {
|
|
|
|
+ self.possible[pos(column, remove) as usize]
|
|
|
|
+ .set(values[0], false);
|
|
|
|
+ found_something = true;
|
|
|
|
+ }
|
|
/* v
|
|
/* v
|
|
.is_some()
|
|
.is_some()
|
|
{
|
|
{
|
|
found_something = true;
|
|
found_something = true;
|
|
};
|
|
};
|
|
*/
|
|
*/
|
|
- self.possible[pos(column, remove) as usize]
|
|
|
|
- .set(values[1], false);
|
|
|
|
|
|
+ if self.possible[pos(column, remove) as usize]
|
|
|
|
+ .get(values[1])
|
|
|
|
+ {
|
|
|
|
+ self.possible[pos(column, remove) as usize]
|
|
|
|
+ .set(values[1], false);
|
|
|
|
+ found_something = true;
|
|
|
|
+ }
|
|
/*
|
|
/*
|
|
.is_some()
|
|
.is_some()
|
|
{
|
|
{
|