From f1565cb2bf7833c10e2c4fce46c0f3f6303cef41 Mon Sep 17 00:00:00 2001 From: ddidderr Date: Sat, 30 Jul 2022 08:44:44 +0200 Subject: [PATCH] show loops per second while running --- src/main.rs | 103 +++++++++++++++++++++++++++++----------------------- 1 file changed, 57 insertions(+), 46 deletions(-) diff --git a/src/main.rs b/src/main.rs index 1d728f1..da300ba 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,5 +1,7 @@ use std::time::Instant; +const MAX_BLOCK_SIZE: usize = 10; + struct SField { field: Vec, fixed: Vec, @@ -13,46 +15,50 @@ struct SField { impl 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; // EMPTY FIELD // 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] 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, + 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] + // 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 // #[rustfmt::skip] // let field = vec![ @@ -124,16 +130,21 @@ impl SField { let mut found_solution = false; 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 { //std::thread::sleep_ms(30); //self.print_clear(); //self.print(); - //loop_count += 1; - //if loop_count % 1000000 == 0 { - //self.print_clear(); - //self.print(); - //} + loop_count += 1; + if now.elapsed().as_millis() >= 1000 { + now = Instant::now(); + self.print_clear(); + self.print(); + println!("{} loops/s", loop_count - last_loop_count); + last_loop_count = loop_count; + } if self.is_end() { found_solution = true; num_solutions += 1; @@ -334,21 +345,21 @@ impl SField { } #[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() { *row_elem = self.get_field_at_pos((self.pos / self.size) * self.size + idx); } } #[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() { *col_elem = self.get_field_at_pos(idx * self.size + self.pos % self.size); } } #[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_col = self.pos % self.size / self.block_size * self.block_size; @@ -362,22 +373,22 @@ impl SField { #[inline(always)] fn block_ok(&self, nr: usize) -> bool { - let mut block = [0; 9]; - self.get_block(&mut block); + let mut block = [0; MAX_BLOCK_SIZE * MAX_BLOCK_SIZE]; + self.get_block(&mut block[0..self.size]); !block.contains(&nr) } #[inline(always)] fn row_ok(&self, nr: usize) -> bool { - let mut row = [0; 9]; - self.get_row(&mut row); + let mut row = [0; MAX_BLOCK_SIZE * MAX_BLOCK_SIZE]; + self.get_row(&mut row[0..self.size]); !row.contains(&nr) } #[inline(always)] fn col_ok(&self, nr: usize) -> bool { - let mut col = [0; 9]; - self.get_col(&mut col); + let mut col = [0; MAX_BLOCK_SIZE * MAX_BLOCK_SIZE]; + self.get_col(&mut col[0..self.size]); !col.contains(&nr) } @@ -412,7 +423,7 @@ impl SField { } fn run() -> Result<(), String> { - let mut field = SField::new(3); + let mut field = SField::new(4); field.build_possible_values_db();