show loops per second while running

This commit is contained in:
ddidderr 2022-07-30 08:44:44 +02:00
parent cda502d79c
commit f1565cb2bf
Signed by: ddidderr
GPG Key ID: 3841F1C27E6F0E14

View File

@ -1,5 +1,7 @@
use std::time::Instant; use std::time::Instant;
const MAX_BLOCK_SIZE: usize = 10;
struct SField { struct SField {
field: Vec<usize>, field: Vec<usize>,
fixed: Vec<usize>, fixed: Vec<usize>,
@ -13,46 +15,50 @@ struct SField {
impl SField { impl SField {
pub fn new(block_size: usize) -> SField { pub fn new(block_size: usize) -> SField {
if block_size > MAX_BLOCK_SIZE {
panic!("block size cannot be larger than {}", MAX_BLOCK_SIZE);
}
let size = block_size * block_size; let size = block_size * block_size;
// EMPTY FIELD // EMPTY FIELD
// let field = vec![0; size * size]; // let field = vec![0; size * size];
// BLOCK_SIZE 4 // 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 = vec![
0,0,0,0,0,0,0,0,9, 0,15,0,0,0,0,11,4,0,5,0,0,0,0,12,8,
0,0,0,0,8,9,0,2,0, 12,0,0,9,0,1,0,5,0,0,8,15,0,0,0,13,
0,0,0,0,2,0,4,0,0, 0,0,3,0,0,10,13,0,0,11,4,0,15,0,6,0,
0,0,4,0,6,0,0,0,8, 13,10,0,11,0,0,0,14,0,3,2,0,0,9,0,0,
0,0,0,5,0,0,0,0,0, 0,0,15,10,8,0,0,0,0,0,14,0,0,6,2,0,
0,6,5,0,0,2,0,7,4, 0,0,14,0,0,0,6,0,0,0,0,0,1,0,0,0,
0,3,0,0,0,5,0,4,0, 0,11,0,8,3,0,15,1,6,0,0,0,0,0,0,7,
0,0,1,8,0,0,0,0,0, 0,6,0,7,0,0,0,0,8,13,0,0,10,0,0,0,
0,0,8,2,0,0,0,6,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]
// 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]
// let field = vec![ // let field = vec![
@ -124,16 +130,21 @@ 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;
@ -334,21 +345,21 @@ impl SField {
} }
#[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,22 +373,22 @@ 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)
} }
@ -412,7 +423,7 @@ impl SField {
} }
fn run() -> Result<(), String> { fn run() -> Result<(), String> {
let mut field = SField::new(3); let mut field = SField::new(4);
field.build_possible_values_db(); field.build_possible_values_db();