From f1445b754426df8b0863438fd0106c16bf629bbf Mon Sep 17 00:00:00 2001 From: Matthew Gretton-Dann Date: Thu, 4 Sep 2025 11:12:51 +0200 Subject: [PATCH] Make next_pos iterate over indices. --- main.cc | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/main.cc b/main.cc index 20b51ae..f92ab2a 100644 --- a/main.cc +++ b/main.cc @@ -145,6 +145,7 @@ namespace { /** Get grid length */ auto length() const noexcept -> size_t { return length_; } + auto end() const noexcept -> size_t { return grid_.size(); } auto pos_x(Pos const& pos) const noexcept -> size_t { return pos % length_; } auto pos_y(Pos const& pos) const noexcept -> size_t { return pos / length_; } @@ -195,14 +196,14 @@ namespace { return ye - pos_y(pos); } - /** Get the next position to check. n is the size of the square we just - * added. + /** Get the next position to check starting at pos. + * + * Returns grid_.length() if no more positions avaialble. */ - auto next_pos(Pos const &pos, size_t n) const noexcept -> Pos { - auto const b = grid_.begin() + pos_x(pos) + n + pos_y(pos) * length_; + auto next_pos(Pos const &pos) const noexcept -> Pos { + auto const b = grid_.begin() + pos; auto const p = std::find(b, grid_.end(), empty); - auto const v = p - grid_.begin(); - return v; + return p - grid_.begin(); } private: @@ -296,11 +297,11 @@ namespace { grid.add(sq); sqs.push_back(sq); - pos = grid.next_pos(pos, idx); + pos = grid.next_pos(pos + idx); idx = grid.largest_square(pos, n); // Have we reached the end? If so success! - if (grid.pos_x(pos) == 0 && grid.pos_y(pos) == grid.length()) { break; } + if (pos == grid.end()) { break; } } return Results(length, sqs);