doasku

Sudoku solver
git clone git://git.dimitrijedobrota.com/doasku.git
Log | Files | Refs | README | LICENSE

commit 234d12dba299bd4c1b0f48d58f44f7082569d00f
parent 80ab88e90146b327b622201960bae4414106d2e0
Author: Dimitrije Dobrota <mail@dimitrijedobrota.com>
Date:   Sun,  7 Apr 2024 21:49:40 +0200

Add check for naked quads

Diffstat:
Mmain.cpp | 83+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 83 insertions(+), 0 deletions(-)

diff --git a/main.cpp b/main.cpp @@ -107,12 +107,15 @@ class Ref { for (uint8_t i = 0; i < 9; i++) { if (this->res[i]) continue; if (seen_naked_triplet & (1ul << i)) continue; + for (uint8_t j = i + 1; j < 9; j++) { if (this->res[j]) continue; if (seen_naked_triplet & (1ul << j)) continue; + for (uint8_t k = j + 1; k < 9; k++) { if (this->res[k]) continue; if (seen_naked_triplet & (1ul << k)) continue; + uint16_t val = value[i] | value[j] | value[k]; if (std::popcount(val) != 3) continue; @@ -131,6 +134,44 @@ class Ref { return res; } + auto get_naked_quads() const { + std::vector<std::tuple<uint8_t, uint8_t, uint8_t, uint8_t, uint8_t>> res; + for (uint8_t i = 0; i < 9; i++) { + if (this->res[i]) continue; + if (seen_naked_quad & (1ul << i)) continue; + + for (uint8_t j = i + 1; j < 9; j++) { + if (this->res[j]) continue; + if (seen_naked_quad & (1ul << j)) continue; + + for (uint8_t k = j + 1; k < 9; k++) { + if (this->res[k]) continue; + if (seen_naked_quad & (1ul << k)) continue; + + for (uint8_t l = k + 1; l < 9; l++) { + if (this->res[l]) continue; + if (seen_naked_quad & (1ul << l)) continue; + + uint16_t val = value[i] | value[j] | value[k] | value[l]; + if (std::popcount(val) != 4) continue; + + seen_naked_quad |= 1ul << i; + seen_naked_quad |= 1ul << j; + seen_naked_quad |= 1ul << k; + seen_naked_quad |= 1ul << l; + + while (val) { + const uint8_t idx = std::countr_zero(val); + res.emplace_back(idx, i, j, k, l); + val ^= 1ull << idx; + } + } + } + } + } + return res; + } + private: uint16_t value[9] = {mask_field, mask_field, mask_field, mask_field, mask_field, mask_field, mask_field, mask_field, mask_field}; @@ -141,6 +182,7 @@ class Ref { mutable uint16_t seen_naked_pair = 0; mutable uint16_t seen_naked_triplet = 0; + mutable uint16_t seen_naked_quad = 0; }; class Grid { @@ -223,6 +265,19 @@ class Grid { changes++; } + + const auto nq = subgrids[subgrid].get_naked_quads(); + for (const auto [val, f1, f2, f3, f4] : nq) { + std::cout << "naked quads: " << (int)subgrid << " " << (int)f1 << " " << (int)f2 << " " + << (int)f3 << " " << (int)f4 << " " << (int)val << std::endl; + + for (uint8_t field = 0; field < 9; field++) { + if (field == f1 || field == f2 || field == f3 || field == f4) continue; + _clear(subgrid, field, val); + } + + changes++; + } } for (uint8_t row = 0; row < 9; row++) { @@ -264,6 +319,20 @@ class Grid { changes++; } + + const auto nq = rows[row].get_naked_quads(); + for (const auto [val, c1, c2, c3, c4] : nq) { + std::cout << "naked quad row: " << (int)row << " " << (int)c1 << " " << (int)c2 << " " + << (int)c3 << " " << (int)c4 << " " << (int)val << std::endl; + + for (uint8_t col = 0; col < 9; col++) { + if (col == c1 || col == c2 || col == c3 || col == c4) continue; + const auto [subgrid, field] = row_col_t(row, col).relative(); + _clear(subgrid, field, val); + } + + changes++; + } } for (uint8_t col = 0; col < 9; col++) { @@ -305,6 +374,20 @@ class Grid { changes++; } + + const auto nq = cols[col].get_naked_quads(); + for (const auto [val, r1, r2, r3, r4] : nq) { + std::cout << "naked quad col: " << (int)col << " " << (int)r1 << " " << (int)r2 << " " + << (int)r3 << " " << (int)r4 << " " << (int)val << std::endl; + + for (uint8_t row = 0; row < 9; row++) { + if (row == r1 || row == r2 || row == r3 || row == r4) continue; + const auto [subgrid, field] = row_col_t(row, col).relative(); + _clear(subgrid, field, val); + } + + changes++; + } } if (changes) iter++;