Compare commits
	
		
			2 Commits
		
	
	
		
			81f52b5e43
			...
			bitsets
		
	
	| Author | SHA1 | Date | |
|---|---|---|---|
| 65984d224c | |||
| f1565cb2bf | 
							
								
								
									
										302
									
								
								src/main.rs
									
									
									
									
									
								
							
							
						
						
									
										302
									
								
								src/main.rs
									
									
									
									
									
								
							| @@ -1,57 +1,123 @@ | ||||
| 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 { | ||||
|     field: Vec<usize>, | ||||
|     fixed: Vec<usize>, | ||||
|     size: usize, | ||||
|     block_size: usize, | ||||
|     pos: usize, | ||||
|     pos_last_placed: usize, | ||||
|     num_fields: 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 { | ||||
|     pub fn new(block_size: usize) -> SField { | ||||
|         let size = block_size * block_size; | ||||
|     pub fn new() -> SField { | ||||
|         const SIZE: usize = 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, | ||||
|         // ]; | ||||
|         #[rustfmt::skip] | ||||
|         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,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, | ||||
|             ], | ||||
|             _ => 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, | ||||
|         ]; | ||||
|         // #[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] | ||||
| @@ -81,7 +147,7 @@ impl SField { | ||||
|         //     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| { | ||||
|             if *e == 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 { | ||||
|             field, | ||||
|             fixed, | ||||
|             size, | ||||
|             block_size, | ||||
|             size: SIZE, | ||||
|             pos: 0, | ||||
|             pos_last_placed: 0, | ||||
|             num_fields: size * size, | ||||
|             possible_values: vec![vec![]; size * size], | ||||
|             num_fields: 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 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; | ||||
|                 if num_solutions % 100 == 0 { | ||||
|                     self.print_clear(); | ||||
|                     println!("Solutions: {}", num_solutions); | ||||
|                     self.print(); | ||||
|                 } | ||||
|                 //if num_solutions % 100 == 0 { | ||||
|                 //self.print_clear(); | ||||
|                 //println!("Solutions: {}", num_solutions); | ||||
|                 //self.print(); | ||||
|                 //} | ||||
|  | ||||
|                 if self.is_fixed() { | ||||
|                     continue; | ||||
| @@ -285,8 +423,9 @@ impl SField { | ||||
|                 continue; | ||||
|             } | ||||
|  | ||||
|             if self.ok(*nr) { | ||||
|                 self.set(*nr); | ||||
|             let nr = *nr; | ||||
|             if self.ok(nr) { | ||||
|                 self.set(nr); | ||||
|                 return true; | ||||
|             } | ||||
|         } | ||||
| @@ -300,14 +439,11 @@ impl SField { | ||||
|  | ||||
|     #[inline(always)] | ||||
|     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)] | ||||
|     fn _pos_to_xy(&self) -> (usize, usize) { | ||||
|         let y = self.pos / self.size; | ||||
|         let x = self.pos % self.size; | ||||
|         (x + 1, y + 1) | ||||
|         let (x, y, block_nr) = pos_to_xy_blocknr(self.pos, self.size); | ||||
|  | ||||
|         !self.rows[y].get(nr - 1) && !self.cols[x].get(nr - 1) && !self.blocks[block_nr].get(nr - 1) | ||||
|     } | ||||
|  | ||||
|     #[inline(always)] | ||||
| @@ -329,26 +465,48 @@ impl SField { | ||||
|  | ||||
|     #[inline(always)] | ||||
|     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; | ||||
|     } | ||||
|  | ||||
|     /* | ||||
|     #[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,24 +520,25 @@ 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) | ||||
|     } | ||||
|     */ | ||||
|  | ||||
|     #[inline(always)] | ||||
|     fn next(&mut self) -> bool { | ||||
| @@ -387,6 +546,7 @@ impl SField { | ||||
|             return false; | ||||
|         } | ||||
|  | ||||
|         //println!("next {} -> {}", self.pos, self.pos + 1); | ||||
|         self.pos += 1; | ||||
|         true | ||||
|     } | ||||
| @@ -412,7 +572,7 @@ impl SField { | ||||
| } | ||||
|  | ||||
| fn run() -> Result<(), String> { | ||||
|     let mut field = SField::new(3); | ||||
|     let mut field = SField::new(); | ||||
|  | ||||
|     field.build_possible_values_db(); | ||||
|  | ||||
|   | ||||
		Reference in New Issue
	
	Block a user