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 89173fe1a7e839191a92ab82f79b42af46b1f9bc
parent 59baae55d72f3aae5ff0d656bf3a3cda7795a879
Author: Sebastiano Tronto <sebastiano@tronto.net>
Date:   Wed, 18 Sep 2024 17:21:36 +0200

Split solve h48 from stats

Diffstat:
Msrc/solvers/h48/h48.h | 1+
Msrc/solvers/h48/solve.h | 100-------------------------------------------------------------------------------
Asrc/solvers/h48/stats.h | 100+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
3 files changed, 101 insertions(+), 100 deletions(-)

diff --git a/src/solvers/h48/h48.h b/src/solvers/h48/h48.h @@ -2,4 +2,5 @@ #include "map.h" #include "gendata_cocsep.h" #include "gendata_h48.h" +#include "stats.h" #include "solve.h" diff --git a/src/solvers/h48/solve.h b/src/solvers/h48/solve.h @@ -16,16 +16,6 @@ typedef struct { uint8_t premoves[MAXLEN]; } dfsarg_solveh48_t; -typedef struct { - cube_t cube; - int8_t nmoves; - int8_t depth; - uint8_t moves[MAXLEN]; - uint32_t *cocsepdata; - uint8_t *h48data; - char *s; -} dfsarg_solveh48stats_t; - STATIC uint32_t allowednextmove_h48(uint8_t *, uint8_t, uint32_t); STATIC void solve_h48_appendsolution(dfsarg_solveh48_t *); @@ -33,9 +23,6 @@ STATIC_INLINE bool solve_h48_stop(dfsarg_solveh48_t *); STATIC int64_t solve_h48_dfs(dfsarg_solveh48_t *); STATIC int64_t solve_h48(cube_t, int8_t, int8_t, int8_t, const void *, char *); -STATIC int64_t solve_h48stats_dfs(dfsarg_solveh48stats_t *); -STATIC int64_t solve_h48stats(cube_t, int8_t, const void *, char [static 12]); - STATIC uint32_t allowednextmove_h48(uint8_t *moves, uint8_t n, uint32_t h48branch) { @@ -215,90 +202,3 @@ solve_h48( return nsols; } - -/* -The h48stats solver computes how many moves it takes to solve to -each of the 12 h48 coordinates, one for each value of h from 0 to 11. -The solutions array is filled with the length of the solutions. The -solution array is therefore not a printable string. -*/ -STATIC int64_t -solve_h48stats_dfs(dfsarg_solveh48stats_t *arg) -{ - const int64_t limit = 11; - - int8_t bound, u; - uint8_t m; - uint32_t d; - int64_t coord, h; - dfsarg_solveh48stats_t nextarg; - - /* Check cocsep lower bound (corners only) */ - bound = get_h48_cdata(arg->cube, arg->cocsepdata, &d); - if (bound + arg->nmoves > arg->depth) - return 0; - - /* Check h48 lower bound for h=0 (esep, but no eo) */ - coord = coord_h48_edges(arg->cube, COCLASS(d), TTREP(d), 0); - bound = get_h48_bound(arg->cube, d, 0, 4, arg->h48data); - if (bound + arg->nmoves > arg->depth) - return 0; - - /* Update all other values, if solved */ - coord = coord_h48_edges(arg->cube, COCLASS(d), TTREP(d), 11); - for (h = 0; h <= limit; h++) { - u = coord >> (11-h) == 0 && arg->s[h] == 99; - arg->s[h] = u * arg->nmoves + (1-u) * arg->s[h]; - } - - if (arg->s[limit] != 99) - return 0; - - nextarg = *arg; - nextarg.nmoves = arg->nmoves + 1; - for (m = 0; m < 18; m++) { - nextarg.moves[arg->nmoves] = m; - if (!allowednextmove(nextarg.moves, nextarg.nmoves)) { - /* If a move is not allowed, neither are its 180 - * and 270 degree variations */ - m += 2; - continue; - } - nextarg.cube = move(arg->cube, m); - solve_h48stats_dfs(&nextarg); - } - - return 0; -} - -STATIC int64_t -solve_h48stats( - cube_t cube, - int8_t maxmoves, - const void *data, - char solutions[static 12] -) -{ - int i; - dfsarg_solveh48stats_t arg; - - arg = (dfsarg_solveh48stats_t) { - .cube = cube, - .cocsepdata = get_cocsepdata_ptr(data), - .h48data = get_h48data_ptr(data), - .s = solutions - }; - - for (i = 0; i < 12; i++) - solutions[i] = (char)99; - - for (arg.depth = 0; - arg.depth <= maxmoves && solutions[11] == 99; - arg.depth++) - { - arg.nmoves = 0; - solve_h48stats_dfs(&arg); - } - - return 0; -} diff --git a/src/solvers/h48/stats.h b/src/solvers/h48/stats.h @@ -0,0 +1,100 @@ +/* +The h48stats solver computes how many moves it takes to solve to +each of the 12 h48 coordinates, one for each value of h from 0 to 11. +The solutions array is filled with the length of the solutions. The +solutions array is therefore not a printable string. +*/ + +typedef struct { + cube_t cube; + int8_t nmoves; + int8_t depth; + uint8_t moves[MAXLEN]; + uint32_t *cocsepdata; + uint8_t *h48data; + char *s; +} dfsarg_solveh48stats_t; + +STATIC int64_t solve_h48stats_dfs(dfsarg_solveh48stats_t *); +STATIC int64_t solve_h48stats(cube_t, int8_t, const void *, char [static 12]); + +STATIC int64_t +solve_h48stats_dfs(dfsarg_solveh48stats_t *arg) +{ + const int64_t limit = 11; + + int8_t bound, u; + uint8_t m; + uint32_t d; + int64_t coord, h; + dfsarg_solveh48stats_t nextarg; + + /* Check cocsep lower bound (corners only) */ + bound = get_h48_cdata(arg->cube, arg->cocsepdata, &d); + if (bound + arg->nmoves > arg->depth) + return 0; + + /* Check h48 lower bound for h=0 (esep, but no eo) */ + coord = coord_h48_edges(arg->cube, COCLASS(d), TTREP(d), 0); + bound = get_h48_bound(arg->cube, d, 0, 4, arg->h48data); + if (bound + arg->nmoves > arg->depth) + return 0; + + /* Update all other values, if solved */ + coord = coord_h48_edges(arg->cube, COCLASS(d), TTREP(d), 11); + for (h = 0; h <= limit; h++) { + u = coord >> (11-h) == 0 && arg->s[h] == 99; + arg->s[h] = u * arg->nmoves + (1-u) * arg->s[h]; + } + + if (arg->s[limit] != 99) + return 0; + + nextarg = *arg; + nextarg.nmoves = arg->nmoves + 1; + for (m = 0; m < 18; m++) { + nextarg.moves[arg->nmoves] = m; + if (!allowednextmove(nextarg.moves, nextarg.nmoves)) { + /* If a move is not allowed, neither are its 180 + * and 270 degree variations */ + m += 2; + continue; + } + nextarg.cube = move(arg->cube, m); + solve_h48stats_dfs(&nextarg); + } + + return 0; +} + +STATIC int64_t +solve_h48stats( + cube_t cube, + int8_t maxmoves, + const void *data, + char solutions[static 12] +) +{ + int i; + dfsarg_solveh48stats_t arg; + + arg = (dfsarg_solveh48stats_t) { + .cube = cube, + .cocsepdata = get_cocsepdata_ptr(data), + .h48data = get_h48data_ptr(data), + .s = solutions + }; + + for (i = 0; i < 12; i++) + solutions[i] = (char)99; + + for (arg.depth = 0; + arg.depth <= maxmoves && solutions[11] == 99; + arg.depth++) + { + arg.nmoves = 0; + solve_h48stats_dfs(&arg); + } + + return 0; +}