Skip to content

Commit

Permalink
Applied OnlyCellStrategy-Backtracking fix
Browse files Browse the repository at this point in the history
The fix for issue #56 was already present in non-killer Sudoku in
v0.2.0, but never noticed.
This PR pushes the fix for this bug in a successor of v0.2.0.
  • Loading branch information
florian1345 committed Apr 5, 2021
1 parent 57c9dbe commit 230bc79
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 8 deletions.
34 changes: 34 additions & 0 deletions src/fix_tests.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
use crate::{Sudoku, SudokuGrid};
use crate::constraint::DefaultConstraint;
use crate::solver::{Solution, Solver};
use crate::solver::strategy::{
OnlyCellStrategy,
StrategicBacktrackingSolver
};

#[test]
fn only_cell_strategic_backtracking() {
let sudoku = Sudoku::parse("3x3;
, , , , ,7,3, , ,\
,1,2, , , ,5,4, ,\
, ,3,4, , , ,1, ,\
, ,5,6, , , ,8, ,\
, , , , , , , , ,\
7, , , , ,2,4, , ,\
6,4,1, , , ,8, , ,\
5,3, , , ,6,7, , ,\
, , , , ,9, , , ", DefaultConstraint).unwrap();
let solver = StrategicBacktrackingSolver::new(OnlyCellStrategy);
let expected = SudokuGrid::parse("3x3;
4,5,6,2,1,7,3,9,8,\
8,1,2,9,6,3,5,4,7,\
9,7,3,4,5,8,6,1,2,\
1,2,5,6,7,4,9,8,3,\
3,6,4,8,9,1,2,7,5,\
7,9,8,5,3,2,4,6,1,\
6,4,1,7,2,5,8,3,9,\
5,3,9,1,8,6,7,2,4,\
2,8,7,3,4,9,1,5,6").unwrap();

assert_eq!(Solution::Unique(expected), solver.solve(&sudoku));
}
3 changes: 3 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,9 @@ pub mod generator;
pub mod solver;
pub mod util;

#[cfg(test)]
mod fix_tests;

use constraint::Constraint;
use error::{SudokuError, SudokuParseError, SudokuParseResult, SudokuResult};

Expand Down
9 changes: 1 addition & 8 deletions src/solver/strategy/solvers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,15 +130,8 @@ impl<S: Strategy> StrategicBacktrackingSolver<S> {

for number in options {
let mut sudoku_info = sudoku_info.clone();
sudoku_info
.enter_cell_no_update(min_options_column, min_options_row, number)
sudoku_info.enter_cell(min_options_column, min_options_row, number)
.unwrap();
let options_info = sudoku_info
.get_options_mut(min_options_column, min_options_row)
.unwrap();
options_info.clear();
options_info.insert(number).unwrap();

let next_solution = self.solve_rec(&mut sudoku_info);
solution = solution.union(next_solution);

Expand Down

0 comments on commit 230bc79

Please sign in to comment.