Compare commits

...

2 Commits

View File

@ -1,47 +1,97 @@
use std::time::Instant; use std::time::Instant;
#[derive(Copy, Clone, Debug)]
struct Bitset {
set: u16,
}
impl Bitset {
pub fn new() -> Self {
Self { set: 0 }
}
#[inline(always)]
fn get(&self, nr: usize) -> bool {
self.set & (1 << nr) != 0
}
#[inline(always)]
fn set(&mut self, nr: usize) {
self.set |= 1 << nr;
}
#[inline(always)]
fn unset(&mut self, nr: usize) {
self.set &= !(1 << nr);
}
}
const BLOCK_SIZE: usize = 4;
//const MAX_BLOCK_SIZE: usize = 10;
struct SField { struct SField {
field: Vec<usize>, field: Vec<usize>,
fixed: Vec<usize>, fixed: Vec<usize>,
size: usize, size: usize,
block_size: usize,
pos: usize, pos: usize,
pos_last_placed: usize, pos_last_placed: usize,
num_fields: usize, num_fields: usize,
possible_values: Vec<Vec<usize>>, possible_values: Vec<Vec<usize>>,
// For each row/col/block we create a bitset that represents the numbers
// that are already in that row, col or block.
// This way we can check very fast if a nr fits into a specific field or not.
rows: [Bitset; BLOCK_SIZE * BLOCK_SIZE],
cols: [Bitset; BLOCK_SIZE * BLOCK_SIZE],
blocks: [Bitset; BLOCK_SIZE * BLOCK_SIZE],
}
#[inline(always)]
fn xy_to_pos(x: usize, y: usize, size: usize) -> usize {
y * size + x
}
#[inline(always)]
fn pos_to_xy_blocknr(pos: usize, size: usize) -> (usize, usize, usize) {
let x = pos % size;
let y = pos / size;
let block_x = x / BLOCK_SIZE;
let block_y = y / BLOCK_SIZE;
let block_nr = block_y * BLOCK_SIZE + block_x;
(x, y, block_nr)
} }
impl SField { impl SField {
pub fn new(block_size: usize) -> SField { pub fn new() -> SField {
let size = block_size * block_size; const SIZE: usize = BLOCK_SIZE * BLOCK_SIZE;
// EMPTY FIELD // EMPTY FIELD
// let field = vec![0; size * size]; // let field = vec![0; size * size];
// BLOCK_SIZE 4
// #[rustfmt::skip]
// let field = vec![
// 0,15,0,0,0,0,11,4,0,5,0,0,0,0,12,8,
// 12,0,0,9,0,1,0,5,0,0,8,15,0,0,0,13,
// 0,0,3,0,0,10,13,0,0,11,4,0,15,0,6,0,
// 13,10,0,11,0,0,0,14,0,3,2,0,0,9,0,0,
// 0,0,15,10,8,0,0,0,0,0,14,0,0,6,2,0,
// 0,0,14,0,0,0,6,0,0,0,0,0,1,0,0,0,
// 0,11,0,8,3,0,15,1,6,0,0,0,0,0,0,7,
// 0,6,0,7,0,0,0,0,8,13,0,0,10,0,0,0,
// 0,14,0,2,0,0,0,0,3,0,5,0,11,15,9,0,
// 0,0,0,12,11,0,2,0,0,0,0,0,0,0,0,0,
// 0,8,0,0,6,14,1,0,13,15,0,0,0,0,0,10,
// 10,0,0,3,9,0,7,0,0,1,0,0,13,12,8,0,
// 7,0,0,0,0,0,14,0,0,0,0,0,0,0,0,9,
// 0,3,0,14,0,0,9,6,0,0,0,0,0,4,0,0,
// 0,4,6,0,0,7,0,0,0,0,0,0,0,0,0,0,
// 5,2,0,0,0,4,10,15,1,0,3,0,0,0,7,0,
// ];
// MANY SOLUTIONS
#[rustfmt::skip] #[rustfmt::skip]
let field = vec![ let field = match BLOCK_SIZE {
4 => vec![
0usize,15,0,0,0,0,11,4,0,5,0,0,0,0,12,8,
12,0,0,9,0,1,0,5,0,0,8,15,0,0,0,13,
0,0,3,0,0,10,13,0,0,11,4,0,15,0,6,0,
13,10,0,11,0,0,0,14,0,3,2,0,0,9,0,0,
0,0,15,10,8,0,0,0,0,0,14,0,0,6,2,0,
0,0,14,0,0,0,6,0,0,0,0,0,1,0,0,0,
0,11,0,8,3,0,15,1,6,0,0,0,0,0,0,7,
0,6,0,7,0,0,0,0,8,13,0,0,10,0,0,0,
0,14,0,2,0,0,0,0,3,0,5,0,11,15,9,0,
0,0,0,12,11,0,2,0,0,0,0,0,0,0,0,0,
0,8,0,0,6,14,1,0,13,15,0,0,0,0,0,10,
10,0,0,3,9,0,7,0,0,1,0,0,13,12,8,0,
7,0,0,0,0,0,14,0,0,0,0,0,0,0,0,9,
0,3,0,14,0,0,9,6,0,0,0,0,0,4,0,0,
0,4,6,0,0,7,0,0,0,0,0,0,0,0,0,0,
5,2,0,0,0,4,10,15,1,0,3,0,0,0,7,0,
],
3 => vec![
0,0,0,0,0,0,0,0,9, 0,0,0,0,0,0,0,0,9,
0,0,0,0,8,9,0,2,0, 0,0,0,0,8,9,0,2,0,
0,0,0,0,2,0,4,0,0, 0,0,0,0,2,0,4,0,0,
@ -51,7 +101,23 @@ impl SField {
0,3,0,0,0,5,0,4,0, 0,3,0,0,0,5,0,4,0,
0,0,1,8,0,0,0,0,0, 0,0,1,8,0,0,0,0,0,
0,0,8,2,0,0,0,6,0, 0,0,8,2,0,0,0,6,0,
]; ],
_ => panic!("Unsupported block size. Only 3 and 4 is ok for now.")
};
// MANY SOLUTIONS
// #[rustfmt::skip]
// let field = vec![
// 0,0,0,0,0,0,0,0,9,
// 0,0,0,0,8,9,0,2,0,
// 0,0,0,0,2,0,4,0,0,
// 0,0,4,0,6,0,0,0,8,
// 0,0,0,5,0,0,0,0,0,
// 0,6,5,0,0,2,0,7,4,
// 0,3,0,0,0,5,0,4,0,
// 0,0,1,8,0,0,0,0,0,
// 0,0,8,2,0,0,0,6,0,
// ];
// HARD // HARD
// #[rustfmt::skip] // #[rustfmt::skip]
@ -81,7 +147,7 @@ impl SField {
// 7,3,6,0,2,0,0,0,0, // 7,3,6,0,2,0,0,0,0,
// ]; // ];
let mut fixed = Vec::with_capacity(size * size); let mut fixed = Vec::with_capacity(SIZE * SIZE);
field.iter().for_each(|e| { field.iter().for_each(|e| {
if *e == 0 { if *e == 0 {
fixed.push(0); fixed.push(0);
@ -90,15 +156,82 @@ impl SField {
} }
}); });
// create bitsets
// rows
let mut bit_rows = [Bitset::new(); SIZE];
for row_nr in 0..SIZE {
let mut row = [0; SIZE];
for count in 0..SIZE {
row[count] = field[xy_to_pos(count, row_nr, SIZE)];
}
row.into_iter()
.filter(|val| *val != 0)
.for_each(|val| bit_rows[row_nr].set(val - 1));
}
// cols
let mut bit_cols = [Bitset::new(); SIZE];
for col_nr in 0..SIZE {
let mut col = [0; SIZE];
for count in 0..SIZE {
col[count] = field[xy_to_pos(col_nr, count, SIZE)];
}
col.into_iter()
.filter(|val| *val != 0)
.for_each(|val| bit_cols[col_nr].set(val - 1));
}
// blocks
let mut bit_blocks = [Bitset::new(); SIZE];
for block_nr in 0..SIZE {
let block_start_x = block_nr * BLOCK_SIZE % SIZE;
let block_start_y = block_nr * BLOCK_SIZE / SIZE * BLOCK_SIZE;
let mut block = [0; SIZE];
for count in 0..SIZE {
let block_offset_x = count % BLOCK_SIZE;
let block_offset_y = count / BLOCK_SIZE;
let field_x = block_start_x + block_offset_x;
let field_y = block_start_y + block_offset_y;
//dbg!(
//block_nr,
//block_start_x,
//block_start_y,
//block_offset_x,
//block_offset_y,
//field_x,
//field_y,
//);
block[count] = field[xy_to_pos(field_x, field_y, SIZE)];
}
block
.into_iter()
.filter(|x| *x != 0)
.for_each(|x| bit_blocks[block_nr].set(x - 1));
}
//dbg!(bit_rows);
//dbg!(bit_cols);
//dbg!(bit_blocks);
SField { SField {
field, field,
fixed, fixed,
size, size: SIZE,
block_size,
pos: 0, pos: 0,
pos_last_placed: 0, pos_last_placed: 0,
num_fields: size * size, num_fields: SIZE * SIZE,
possible_values: vec![vec![]; size * size], possible_values: vec![vec![]; SIZE * SIZE],
rows: bit_rows,
cols: bit_cols,
blocks: bit_blocks,
} }
} }
@ -124,24 +257,29 @@ impl SField {
let mut found_solution = false; let mut found_solution = false;
let mut num_solutions = 0; let mut num_solutions = 0;
//let mut loop_count = 0; let mut loop_count = 0;
let mut last_loop_count = 0;
let mut now = Instant::now();
loop { loop {
//std::thread::sleep_ms(30); //std::thread::sleep_ms(30);
//self.print_clear(); //self.print_clear();
//self.print(); //self.print();
//loop_count += 1; loop_count += 1;
//if loop_count % 1000000 == 0 { if now.elapsed().as_millis() >= 1000 {
//self.print_clear(); now = Instant::now();
//self.print(); self.print_clear();
//} self.print();
println!("{} loops/s", loop_count - last_loop_count);
last_loop_count = loop_count;
}
if self.is_end() { if self.is_end() {
found_solution = true; found_solution = true;
num_solutions += 1; num_solutions += 1;
if num_solutions % 100 == 0 { //if num_solutions % 100 == 0 {
self.print_clear(); //self.print_clear();
println!("Solutions: {}", num_solutions); //println!("Solutions: {}", num_solutions);
self.print(); //self.print();
} //}
if self.is_fixed() { if self.is_fixed() {
continue; continue;
@ -285,8 +423,9 @@ impl SField {
continue; continue;
} }
if self.ok(*nr) { let nr = *nr;
self.set(*nr); if self.ok(nr) {
self.set(nr);
return true; return true;
} }
} }
@ -300,14 +439,11 @@ impl SField {
#[inline(always)] #[inline(always)]
fn ok(&self, nr: usize) -> bool { fn ok(&self, nr: usize) -> bool {
self.block_ok(nr) && self.row_ok(nr) && self.col_ok(nr) //self.block_ok(nr) && self.row_ok(nr) && self.col_ok(nr)
}
#[inline(always)] let (x, y, block_nr) = pos_to_xy_blocknr(self.pos, self.size);
fn _pos_to_xy(&self) -> (usize, usize) {
let y = self.pos / self.size; !self.rows[y].get(nr - 1) && !self.cols[x].get(nr - 1) && !self.blocks[block_nr].get(nr - 1)
let x = self.pos % self.size;
(x + 1, y + 1)
} }
#[inline(always)] #[inline(always)]
@ -329,26 +465,48 @@ impl SField {
#[inline(always)] #[inline(always)]
fn set(&mut self, nr: usize) { fn set(&mut self, nr: usize) {
self.field[self.pos] = nr; let old = unsafe { self.field.get_unchecked_mut(self.pos) };
// if another number > 0 was in that cell, we need to remove that number from our bitsets
if *old > 0 {
let (x, y, block_nr) = pos_to_xy_blocknr(self.pos, self.size);
self.rows[y].unset(*old - 1);
self.cols[x].unset(*old - 1);
self.blocks[block_nr].unset(*old - 1);
}
// add new nr to our bitsets if it is a meaningful (> 0) number
if nr > 0 {
let (x, y, block_nr) = pos_to_xy_blocknr(self.pos, self.size);
self.rows[y].set(nr - 1);
self.cols[x].set(nr - 1);
self.blocks[block_nr].set(nr - 1);
}
// write the new number into the field
*old = nr;
// remember the last placed position for debugging purposes
self.pos_last_placed = self.pos; self.pos_last_placed = self.pos;
} }
/*
#[inline(always)] #[inline(always)]
fn get_row(&self, row: &mut [usize; 9]) { fn get_row(&self, row: &mut [usize]) {
for (idx, row_elem) in row.iter_mut().enumerate() { for (idx, row_elem) in row.iter_mut().enumerate() {
*row_elem = self.get_field_at_pos((self.pos / self.size) * self.size + idx); *row_elem = self.get_field_at_pos((self.pos / self.size) * self.size + idx);
} }
} }
#[inline(always)] #[inline(always)]
fn get_col(&self, col: &mut [usize; 9]) { fn get_col(&self, col: &mut [usize]) {
for (idx, col_elem) in col.iter_mut().enumerate() { for (idx, col_elem) in col.iter_mut().enumerate() {
*col_elem = self.get_field_at_pos(idx * self.size + self.pos % self.size); *col_elem = self.get_field_at_pos(idx * self.size + self.pos % self.size);
} }
} }
#[inline(always)] #[inline(always)]
fn get_block(&self, block: &mut [usize; 9]) { fn get_block(&self, block: &mut [usize]) {
let block_start_row = self.pos / self.size / self.block_size * self.block_size; let block_start_row = self.pos / self.size / self.block_size * self.block_size;
let block_start_col = self.pos % self.size / self.block_size * self.block_size; let block_start_col = self.pos % self.size / self.block_size * self.block_size;
@ -362,24 +520,25 @@ impl SField {
#[inline(always)] #[inline(always)]
fn block_ok(&self, nr: usize) -> bool { fn block_ok(&self, nr: usize) -> bool {
let mut block = [0; 9]; let mut block = [0; MAX_BLOCK_SIZE * MAX_BLOCK_SIZE];
self.get_block(&mut block); self.get_block(&mut block[0..self.size]);
!block.contains(&nr) !block.contains(&nr)
} }
#[inline(always)] #[inline(always)]
fn row_ok(&self, nr: usize) -> bool { fn row_ok(&self, nr: usize) -> bool {
let mut row = [0; 9]; let mut row = [0; MAX_BLOCK_SIZE * MAX_BLOCK_SIZE];
self.get_row(&mut row); self.get_row(&mut row[0..self.size]);
!row.contains(&nr) !row.contains(&nr)
} }
#[inline(always)] #[inline(always)]
fn col_ok(&self, nr: usize) -> bool { fn col_ok(&self, nr: usize) -> bool {
let mut col = [0; 9]; let mut col = [0; MAX_BLOCK_SIZE * MAX_BLOCK_SIZE];
self.get_col(&mut col); self.get_col(&mut col[0..self.size]);
!col.contains(&nr) !col.contains(&nr)
} }
*/
#[inline(always)] #[inline(always)]
fn next(&mut self) -> bool { fn next(&mut self) -> bool {
@ -387,6 +546,7 @@ impl SField {
return false; return false;
} }
//println!("next {} -> {}", self.pos, self.pos + 1);
self.pos += 1; self.pos += 1;
true true
} }
@ -412,7 +572,7 @@ impl SField {
} }
fn run() -> Result<(), String> { fn run() -> Result<(), String> {
let mut field = SField::new(3); let mut field = SField::new();
field.build_possible_values_db(); field.build_possible_values_db();