h48

A prototype for an optimal Rubik's cube solver, work in progress.
git clone https://git.tronto.net/h48
Download | Log | Files | Refs | README | LICENSE

commit f1575ed335166a0897f2cfd314a0f4bade6a10fc
parent 3485be851f4fff1c9c6fe621f0619d2b4103bc06
Author: Sebastiano Tronto <sebastiano@tronto.net>
Date:   Sun, 20 Apr 2025 16:47:52 +0200

Improve order of moves in NISS solutions

Now the side that has more moves is written first.
For example: write (U L F) B instead of B (U L F)

This also fixes the test on appendsolutions, which used an older version
of the function's signature.

Diffstat:
Msrc/solvers/solutions.h | 80+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++------------------
Mtest/130_appendsolution/appendsolution_tests.c | 5+++--
2 files changed, 65 insertions(+), 20 deletions(-)

diff --git a/src/solvers/solutions.h b/src/solvers/solutions.h @@ -6,6 +6,10 @@ STATIC bool solution_moves_equal( const solution_moves_t [static 1], const solution_moves_t [static 1]); STATIC bool solution_moves_is_duplicate(size_t n, const solution_moves_t[n+1]); STATIC bool appendchar(solution_list_t [static 1], char); +STATIC bool appendnormal( + const solution_moves_t [static 1], solution_list_t [static 1]); +STATIC bool appendinverse( + const solution_moves_t [static 1], solution_list_t [static 1]); STATIC int64_t appendsolution(const solution_moves_t [static 1], const solution_settings_t [static 1], solution_list_t [static 1], bool, const char *); @@ -94,6 +98,47 @@ appendchar(solution_list_t solutions[static 1], char c) return true; } +STATIC bool +appendnormal( + const solution_moves_t moves[static 1], + solution_list_t list[static 1] +) +{ + int64_t strl; + + if (moves->nmoves == 0) + return true; + + if ((strl = writemoves(moves->nmoves, moves->moves, + list->size - list->used, list->buf + list->used)) < 0) + return false; + list->used += strl; + + return true; +} + +STATIC bool +appendinverse( + const solution_moves_t moves[static 1], + solution_list_t list[static 1] +) +{ + int64_t strl; + + if (moves->npremoves == 0) + return true; + + if (!appendchar(list, '(')) + return false; + + if ((strl = writemoves(moves->npremoves, moves->premoves, + list->size - list->used, list->buf + list->used)) < 0) + return false; + list->used += strl; + + return appendchar(list, ')'); +} + STATIC int64_t appendsolution( const solution_moves_t moves[static 1], @@ -103,7 +148,7 @@ appendsolution( const char *solver_name ) { - int64_t r, strl; + int64_t r; int i; uint8_t t; solution_moves_t tsol[NTRANS]; @@ -151,28 +196,27 @@ appendsolution( last_start = list->buf + list->used; - /* Write moves on normal */ - strl = writemoves(tsol[r].nmoves, tsol[r].moves, - list->size - list->used, list->buf + list->used); - if (strl < 0) - goto appendsolution_error_buffer; - list->used += strl; + /* Append first the moves on the side that has more */ + /* E.g. write (U L F) B instead of B (U L F) */ + if (tsol[r].nmoves >= tsol[r].npremoves) { + if (!appendnormal(&tsol[r], list)) + goto appendsolution_error_buffer; - /* Write moves on inverse with NISS notation */ - if (tsol[r].npremoves > 0) { - if (strl > 0) + if (tsol[r].nmoves > 0 && tsol[r].npremoves > 0) if (!appendchar(list, ' ')) - goto appendsolution_error_buffer; - if (!appendchar(list, '(')) - goto appendsolution_error_buffer; + return false; - strl = writemoves(tsol[r].npremoves, tsol[r].premoves, - list->size - list->used, list->buf + list->used); - if (strl < 0) + if (!appendinverse(&tsol[r], list)) goto appendsolution_error_buffer; - list->used += strl; + } else { + if (!appendinverse(&tsol[r], list)) + goto appendsolution_error_buffer; + + if (tsol[r].nmoves > 0 && tsol[r].npremoves > 0) + if (!appendchar(list, ' ')) + return false; - if (!appendchar(list, ')')) + if (!appendnormal(&tsol[r], list)) goto appendsolution_error_buffer; } diff --git a/test/130_appendsolution/appendsolution_tests.c b/test/130_appendsolution/appendsolution_tests.c @@ -17,7 +17,8 @@ int64_t readmoves(const char *, size_t n, uint8_t [n]); void solution_moves_reset(solution_moves_t [static 1]); bool solution_list_init(solution_list_t [static 1], size_t n, char [n]); int64_t appendsolution(const solution_moves_t [static 1], - const solution_settings_t [static 1], solution_list_t [static 1]); + const solution_settings_t [static 1], solution_list_t [static 1], bool, + const char *); void run(void) { int i, ntrans; @@ -49,7 +50,7 @@ void run(void) { settings.tmask |= UINT64_C(1) << (uint64_t)readtrans(str); } - appendsolution(&moves, &settings, &list); + appendsolution(&moves, &settings, &list, false, "Test"); printf("%s", list.buf); printf("Number of solutions: %" PRIu64 "\n", list.nsols);