simpler for loop

This commit is contained in:
WanderingPenwing 2024-12-03 09:08:02 +01:00
parent b6e9c944f6
commit d5e451e604

View file

@ -111,38 +111,27 @@ impl Sudoku {
let mut collapsed_possibility = HashSet::new(); let mut collapsed_possibility = HashSet::new();
collapsed_possibility.insert(collapsed_state); collapsed_possibility.insert(collapsed_state);
for column_index in 0..self.size { for index in 0..self.size {
if column_index == collapsed_column { if index != collapsed_column {
continue; self.remove_allowed(collapsed_row, index, &collapsed_possibility)?;
} }
if self.grid[collapsed_row][column_index].get() == Some(collapsed_state) { if index != collapsed_row {
return Err(WaveError::Contradiction) self.remove_allowed(index, collapsed_column, &collapsed_possibility)?;
} }
self.remove_allowed(collapsed_row, column_index, &collapsed_possibility)?; let square_row = (collapsed_row/self.square_size)*self.square_size + index/self.square_size;
} let square_column = (collapsed_column/self.square_size)*self.square_size + index%self.square_size;
for row_index in 0..self.size { if square_row != collapsed_row || square_column != collapsed_column {
if row_index == collapsed_row { self.remove_allowed(square_row, square_column, &collapsed_possibility)?;
continue;
}
if self.grid[row_index][collapsed_column].get() == Some(collapsed_state) {
return Err(WaveError::Contradiction)
}
self.remove_allowed(row_index, collapsed_column, &collapsed_possibility)?;
}
for row_index in 0..self.square_size {
for column_index in 0..self.square_size {
let row = (collapsed_row/self.square_size)*self.square_size + row_index;
let column = (collapsed_column/self.square_size)*self.square_size + column_index;
if row == collapsed_row && column == collapsed_column {
continue;
}
if self.grid[row][column].get() == Some(collapsed_state) {
return Err(WaveError::Contradiction)
}
self.remove_allowed(row, column, &collapsed_possibility)?;
} }
} }
self.check_impossible(collapsed_row, collapsed_column)?;
Ok(())
}
fn check_impossible(&self, collapsed_row: usize, collapsed_column: usize) -> Result<(),WaveError> {
let mut missing_states: HashSet<usize> = (1..=self.size).collect(); let mut missing_states: HashSet<usize> = (1..=self.size).collect();
for column_index in 0..self.size { for column_index in 0..self.size {
if let Some(state) = self.grid[collapsed_row][column_index].get() { if let Some(state) = self.grid[collapsed_row][column_index].get() {
@ -195,7 +184,6 @@ impl Sudoku {
} }
return Err(WaveError::Contradiction) return Err(WaveError::Contradiction)
} }
Ok(()) Ok(())
} }
@ -269,26 +257,17 @@ impl Sudoku {
} }
fn propagate_backtrack(&mut self, cell_pos: [usize; 2], removed_state: usize) { fn propagate_backtrack(&mut self, cell_pos: [usize; 2], removed_state: usize) {
for row_index in 0..self.size { for index in 0..self.size {
if row_index == cell_pos[0] { if index != cell_pos[0] {
continue self.grid[index][cell_pos[1]].add_allowed(removed_state);
} }
self.grid[row_index][cell_pos[1]].add_allowed(removed_state); if index != cell_pos[1] {
self.grid[cell_pos[0]][index].add_allowed(removed_state);
} }
for column_index in 0..self.size { let square_row = (cell_pos[0]/self.square_size)*self.square_size + index/self.square_size;
if column_index == cell_pos[1] { let square_column = (cell_pos[1]/self.square_size)*self.square_size + index%self.square_size;
continue if square_row != cell_pos[0] || square_column != cell_pos[1] {
} self.grid[square_row][square_column].add_allowed(removed_state);
self.grid[cell_pos[0]][column_index].add_allowed(removed_state);
}
for row_index in 0..self.square_size {
for column_index in 0..self.square_size {
let row = (cell_pos[0]/self.square_size)*self.square_size + row_index;
let column = (cell_pos[1]/self.square_size)*self.square_size + column_index;
if row == cell_pos[0] && column == cell_pos[1] {
continue
}
self.grid[row][column].add_allowed(removed_state);
} }
} }
} }
@ -302,6 +281,11 @@ impl Sudoku {
} }
fn remove_allowed(&mut self, row_index: usize, column_index: usize, set_to_remove: &HashSet<usize>) -> Result<(), WaveError> { fn remove_allowed(&mut self, row_index: usize, column_index: usize, set_to_remove: &HashSet<usize>) -> Result<(), WaveError> {
if let Some(state) = self.grid[row_index][column_index].get() {
if set_to_remove.contains(&state) {
return Err(WaveError::Contradiction)
}
}
match self.grid[row_index][column_index].remove_allowed(set_to_remove) { match self.grid[row_index][column_index].remove_allowed(set_to_remove) {
Ok(result) => { Ok(result) => {
let cell::RemoveResult::Collapsed(state) = result else { let cell::RemoveResult::Collapsed(state) = result else {