Commit Graph

8 Commits

Author SHA1 Message Date
9cbc6c4cbc Comment code. 2025-09-01 12:32:43 +02:00
e865b2a567 Change Grid to use vector not string.
This is another optimisation, as it improves memory layout and we are
not using a reference counted string.

On a M1 Macbook Air this brings runtime for solving the 9 cell down to
under 3m30s.  Which is about a 15% performance improvement.
2025-09-01 11:42:07 +02:00
355c4211aa Fix bug in largest_square: y direction
We were not taking into account that the largest_square may be
constrained by the y position as well as the x.  i.e. is there enough
vertical space for the square.

This commit adds that check.

We were working before because C++ is not memory safe, and we weren't
running debug builds.
2025-09-01 11:30:16 +02:00
d7b1290379 Optimize selection of next position.
Grid::next_pos was starting its scan from the position after the current
 one.

This is inefficient as we know how large the square we've just placed is
 as we scan those positions.

This optimisation just skips that square when starting the scan.
2025-09-01 11:00:56 +02:00
3095d38b8a Optimize: if sq N fits then sq (N - 1) does too.
As an optimisation we know that if a square with side length N fits in a
certain position, then one of side length N - 1 does too.  And
conversely, if a square of side length N does not fit then one of side
length N + 1 does not.

So our implementation which asked if every square fitted in a certain
position called Grid::fits() too many times, once for each side length.

We replace this by a function Grid::largest_square which gives the
appropriate side length to start with.

Naïvely the optimisation here is that instead of checking whether N
squares fit, and so having to do (N * (N + 1)) / 2 comparisons in fits
per position, we instead do one scan in largest_square and do at most N
comparisons.

A performance improvement is also seen in real life.
2025-09-01 10:34:24 +02:00
6ac6f2af9b Update VCS config. 2025-08-31 17:40:43 +01:00
d85a5ae57e Change the iteration method used.
We stop using function calls to move down an index but instead use a for
 loop.

This actually provides a slight performance improvement as we are not
repeated making and destroying function frames.
2025-08-31 17:40:30 +01:00
2423f39c57 Initial C++ version of Partridge solver
This is almost a complete copy of the OCaml version

About twice as fast.
2025-08-31 17:05:06 +01:00