Compare commits

..

No commits in common. "81f52b5e4327d27d21f49b6635624e7c69eef114" and "46c96a22baa32749112b9c2fa493240d711a1fde" have entirely different histories.

4 changed files with 58 additions and 64 deletions

2
Cargo.lock generated
View File

@ -4,4 +4,4 @@ version = 3
[[package]] [[package]]
name = "sudk" name = "sudk"
version = "0.1.1" version = "0.1.0"

View File

@ -1,14 +1,9 @@
[package] [package]
name = "sudk" name = "sudk"
version = "0.1.1" version = "0.1.0"
authors = ["ddidderr <ddidderr@paul.network>"] authors = ["ddidderr <ddidderr@paul.network>"]
edition = "2021" edition = "2021"
[lints.clippy]
pedantic = { level = "warn", priority = -1 }
todo = "warn"
unwrap_used = "warn"
[dependencies] [dependencies]
[profile.release] [profile.release]

View File

@ -1,3 +0,0 @@
group_imports = "StdExternalCrate"
imports_granularity = "Crate"
imports_layout = "HorizontalVertical"

View File

@ -4,26 +4,10 @@ const BLOCK_SIZE: usize = 3;
const SIZE: usize = BLOCK_SIZE * BLOCK_SIZE; const SIZE: usize = BLOCK_SIZE * BLOCK_SIZE;
const NUM_FIELDS: usize = SIZE * SIZE; const NUM_FIELDS: usize = SIZE * SIZE;
fn print_gray() {
print!("\x1b\x5b\x31\x3b\x33\x30\x6d");
}
fn print_green() {
print!("\x1b\x5b\x31\x3b\x33\x32\x6d");
}
fn print_neutral() {
print!("\x1b\x5b\x31\x3b\x30\x6d");
}
fn print_clear() {
print!("\x1b\x5b\x48\x1b\x5b\x32\x4a");
}
struct SField { struct SField {
field: Vec<u8>, field: Vec<u8>,
skip_forward: Vec<u8>, skipf: Vec<u8>,
skip_backward: Vec<u8>, skipb: Vec<u8>,
pos: usize, pos: usize,
possible_values: Vec<Vec<u8>>, possible_values: Vec<Vec<u8>>,
} }
@ -43,22 +27,6 @@ impl SField {
0,0,8,2,0,0,0,6,0, 0,0,8,2,0,0,0,6,0,
]; ];
let mut sudoku = SField {
field,
skip_forward: vec![0; NUM_FIELDS + 1],
skip_backward: vec![0; NUM_FIELDS + 1],
pos: 0,
possible_values: vec![vec![]; NUM_FIELDS],
};
sudoku.build_possible_values_db();
sudoku.build_skip_dbs();
sudoku
}
fn build_skip_dbs(&mut self) {
fn find_fixed_streak_forward(mut idx: usize, field: &[u8]) -> u8 { fn find_fixed_streak_forward(mut idx: usize, field: &[u8]) -> u8 {
let mut fixed_count = 1; let mut fixed_count = 1;
idx += 1; idx += 1;
@ -83,21 +51,29 @@ impl SField {
fixed_count fixed_count
} }
let fwd = &mut self.skip_forward; let mut skipf = vec![0; NUM_FIELDS + 1];
for (idx, nr) in self.field.iter().enumerate() { for (idx, nr) in field.iter().enumerate() {
match nr { match nr {
0 => fwd[idx] = *nr, 0 => skipf[idx] = *nr,
_ => fwd[idx] = find_fixed_streak_forward(idx, &self.field), _ => skipf[idx] = find_fixed_streak_forward(idx, &field),
} }
} }
let bwd = &mut self.skip_backward; let mut skipb = vec![0; NUM_FIELDS + 1];
for (idx, nr) in self.field.iter().enumerate().rev() { for (idx, nr) in field.iter().enumerate().rev() {
match nr { match nr {
0 => bwd[idx + 1] = *nr, 0 => skipb[idx + 1] = *nr,
_ => bwd[idx + 1] = find_fixed_streak_backward(idx, &self.field), _ => skipb[idx + 1] = find_fixed_streak_backward(idx, &field),
} }
} }
SField {
field,
skipf,
skipb,
pos: 0,
possible_values: vec![vec![]; SIZE * SIZE],
}
} }
fn build_possible_values_db(&mut self) { fn build_possible_values_db(&mut self) {
@ -108,7 +84,7 @@ impl SField {
self.pos = idx; self.pos = idx;
// try all values between 1 and =self.size and remember the good ones // try all values between 1 and =self.size and remember the good ones
let mut good_ones = Vec::with_capacity(SIZE); let mut good_ones = Vec::with_capacity(SIZE);
for nr in 1..=(u8::try_from(SIZE).expect("SIZE is too big")) { for nr in 1..=(SIZE as u8) {
if self.ok(nr) { if self.ok(nr) {
good_ones.push(nr); good_ones.push(nr);
} }
@ -125,9 +101,9 @@ impl SField {
if !self.put_valid_nr() { if !self.put_valid_nr() {
self.clear_current_field(); self.clear_current_field();
if !self.prev() { if !self.prev() {
print_clear(); self.print_clear();
self.print(); self.print();
println!("Number of solutions: {num_solutions}"); println!("Number of solutions: {}", num_solutions);
break; break;
} }
continue; continue;
@ -136,9 +112,9 @@ impl SField {
if !self.next() { if !self.next() {
num_solutions += 1; num_solutions += 1;
if num_solutions % 10_000 == 0 { if num_solutions % 10_000 == 0 {
print_clear(); self.print_clear();
self.print(); self.print();
println!("Number of solutions: {num_solutions}"); println!("Number of solutions: {}", num_solutions);
} }
self.clear_current_field(); self.clear_current_field();
@ -149,6 +125,22 @@ impl SField {
num_solutions > 0 num_solutions > 0
} }
fn print_gray(&self) {
print!("\x1b\x5b\x31\x3b\x33\x30\x6d");
}
fn print_green(&self) {
print!("\x1b\x5b\x31\x3b\x33\x32\x6d");
}
fn print_neutral(&self) {
print!("\x1b\x5b\x31\x3b\x30\x6d");
}
fn print_clear(&self) {
print!("\x1b\x5b\x48\x1b\x5b\x32\x4a");
}
fn print(&self) { fn print(&self) {
for i in 0..NUM_FIELDS { for i in 0..NUM_FIELDS {
if i != 0 && i % SIZE == 0 { if i != 0 && i % SIZE == 0 {
@ -156,15 +148,15 @@ impl SField {
} }
if i == self.pos { if i == self.pos {
print_green(); self.print_green();
} else if self.get_field_at_pos(i) == 0 { } else if self.get_field_at_pos(i) == 0 {
print_gray(); self.print_gray();
} }
print!("{:2} ", self.get_field_at_pos(i)); print!("{:2} ", self.get_field_at_pos(i));
if i == self.pos || self.get_field_at_pos(i) == 0 { if i == self.pos || self.get_field_at_pos(i) == 0 {
print_neutral(); self.print_neutral();
} }
} }
println!(); println!();
@ -182,7 +174,7 @@ impl SField {
// so self.pos can safely be used to index here // so self.pos can safely be used to index here
let possible_vals = &self.possible_values[self.pos]; let possible_vals = &self.possible_values[self.pos];
for nr in possible_vals { for nr in possible_vals.iter() {
if *nr <= current_nr { if *nr <= current_nr {
continue; continue;
} }
@ -254,7 +246,7 @@ impl SField {
} }
fn next(&mut self) -> bool { fn next(&mut self) -> bool {
let new_pos = self.pos + 1 + self.skip_forward[self.pos + 1] as usize; let new_pos = self.pos + 1 + self.skipf[self.pos + 1] as usize;
if new_pos >= NUM_FIELDS { if new_pos >= NUM_FIELDS {
return false; return false;
@ -265,7 +257,7 @@ impl SField {
} }
fn prev(&mut self) -> bool { fn prev(&mut self) -> bool {
let new_pos = self.pos - 1 - self.skip_backward[self.pos] as usize; let new_pos = self.pos - 1 - self.skipb[self.pos] as usize;
if new_pos >= NUM_FIELDS { if new_pos >= NUM_FIELDS {
return false; return false;
@ -284,7 +276,17 @@ impl SField {
} }
} }
fn main() { fn run() -> Result<(), String> {
let mut field = SField::new(); let mut field = SField::new();
field.build_possible_values_db();
field.solve(); field.solve();
Ok(())
}
fn main() {
if let Err(e) = run() {
println!("{}", e)
}
} }