fix validation of 3x3 blocks

This commit is contained in:
mxhagen 2024-09-24 18:40:02 +02:00
parent 2352f3bfda
commit 46f470821b
2 changed files with 17 additions and 1 deletions

View File

@ -22,7 +22,7 @@ pub fn is_solution(sudoku: &Board) -> bool {
let mut block_set = 0u16;
for j in 0..9 {
let tmp = block_set;
block_set ^= 1 << sudoku[i / 3 * 3 + i % 3][j / 3 * 3 + j % 3];
block_set ^= 1 << sudoku[i / 3 * 3 + j / 3][i % 3 * 3 + j % 3];
if block_set < tmp {
return false;
}

View File

@ -47,3 +47,19 @@ fn invalid_unfinished() {
];
assert!(!is_solution(&board));
}
#[test]
fn invalid_blocks_wrong() {
let board = [
[5, 6, 9, 3, 7, 8, 1, 2, 4],
[7, 4, 3, 1, 2, 9, 6, 5, 8],
[4, 5, 1, 6, 8, 2, 9, 7, 3],
[9, 8, 6, 7, 4, 5, 3, 1, 2],
[6, 3, 7, 2, 5, 1, 4, 8, 9],
[8, 9, 2, 4, 1, 3, 7, 6, 5],
[3, 1, 8, 5, 6, 4, 2, 9, 7],
[2, 7, 4, 8, 9, 6, 5, 3, 1],
[1, 2, 5, 9, 3, 7, 8, 4, 6],
];
assert!(!is_solution(&board));
}