Compare commits

...

7 Commits

Author SHA1 Message Date
ff43ba5287 Make add and clear use indices.
This is the last commit of the workstream and we
have a ~10% speed improvement.
2025-09-04 11:27:49 +02:00
f1445b7544 Make next_pos iterate over indices. 2025-09-04 11:12:51 +02:00
ed71280dc2 Convert largest_square to use pos indices.
This makes the code a bit more confusing but no functional change.
2025-09-04 11:09:22 +02:00
a066fdb552 Convert Pos to an index.
Pos is now just an index into the grid.

This will be a slow down for the moment as we haven't worked through the
 changes to the algorithms yet.
2025-09-04 11:02:54 +02:00
e557933590 [1/n]: Refactor x() & y() calls on position
The goal of this sequence of patches is to end up representing a
position as a single integer index into the grid instead of an (x, y)
pair.  I believe this will have better performance.

First step move all calls of x(), y() into calls on methods of Results &
Grid as in the future these will need to use the length of the grid.
2025-09-04 10:53:20 +02:00
65569982ee Abstract away types and use 64-bit everywhere
We abstract away the types being used from 'int' and 'char'.  The one
exception being for output.

This allows us to experiment to see if using different types can improve
performance.

It turns out it does - and that using 64-bit integers everywhere is a
good idea.
2025-09-03 09:30:33 +02:00
4b9feefbf4 Split out results structure
We now return the results in a separate structure to the grid we have
been working on.

This ultimately makes it easier to play with the implementation of the
solution finder.
2025-09-03 08:59:06 +02:00

240
main.cc
View File

@@ -13,15 +13,11 @@
#include <iostream> #include <iostream>
#include <set> #include <set>
using size_t = std::size_t;
namespace { namespace {
/** (x, y) pair storing a position. */ /** (x, y) pair storing a position. */
using Pos = std::pair<int, int>; using Pos = std::size_t;
/** Get x co-ordinate from position */
auto x(Pos const &p) noexcept -> int { return p.first; }
/** Get y co-ordinate from position */
auto y(Pos const &p) noexcept -> int { return p.second; }
/** A square - consisting of position of closest corner to origin, and side-length. /** A square - consisting of position of closest corner to origin, and side-length.
*/ */
@@ -30,7 +26,7 @@ namespace {
* \param pos Position of closest corner to origin * \param pos Position of closest corner to origin
* \param length Side length. * \param length Side length.
*/ */
Square(Pos const &pos, int const length) noexcept : pos_(pos), length_(length) { Square(Pos pos, size_t const length) noexcept : pos_(pos), length_(length) {
} }
Square(Square const &other) noexcept = default; Square(Square const &other) noexcept = default;
@@ -44,23 +40,101 @@ namespace {
~Square() noexcept = default; ~Square() noexcept = default;
/** Get x co-ordinate of closest corner to origin. */ /** Get x co-ordinate of closest corner to origin. */
auto x() const noexcept -> int { return ::x(pos_); } auto pos() const noexcept -> Pos { return pos_; }
/** Get y co-ordinate of closest corner to origin. */
auto y() const noexcept -> int { return ::y(pos_); }
/** Get side length. */ /** Get side length. */
auto length() const noexcept -> int { return length_; } auto length() const noexcept -> size_t { return length_; }
private: private:
Pos pos_; ///< Position of corner closest to origin Pos pos_; ///< Position of corner closest to origin
int length_; ///< Side length size_t length_; ///< Side length
};
/** Structure holding the results.
*/
struct Results {
Results(size_t length, std::vector<Square> squares) : length_(length), squares_(std::move(squares)) {
}
Results(Results const &other) noexcept = delete;
Results &operator=(Results const &other) noexcept = delete;
Results &operator=(Results &&other) noexcept = default;
Results(Results &&other) noexcept = default;
~Results() noexcept = default;
auto length() const noexcept -> size_t { return length_; }
/** Output the grid. */
auto output() const -> void {
std::string out(length_ * length_, '.');
for (auto const &sq: squares_) {
prettify_sq(out, sq);
}
for (size_t idx = 0; idx < length_ * length_; idx += length_) {
std::cout << std::string_view(out.data() + idx, length_) << '\n';
}
}
private:
auto set(std::string &s, size_t x, size_t y, char c) const noexcept -> void {
assert(x < length_);
assert(y < length_);
assert(grid_[x + y * length_] != c);
s[x + y * length_] = c;
}
auto sq_x(Square const &sq) const noexcept -> size_t { return sq.pos() % length_; }
auto sq_y(Square const &sq) const noexcept -> size_t { return sq.pos() / length_; }
auto prettify_sq(std::string &s, Square const &sq) const noexcept -> void {
switch (sq.length()) {
case 1: set(s, sq_x(sq), sq_y(sq), '*');
break;
case 2: set(s, sq_x(sq), sq_y(sq), '+');
set(s, sq_x(sq) + 1, sq_y(sq), '+');
set(s, sq_x(sq), sq_y(sq) + 1, '+');
set(s, sq_x(sq) + 1, sq_y(sq) + 1, '+');
break;
default: {
auto n = sq.length();
set(s, sq_x(sq), sq_y(sq), '+');
set(s, sq_x(sq) + n - 1, sq_y(sq), '+');
set(s, sq_x(sq), sq_y(sq) + n - 1, '+');
set(s, sq_x(sq) + n - 1, sq_y(sq) + n - 1, '+');
for (size_t i = 1; i < n - 1; ++i) {
set(s, sq_x(sq) + i, sq_y(sq), '-');
set(s, sq_x(sq) + i, sq_y(sq) + n - 1, '-');
set(s, sq_x(sq), sq_y(sq) + i, '|');
for (size_t j = 1; j < n - 1; ++j) {
set(s, sq_x(sq) + j, sq_y(sq) + i, ' ');
}
set(s, sq_x(sq) + n - 1, sq_y(sq) + i, '|');
}
size_t i = sq_x(sq) + n - 1;
while (n != 0) {
set(s, --i, sq_y(sq) + 1, '0' + (n % 10));
n /= 10;
}
}
}
}
size_t length_;
std::vector<Square> squares_;
}; };
/** An N * N grid of characters. */ /** An N * N grid of characters. */
struct Grid { struct Grid {
// Type to use for the grid contents
using T = std::int_fast64_t;
/** Construct a grid of given side-length. */ /** Construct a grid of given side-length. */
Grid(int length) : grid_(length * length, empty), length_(length) { Grid(size_t length) : grid_(length * length, empty), length_(length) {
} }
Grid(Grid const &other) = delete; Grid(Grid const &other) = delete;
@@ -74,47 +148,35 @@ namespace {
~Grid() noexcept = default; ~Grid() noexcept = default;
/** Get grid length */ /** Get grid length */
auto length() const noexcept -> int { return length_; } auto end() const noexcept -> size_t { return grid_.size(); }
/** Add a square to the grid. */ /** Add a square to the grid. */
auto add(Square const &sq) noexcept -> void { auto add(Square const &sq) noexcept -> void {
for (auto i = sq.x(); i < sq.x() + sq.length(); ++i) { /* One would expect the fastest way to do this would be to have x be the
for (auto j = sq.y(); j < sq.y() + sq.length(); ++j) { * fastest increasing index so we stores [pos, pos + 1,..., pos+length, ...]
set(i, j, sq.length()); * But experimentation tells us this isn't so, and storing
* [pos, pos + length, ..., pos + 1, ...] is faster!
*/
for (auto x = 0; x < sq.length(); ++x) {
for (auto y = sq.pos(); y < sq.pos() + sq.length() * length_; y += length_) {
grid_[x + y] = filled;
} }
} }
} }
/** Clear a square from the grid. */ /** Clear a square from the grid. */
auto clear(Square const &sq) noexcept -> void { auto clear(Square const &sq) noexcept -> void {
for (auto i = sq.x(); i < sq.x() + sq.length(); ++i) { for (auto x = 0; x < sq.length(); ++x) {
for (auto j = sq.y(); j < sq.y() + sq.length(); ++j) { for (auto y = sq.pos(); y < sq.pos() + sq.length() * length_; y += length_) {
set(i, j, empty); grid_[x + y] = empty;
} }
} }
} }
auto prettify() noexcept -> void {
for (auto idx = 0; idx < grid_.size(); ++idx) {
if (grid_[idx] < 32) {
auto const pos = std::make_pair(idx % length_, idx / length_);
prettify_sq(Square(pos, grid_[idx]));
}
}
}
/** Output the grid. */
auto output() const -> void {
for (auto idx = 0; idx < length_ * length_; idx += length_) {
std::cout << std::string_view(grid_.data() + idx, length_) << '\n';
}
}
/** \brief Get length of the largest square that fits at \a pos in the grid. /** \brief Get length of the largest square that fits at \a pos in the grid.
*/ */
auto largest_square(Pos const &pos, int n) const noexcept -> int { auto largest_square(Pos pos, size_t n) const noexcept -> size_t {
assert(x(pos) < length_); assert(pos < end());
assert(y(pos) < length_);
/* Because of how we walk through the grid (starting at 0,0 then increasing /* Because of how we walk through the grid (starting at 0,0 then increasing
* x followed by y) we can assume that if the position (b, y) is clear * x followed by y) we can assume that if the position (b, y) is clear
@@ -123,93 +185,63 @@ namespace {
* This means we only need to look for the first non-clear position along the * This means we only need to look for the first non-clear position along the
* current row. * current row.
*/ */
auto b = x(pos); auto const pos_x = pos % length_;
auto const pos_y0 = pos - pos_x;
auto b = pos;
// Make sure we don't go looking in the next row. // Make sure we don't go looking in the next row.
auto e = std::min(x(pos) + n, length_); auto const e = std::min(pos + n, pos_y0 + length_);
while (b < e) { while (b < e) {
if (grid_[b + y(pos) * length_] != '.') { break; } if (grid_[b] != empty) { break; }
++b; ++b;
} }
// Check that this length fits vertically as well. // Check that this length fits vertically as well.
auto len = b - x(pos); auto const len = b - pos;
auto ye = std::min(y(pos) + len, length_); auto const pos_y = pos / length_;
return ye - y(pos); auto const ye = std::min(pos_y + len, length_);
return ye - pos_y;
} }
/** Get the next position to check. n is the size of the square we just /** Get the next position to check starting at pos.
* added. *
* Returns grid_.length() if no more positions avaialble.
*/ */
auto next_pos(Pos const &pos, int n) const noexcept -> Pos { auto next_pos(Pos pos) const noexcept -> Pos {
auto const b = grid_.begin() + x(pos) + n + y(pos) * length_; auto const b = grid_.begin() + pos;
auto const p = std::find(b, grid_.end(), empty); auto const p = std::find(b, grid_.end(), empty);
auto const v = p - grid_.begin(); return p - grid_.begin();
return std::make_pair(v % length_, v / length_);
} }
private: private:
auto prettify_sq(Square const &sq) noexcept -> void {
switch (sq.length()) {
case 1: set(sq.x(), sq.y(), '*');
break;
case 2: set(sq.x(), sq.y(), '+');
set(sq.x() + 1, sq.y(), '+');
set(sq.x(), sq.y() + 1, '+');
set(sq.x() + 1, sq.y() + 1, '+');
break;
default: {
auto n = sq.length();
set(sq.x(), sq.y(), '+');
set(sq.x() + n - 1, sq.y(), '+');
set(sq.x(), sq.y() + n - 1, '+');
set(sq.x() + n - 1, sq.y() + n - 1, '+');
for (int i = 1; i < n - 1; ++i) {
set(sq.x() + i, sq.y(), '-');
set(sq.x() + i, sq.y() + n - 1, '-');
set(sq.x(), sq.y() + i, '|');
for (int j = 1; j < n - 1; ++j) {
set(sq.x() + j, sq.y() + i, ' ');
}
set(sq.x() + n - 1, sq.y() + i, '|');
}
int i = sq.x() + n - 1;
while (n != 0) {
set(--i, sq.y() + 1, '0' + (n % 10));
n /= 10;
}
}
}
}
/** Set the grid position (x, y) to the character c. /** Set the grid position (x, y) to the character c.
* *
* It is an error if (x, y) is already set to c - as that means we have overlapping * It is an error if (x, y) is already set to c - as that means we have overlapping
* squares. * squares.
*/ */
auto set(int x, int y, char c) noexcept -> void { auto set(size_t x, size_t y, T c) noexcept -> void {
assert(x < length_); assert(x < length_);
assert(y < length_); assert(y < length_);
assert(grid_[x + y * length_] != c); assert(grid_[x + y * length_] != c);
grid_[x + y * length_] = c; grid_[x + y * length_] = c;
} }
std::vector<char> grid_; ///< The grid std::vector<T> grid_; ///< The grid
int length_; ///< Side length size_t length_; ///< Side length
static constexpr char empty = '.'; ///< Character used for an empty cell. static constexpr char empty = 0; ///< Character used for an empty cell.
static constexpr char filled = 1; ///< Character used for a filled cell,
}; };
/** Get the n'th triangular number. */ /** Get the n'th triangular number. */
auto triangle_num(int n) noexcept -> int { return (n * (n + 1)) / 2; } auto triangle_num(size_t n) noexcept -> size_t { return (n * (n + 1)) / 2; }
/** Vector used to identify the available squares. */ /** Vector used to identify the available squares. */
using Avail = std::vector<int>; using Avail = std::vector<size_t>;
/** Find a solution to the \a n th Partridge problem. /** Find a solution to the \a n th Partridge problem.
* *
* Returns the grid of the solution. * Returns the grid of the solution.
*/ */
auto find_solution(int const n) noexcept -> Grid { auto find_solution(size_t const n) noexcept -> Results {
/* Implementation is iterative, as opposed to recursive. /* Implementation is iterative, as opposed to recursive.
* *
* The recursive implementation is easier to understand - but is * The recursive implementation is easier to understand - but is
@@ -238,8 +270,8 @@ namespace {
sqs.reserve(length); sqs.reserve(length);
// Start at the origin with a square of longest side length. // Start at the origin with a square of longest side length.
Pos pos{0, 0}; Pos pos = 0;
int idx = n; size_t idx = n;
while (true) { while (true) {
/* If the idx is 0 we've looked at all possible square lengths for this /* If the idx is 0 we've looked at all possible square lengths for this
@@ -254,13 +286,16 @@ namespace {
sqs.pop_back(); sqs.pop_back();
grid.clear(sq); grid.clear(sq);
++avail_sqs[sq.length()]; ++avail_sqs[sq.length()];
pos = std::make_pair(sq.x(), sq.y()); pos = sq.pos();
idx = sq.length() - 1; idx = sq.length() - 1;
continue; continue;
} }
// If there are no squares available of the current size try the next one. // If there are no squares available of the current size try the next one.
if (avail_sqs[idx] == 0) { --idx; continue; } if (avail_sqs[idx] == 0) {
--idx;
continue;
}
/* Place a square of side length idx at pos, push this onto the stack and /* Place a square of side length idx at pos, push this onto the stack and
* set up to look at the next position. * set up to look at the next position.
@@ -270,20 +305,19 @@ namespace {
grid.add(sq); grid.add(sq);
sqs.push_back(sq); sqs.push_back(sq);
pos = grid.next_pos(pos, idx); pos = grid.next_pos(pos + idx);
idx = grid.largest_square(pos, n); idx = grid.largest_square(pos, n);
// Have we reached the end? If so success! // Have we reached the end? If so success!
if (x(pos) == 0 && y(pos) == grid.length()) { break; } if (pos == grid.end()) { break; }
} }
grid.prettify(); return Results(length, sqs);
return grid;
} }
} // anon namespace } // anon namespace
int main(int argc, char **argv) { int main(int argc, char **argv) {
auto n = (argc == 1) ? 8 : atoi(argv[1]); auto n = (argc == 1) ? 8 : std::atol(argv[1]);
auto grid = find_solution(n); auto grid = find_solution(n);
std::cout << "Partridge problem " << n << " side length " << grid.length() << '\n'; std::cout << "Partridge problem " << n << " side length " << grid.length() << '\n';
grid.output(); grid.output();