Add Hidden Singles check in a box

This commit is contained in:
Drew Galbraith 2023-05-04 01:10:15 -07:00
parent 0e482fcf4b
commit 5e1faa90f5
2 changed files with 31 additions and 328 deletions

View file

@ -15,6 +15,7 @@ uint8_t BoxStart(uint8_t id) {
return row + col;
}
constexpr std::array<uint8_t, 9> kBoxStarts{0, 3, 6, 27, 30, 33, 54, 57, 60};
constexpr std::array<uint8_t, 9> kBoxOffsets{0, 1, 2, 9, 10, 11, 18, 19, 20};
} // namespace
@ -80,9 +81,38 @@ bool Puzzle::ApplyNextStep() {
for (uint8_t v = 1; v <= 9; v++) {
if (cells_[i].IsPossible(v)) {
AssignSquare(i, v);
return true;
}
}
return true;
}
}
// Search for a hidden single in a box.
for (int box = 0; box < 9; box++) {
uint8_t boxroot = kBoxStarts[box];
// FIXME: We should be able to check all of the numbers at once.
for (int n = 1; n <= 9; n++) {
int8_t found_loc = -1;
bool exit = false;
for (int cellind = 0; cellind < 9 && !exit; cellind++) {
Cell cell = cells_[boxroot + kBoxOffsets[cellind]];
if (cell.IsSolved()) {
if (cell.value() == n) {
// This number is solved in this box, we can exit.
exit = true;
}
continue;
}
if (cell.IsPossible(n)) {
if (found_loc != -1) {
exit = true;
}
found_loc = boxroot + kBoxOffsets[cellind];
}
}
if (found_loc != -1 && !exit) {
AssignSquare(found_loc, n);
return true;
}
}
}
return false;