Skip to content

Commit

Permalink
fix: more idiomatic Rust
Browse files Browse the repository at this point in the history
  • Loading branch information
mlbright committed Nov 30, 2022
1 parent 3532f79 commit 5fa9816
Showing 1 changed file with 19 additions and 19 deletions.
38 changes: 19 additions & 19 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,15 +155,13 @@ impl Sudoku {
puzzle[square].remove(value_to_eliminate);

match puzzle[square].len() {
0 => return false,
0 => return false, // Contradiction
1 => {
// (1) If a square s is reduced to one value, then eliminate it from its peers.
if puzzle[square].len() == 1 {
let last_value = puzzle[square].first();
for peer in self.peers[square].iter() {
if !self.eliminate(puzzle, *peer, last_value) {
return false;
}
let last_value = puzzle[square].first();
for peer in self.peers[square].iter() {
if !self.eliminate(puzzle, *peer, last_value) {
return false;
}
}
}
Expand Down Expand Up @@ -215,34 +213,36 @@ impl Sudoku {
match p {
None => None,
Some(puzzle) => {
let mut min_square = 82;
let mut min_square: Option<usize> = None;
let mut min_size = 10;

for (i, square) in puzzle.iter().enumerate() {
let size = square.len();
if size > 1 && size < min_size {
min_square = i;
min_square = Some(i);
min_size = size;
if min_size == 2 {
break;
}
}
}

if min_square == 82 {
return Some(puzzle);
}

for value in 1..=9 {
if puzzle[min_square].contains(value) {
let mut puzzle_copy = puzzle.clone();
if self.assign(&mut puzzle_copy, min_square, value) {
if let Some(result) = self.search(Some(puzzle_copy)) {
return Some(result);
match min_square {
None => return Some(puzzle),
Some(min_sq) => {
for value in 1..=9 {
if puzzle[min_sq].contains(value) {
let mut puzzle_copy = puzzle.clone();
if self.assign(&mut puzzle_copy, min_sq, value) {
if let Some(result) = self.search(Some(puzzle_copy)) {
return Some(result);
}
}
}
}
}
}

None
}
}
Expand Down

0 comments on commit 5fa9816

Please sign in to comment.