doasku

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

commit 233d1d7f9309c2b6d806038da8e50b5ae25fdcd5
parent f74e799445706bddd08fcbce6a24676a8a4f987b
Author: Dimitrije Dobrota <mail@dimitrijedobrota.com>
Date:   Fri, 14 Jun 2024 23:50:30 +0200

Streamline solve function for readability

Diffstat:
Mmain.cpp | 125+++++++++++++++++++++++++------------------------------------------------------
1 file changed, 40 insertions(+), 85 deletions(-)

diff --git a/main.cpp b/main.cpp @@ -264,24 +264,39 @@ class Grid { bool solve() { // clang-format off static const auto sub_op = - [this](void (Grid::*op)(operation_t), Ref::changes_t (Ref::*f)() const, uint8_t subgrid) { - for_each((subgrids[subgrid].*(f))(), [this, subgrid, op](const Ref::change_t ch) { - (this->*(op))(operation_t({cord_t(subgrid), cord_t(std::get<0>(ch))}, std::get<1>(ch))); - }); + [this](void (Grid::*op)(operation_t), Ref::changes_t (Ref::*f)() const) { + for(uint8_t subgrid = 0; subgrid < 9; subgrid++) { + for_each((subgrids[subgrid].*(f))(), [this, subgrid, op](const Ref::change_t ch) { + (this->*(op))(operation_t({cord_t(subgrid), cord_t(std::get<0>(ch))}, std::get<1>(ch))); + }); + } + return changed; }; static const auto row_op = - [this](void (Grid::*op)(operation_t), Ref::changes_t (Ref::*f)() const, uint8_t row) { - for_each((rows[row].*(f))(), [this, row, op](const Ref::change_t ch) { - (this->*(op))(operation_t({row, std::get<0>(ch)}, std::get<1>(ch))); - }); + [this](void (Grid::*op)(operation_t), Ref::changes_t (Ref::*f)() const) { + for(uint8_t row = 0; row < 9; row++) { + for_each((rows[row].*(f))(), [this, row, op](const Ref::change_t ch) { + (this->*(op))(operation_t({row, std::get<0>(ch)}, std::get<1>(ch))); + }); + } + return changed; }; static const auto col_op = - [this](void (Grid::*op)(operation_t), Ref::changes_t (Ref::*f)() const, uint8_t col) { - for_each((cols[col].*(f))(), [this, col, op](const Ref::change_t ch) { - (this->*(op))(operation_t({std::get<0>(ch), col}, std::get<1>(ch))); - }); + [this](void (Grid::*op)(operation_t), Ref::changes_t (Ref::*f)() const) { + for(uint8_t col = 0; col < 9; col++) { + for_each((cols[col].*(f))(), [this, col, op](const Ref::change_t ch) { + (this->*(op))(operation_t({std::get<0>(ch), col}, std::get<1>(ch))); + }); + } + return changed; + }; + + static const auto all_op = + [this](void (Grid::*op)(operation_t), Ref::changes_t (Ref::*f)() const) { + sub_op(op, f), row_op(op, f), col_op(op, f); + return changed; }; // clang-format on @@ -289,79 +304,19 @@ class Grid { while (changed) { changed = false; - for (uint8_t subgrid = 0; subgrid < 9; subgrid++) { - sub_op(&Grid::op_set, &Ref::get_naked_singles, subgrid); - } - - if (changed) continue; - - for (uint8_t idx = 0; idx < 9; idx++) { - sub_op(&Grid::op_set, &Ref::get_hidden_singles, idx); - row_op(&Grid::op_set, &Ref::get_hidden_singles, idx); - col_op(&Grid::op_set, &Ref::get_hidden_singles, idx); - } - - if (changed) continue; - - for (uint8_t subgrid = 0; subgrid < 9; subgrid++) { - sub_op(&Grid::op_clear_row, &Ref::get_pointing_row, subgrid); - sub_op(&Grid::op_clear_col, &Ref::get_pointing_col, subgrid); - } - - if (changed) continue; - - for (uint8_t idx = 0; idx < 9; idx++) { - sub_op(&Grid::op_mask, &Ref::get_hidden_pairs, idx); - row_op(&Grid::op_mask, &Ref::get_hidden_pairs, idx); - col_op(&Grid::op_mask, &Ref::get_hidden_pairs, idx); - } - - if (changed) continue; - - for (uint8_t idx = 0; idx < 9; idx++) { - sub_op(&Grid::op_mask, &Ref::get_hidden_triplets, idx); - row_op(&Grid::op_mask, &Ref::get_hidden_triplets, idx); - col_op(&Grid::op_mask, &Ref::get_hidden_triplets, idx); - } - - if (changed) continue; - - for (uint8_t idx = 0; idx < 9; idx++) { - sub_op(&Grid::op_mask, &Ref::get_hidden_quads, idx); - row_op(&Grid::op_mask, &Ref::get_hidden_quads, idx); - col_op(&Grid::op_mask, &Ref::get_hidden_quads, idx); - } - - if (changed) continue; - - for (uint8_t idx = 0; idx < 9; idx++) { - sub_op(&Grid::op_clear, &Ref::get_naked_pairs, idx); - row_op(&Grid::op_clear, &Ref::get_naked_pairs, idx); - col_op(&Grid::op_clear, &Ref::get_naked_pairs, idx); - } - - if (changed) continue; - - for (uint8_t idx = 0; idx < 9; idx++) { - sub_op(&Grid::op_clear, &Ref::get_naked_triplets, idx); - row_op(&Grid::op_clear, &Ref::get_naked_triplets, idx); - col_op(&Grid::op_clear, &Ref::get_naked_triplets, idx); - } - - if (changed) continue; - - for (uint8_t idx = 0; idx < 9; idx++) { - sub_op(&Grid::op_clear, &Ref::get_naked_quads, idx); - row_op(&Grid::op_clear, &Ref::get_naked_quads, idx); - col_op(&Grid::op_clear, &Ref::get_naked_quads, idx); - } - - if (changed) continue; - - for (uint8_t idx = 0; idx < 9; idx++) { - row_op(&Grid::op_clear_row_rel, &Ref::get_pointing_row, idx); - col_op(&Grid::op_clear_col_rel, &Ref::get_pointing_row, idx); - } + if (sub_op(&Grid::op_set, &Ref::get_naked_singles)) continue; + if (all_op(&Grid::op_set, &Ref::get_hidden_singles)) continue; + if (sub_op(&Grid::op_clear_row, &Ref::get_pointing_row)) continue; + if (sub_op(&Grid::op_clear_col, &Ref::get_pointing_col)) continue; + if (all_op(&Grid::op_mask, &Ref::get_hidden_pairs)) continue; + if (all_op(&Grid::op_mask, &Ref::get_hidden_triplets)) continue; + if (all_op(&Grid::op_mask, &Ref::get_hidden_quads)) continue; + if (all_op(&Grid::op_clear, &Ref::get_naked_pairs)) continue; + if (all_op(&Grid::op_clear, &Ref::get_naked_triplets)) continue; + if (all_op(&Grid::op_clear, &Ref::get_naked_quads)) continue; + + row_op(&Grid::op_clear_row_rel, &Ref::get_pointing_row); + col_op(&Grid::op_clear_col_rel, &Ref::get_pointing_row); } return _is_finished();