diff --git a/src/main.rs b/src/main.rs index e83f928..80100f5 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,5 +1,10 @@ +use std::time::Instant; + struct SField { field: Vec, + rows: Vec>, + cols: Vec>, + blocks: Vec>, fixed: Vec, size: usize, block_size: usize, @@ -88,8 +93,15 @@ impl SField { } }); + let rows = SField::rows_from_plain_field(&field); + let cols = SField::cols_from_plain_field(&field); + let blocks = SField::blocks_from_plain_field(&field); + SField { field, + rows, + cols, + blocks, fixed, size, block_size, @@ -100,6 +112,51 @@ impl SField { } } + fn rows_from_plain_field(field: &[usize]) -> Vec> { + let mut rows = vec![vec![0; 9]; 9]; + for row in 0..9 { + for col in 0..9 { + rows[row][col] = field[row * 9 + col]; + } + } + rows + } + + fn cols_from_plain_field(field: &[usize]) -> Vec> { + let mut cols = vec![vec![0; 9]; 9]; + for row in 0..9 { + for col in 0..9 { + cols[col][row] = field[row * 9 + col]; + } + } + cols + } + + fn blocks_from_plain_field(field: &[usize]) -> Vec> { + fn get_block_with_pos(field: &[usize], pos: usize) -> Vec { + let block_start_row = pos / 9 / 3 * 3; + let block_start_col = pos % 9 / 3 * 3; + + let mut block = vec![]; + + for r in 0..3 { + for c in 0..3 { + block.push(field[9 * (block_start_row + r) + block_start_col + c]) + } + } + + block + } + + let mut blocks = vec![]; + let block_start_positions = [0, 3, 6, 27, 30, 33, 54, 57, 60]; + for pos in block_start_positions { + blocks.push(get_block_with_pos(field, pos)); + } + + blocks + } + fn build_possible_values_db(&mut self) { for idx in 0..self.num_fields { if self.fixed[idx] == 1 { @@ -325,58 +382,103 @@ impl SField { unsafe { *self.field.get_unchecked(pos) } } + #[inline(always)] + fn get_x_y_block_blockoffset(&self) -> (usize, usize, usize, usize) { + // TODO: + // this only works for 3x3 sudoku + #[rustfmt::skip] + let block_nrs = [ + 0, 1, 2, 9,10,11,18,19,20, + 3, 4, 5,12,13,14,21,22,23, + 6, 7, 8,15,16,17,24,25,26, + 27,28,29,36,37,38,45,46,47, + 30,31,32,39,40,41,48,49,50, + 33,34,35,42,43,44,51,52,53, + 54,55,56,63,64,65,72,73,74, + 57,58,59,66,67,68,75,76,77, + 60,61,62,69,70,71,78,79,80, + ]; + + let y = self.pos / self.size; + let x = self.pos % self.size; + + let block_field = unsafe { block_nrs.get_unchecked(self.pos) }; + + let block_nr = block_field / self.size; + let block_offset = block_field - (block_nr * self.size); + + (x, y, block_nr, block_offset) + } + #[inline(always)] fn set(&mut self, nr: usize) { + //assert_eq!(self.field, self.flatten_rows()); + //assert_eq!(self.field, self.flatten_cols()); + //assert_eq!(self.field, self.flatten_blocks()); + self.field[self.pos] = nr; self.pos_last_placed = self.pos; - } - #[inline(always)] - fn get_row(&self, row: &mut [usize; 9]) { - for (idx, row_elem) in row.iter_mut().enumerate() { - *row_elem = self.get_field_at_pos((self.pos / self.size) * self.size + idx); - } - } + // update rows, cols and blocks + // this is more expensive than only updating field but should be outweight + // by the fact that row_ok(), col_ok() and block_ok() just need to do simple + // lookups now - #[inline(always)] - fn get_col(&self, col: &mut [usize; 9]) { - for (idx, col_elem) in col.iter_mut().enumerate() { - *col_elem = self.get_field_at_pos(idx * self.size + self.pos % self.size); - } - } + let (x, y, block_nr, block_offset) = self.get_x_y_block_blockoffset(); - #[inline(always)] - fn get_block(&self, block: &mut [usize; 9]) { - 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; + //println!("[{},{}] setting {}", y, x, nr); - for r in 0..self.block_size { - for c in 0..self.block_size { - block[r * self.block_size + c] = - self.get_field_at_pos(self.size * (block_start_row + r) + block_start_col + c); - } - } + let row = unsafe { self.rows.get_unchecked_mut(y) }; + let value = unsafe { row.get_unchecked_mut(x) }; + *value = nr; + + let col = unsafe { self.cols.get_unchecked_mut(x) }; + let value = unsafe { col.get_unchecked_mut(y) }; + *value = nr; + + let block = unsafe { self.blocks.get_unchecked_mut(block_nr) }; + let value = unsafe { block.get_unchecked_mut(block_offset) }; + *value = nr; + + //assert_eq!(self.field, self.flatten_rows()); + //assert_eq!(self.field, self.flatten_cols()); + //assert_eq!(self.field, self.flatten_blocks()); } #[inline(always)] fn block_ok(&self, nr: usize) -> bool { - let mut block = [0; 9]; - self.get_block(&mut block); - !block.contains(&nr) + // TODO + // works only on 3x3 sudoku + #[rustfmt::skip] + let block_nrs = [ + 0,0,0,1,1,1,2,2,2, + 0,0,0,1,1,1,2,2,2, + 0,0,0,1,1,1,2,2,2, + 3,3,3,4,4,4,5,5,5, + 3,3,3,4,4,4,5,5,5, + 3,3,3,4,4,4,5,5,5, + 6,6,6,7,7,7,8,8,8, + 6,6,6,7,7,7,8,8,8, + 6,6,6,7,7,7,8,8,8, + ]; + + let block_nr = unsafe { block_nrs.get_unchecked(self.pos) }; + //println!("Block {}: {:?}", block_nr, self.blocks[*block_nr]); + !unsafe { self.blocks.get_unchecked(*block_nr) }.contains(&nr) } #[inline(always)] fn row_ok(&self, nr: usize) -> bool { - let mut row = [0; 9]; - self.get_row(&mut row); - !row.contains(&nr) + let row_nr = self.pos / self.size; + //println!("{:?}", self.rows[row_nr]); + !unsafe { self.rows.get_unchecked(row_nr) }.contains(&nr) } #[inline(always)] fn col_ok(&self, nr: usize) -> bool { - let mut col = [0; 9]; - self.get_col(&mut col); - !col.contains(&nr) + let col_nr = self.pos % self.size; + //println!("{:?}", self.cols[col_nr]); + !unsafe { self.cols.get_unchecked(col_nr) }.contains(&nr) } #[inline(always)] @@ -401,10 +503,53 @@ impl SField { #[inline(always)] fn solve(&mut self) { + let now = Instant::now(); if !self.solve_backtracking() { println!("there is no solution."); } + println!("took {:.3} seconds", now.elapsed().as_secs_f64()); } + + //fn flatten_blocks(&self) -> Vec { + //#[rustfmt::skip] + //let block_nrs = [ + //0, 1, 2, 9,10,11,18,19,20, + //3, 4, 5,12,13,14,21,22,23, + //6, 7, 8,15,16,17,24,25,26, + //27,28,29,36,37,38,45,46,47, + //30,31,32,39,40,41,48,49,50, + //33,34,35,42,43,44,51,52,53, + //54,55,56,63,64,65,72,73,74, + //57,58,59,66,67,68,75,76,77, + //60,61,62,69,70,71,78,79,80, + //]; + + //let mut flattened = Vec::with_capacity(self.num_fields); + //for (idx, nr) in block_nrs.iter().enumerate() { + //let block_nr = nr / self.size; + //let block_offset = nr - (block_nr * self.size); + //flattened.push(self.blocks[block_nr][block_offset]); + //} + //flattened + //} + + //fn flatten_rows(&self) -> Vec { + //let mut flattened = Vec::with_capacity(self.num_fields); + //for row in self.rows.iter() { + //for val in row { + //flattened.push(*val); + //} + //} + //flattened + //} + + //fn flatten_cols(&self) -> Vec { + //let mut flattened = Vec::with_capacity(self.num_fields); + //for idx in 0..self.num_fields { + //flattened.push(self.cols[idx % self.size][idx / self.size]); + //} + //flattened + //} } fn run() -> Result<(), String> {