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

constants.h (10138B)


      1 #define UINT8_BIT(i) (UINT8_C(1) << (uint8_t)(i))
      2 
      3 #define FACTORIAL_MAX INT64_C(12)
      4 
      5 #define POW_2_11   INT64_C(2048)
      6 #define POW_3_7    INT64_C(2187)
      7 #define FACT_12    INT64_C(479001600)
      8 #define FACT_8     INT64_C(40320)
      9 #define COMB_12_4  INT64_C(495)
     10 #define COMB_8_4   INT64_C(70)
     11 
     12 STATIC int64_t binomial[12][12] = {
     13 	{1,  0,  0,   0,   0,   0,   0,   0,   0,  0,  0, 0},
     14 	{1,  1,  0,   0,   0,   0,   0,   0,   0,  0,  0, 0},
     15 	{1,  2,  1,   0,   0,   0,   0,   0,   0,  0,  0, 0},
     16 	{1,  3,  3,   1,   0,   0,   0,   0,   0,  0,  0, 0},
     17 	{1,  4,  6,   4,   1,   0,   0,   0,   0,  0,  0, 0},
     18 	{1,  5, 10,  10,   5,   1,   0,   0,   0,  0,  0, 0},
     19 	{1,  6, 15,  20,  15,   6,   1,   0,   0,  0,  0, 0},
     20 	{1,  7, 21,  35,  35,  21,   7,   1,   0,  0,  0, 0},
     21 	{1,  8, 28,  56,  70,  56,  28,   8,   1,  0,  0, 0},
     22 	{1,  9, 36,  84, 126, 126,  84,  36,   9,  1,  0, 0},
     23 	{1, 10, 45, 120, 210, 252, 210, 120,  45, 10,  1, 0},
     24 	{1, 11, 55, 165, 330, 462, 462, 330, 165, 55, 11, 1},
     25 };
     26 
     27 #define MOVE_U  UINT8_C(0)
     28 #define MOVE_U2 UINT8_C(1)
     29 #define MOVE_U3 UINT8_C(2)
     30 #define MOVE_D  UINT8_C(3)
     31 #define MOVE_D2 UINT8_C(4)
     32 #define MOVE_D3 UINT8_C(5)
     33 #define MOVE_R  UINT8_C(6)
     34 #define MOVE_R2 UINT8_C(7)
     35 #define MOVE_R3 UINT8_C(8)
     36 #define MOVE_L  UINT8_C(9)
     37 #define MOVE_L2 UINT8_C(10)
     38 #define MOVE_L3 UINT8_C(11)
     39 #define MOVE_F  UINT8_C(12)
     40 #define MOVE_F2 UINT8_C(13)
     41 #define MOVE_F3 UINT8_C(14)
     42 #define MOVE_B  UINT8_C(15)
     43 #define MOVE_B2 UINT8_C(16)
     44 #define MOVE_B3 UINT8_C(17)
     45 
     46 #define TRANS_UFr UINT8_C(0)
     47 #define TRANS_ULr UINT8_C(1)
     48 #define TRANS_UBr UINT8_C(2)
     49 #define TRANS_URr UINT8_C(3)
     50 #define TRANS_DFr UINT8_C(4)
     51 #define TRANS_DLr UINT8_C(5)
     52 #define TRANS_DBr UINT8_C(6)
     53 #define TRANS_DRr UINT8_C(7)
     54 #define TRANS_RUr UINT8_C(8)
     55 #define TRANS_RFr UINT8_C(9)
     56 #define TRANS_RDr UINT8_C(10)
     57 #define TRANS_RBr UINT8_C(11)
     58 #define TRANS_LUr UINT8_C(12)
     59 #define TRANS_LFr UINT8_C(13)
     60 #define TRANS_LDr UINT8_C(14)
     61 #define TRANS_LBr UINT8_C(15)
     62 #define TRANS_FUr UINT8_C(16)
     63 #define TRANS_FRr UINT8_C(17)
     64 #define TRANS_FDr UINT8_C(18)
     65 #define TRANS_FLr UINT8_C(19)
     66 #define TRANS_BUr UINT8_C(20)
     67 #define TRANS_BRr UINT8_C(21)
     68 #define TRANS_BDr UINT8_C(22)
     69 #define TRANS_BLr UINT8_C(23)
     70 
     71 #define TRANS_UFm UINT8_C(24)
     72 #define TRANS_ULm UINT8_C(25)
     73 #define TRANS_UBm UINT8_C(26)
     74 #define TRANS_URm UINT8_C(27)
     75 #define TRANS_DFm UINT8_C(28)
     76 #define TRANS_DLm UINT8_C(29)
     77 #define TRANS_DBm UINT8_C(30)
     78 #define TRANS_DRm UINT8_C(31)
     79 #define TRANS_RUm UINT8_C(32)
     80 #define TRANS_RFm UINT8_C(33)
     81 #define TRANS_RDm UINT8_C(34)
     82 #define TRANS_RBm UINT8_C(35)
     83 #define TRANS_LUm UINT8_C(36)
     84 #define TRANS_LFm UINT8_C(37)
     85 #define TRANS_LDm UINT8_C(38)
     86 #define TRANS_LBm UINT8_C(39)
     87 #define TRANS_FUm UINT8_C(40)
     88 #define TRANS_FRm UINT8_C(41)
     89 #define TRANS_FDm UINT8_C(42)
     90 #define TRANS_FLm UINT8_C(43)
     91 #define TRANS_BUm UINT8_C(44)
     92 #define TRANS_BRm UINT8_C(45)
     93 #define TRANS_BDm UINT8_C(46)
     94 #define TRANS_BLm UINT8_C(47)
     95 
     96 #define NMOVES  (1+MOVE_B3)
     97 #define NTRANS  (1+TRANS_BLm)
     98 
     99 #define MM_ALLMOVES    UINT32_C(0x3FFFF)
    100 #define MM_NOHALFTURNS UINT32_C(0x2DB6D)
    101 
    102 #define CORNER_UFR      UINT8_C(0)
    103 #define CORNER_UBL      UINT8_C(1)
    104 #define CORNER_DFL      UINT8_C(2)
    105 #define CORNER_DBR      UINT8_C(3)
    106 #define CORNER_UFL      UINT8_C(4)
    107 #define CORNER_UBR      UINT8_C(5)
    108 #define CORNER_DFR      UINT8_C(6)
    109 #define CORNER_DBL      UINT8_C(7)
    110 
    111 #define EDGE_UF       UINT8_C(0)
    112 #define EDGE_UB       UINT8_C(1)
    113 #define EDGE_DB       UINT8_C(2)
    114 #define EDGE_DF       UINT8_C(3)
    115 #define EDGE_UR       UINT8_C(4)
    116 #define EDGE_UL       UINT8_C(5)
    117 #define EDGE_DL       UINT8_C(6)
    118 #define EDGE_DR       UINT8_C(7)
    119 #define EDGE_FR       UINT8_C(8)
    120 #define EDGE_FL       UINT8_C(9)
    121 #define EDGE_BL       UINT8_C(10)
    122 #define EDGE_BR       UINT8_C(11)
    123 
    124 #define EOSHIFT     UINT8_C(4)
    125 #define COSHIFT     UINT8_C(5)
    126 
    127 #define PBITS       UINT8_C(0xF)
    128 #define ESEPBIT_1   UINT8_C(0x4)
    129 #define ESEPBIT_2   UINT8_C(0x8)
    130 #define CSEPBIT     UINT8_C(0x4)
    131 #define EOBIT       UINT8_C(0x10)
    132 #define COBITS      UINT8_C(0xF0)
    133 #define COBITS_2    UINT8_C(0x60)
    134 #define CTWIST_CW   UINT8_C(0x20)
    135 #define CTWIST_CCW  UINT8_C(0x40)
    136 #define EFLIP       UINT8_C(0x10)
    137 #define UINT8_ERROR UINT8_C(0xFF)
    138 
    139 STATIC const char *cornerstr[] = {
    140 	[CORNER_UFR] = "UFR",
    141 	[CORNER_UBL] = "UBL",
    142 	[CORNER_DFL] = "DFL",
    143 	[CORNER_DBR] = "DBR",
    144 	[CORNER_UFL] = "UFL",
    145 	[CORNER_UBR] = "UBR",
    146 	[CORNER_DFR] = "DFR",
    147 	[CORNER_DBL] = "DBL"
    148 };
    149 
    150 STATIC const char *cornerstralt[] = {
    151 	[CORNER_UFR] = "URF",
    152 	[CORNER_UBL] = "ULB",
    153 	[CORNER_DFL] = "DLF",
    154 	[CORNER_DBR] = "DRB",
    155 	[CORNER_UFL] = "ULF",
    156 	[CORNER_UBR] = "URB",
    157 	[CORNER_DFR] = "DRF",
    158 	[CORNER_DBL] = "DLB"
    159 };
    160 
    161 STATIC const char *edgestr[] = {
    162 	[EDGE_UF] = "UF",
    163 	[EDGE_UB] = "UB",
    164 	[EDGE_DB] = "DB",
    165 	[EDGE_DF] = "DF",
    166 	[EDGE_UR] = "UR",
    167 	[EDGE_UL] = "UL",
    168 	[EDGE_DL] = "DL",
    169 	[EDGE_DR] = "DR",
    170 	[EDGE_FR] = "FR",
    171 	[EDGE_FL] = "FL",
    172 	[EDGE_BL] = "BL",
    173 	[EDGE_BR] = "BR"
    174 };
    175 
    176 STATIC const char *movestr[] = {
    177 	[MOVE_U]  = "U",
    178 	[MOVE_U2] = "U2",
    179 	[MOVE_U3] = "U'",
    180 	[MOVE_D]  = "D",
    181 	[MOVE_D2] = "D2",
    182 	[MOVE_D3] = "D'",
    183 	[MOVE_R]  = "R",
    184 	[MOVE_R2] = "R2",
    185 	[MOVE_R3] = "R'",
    186 	[MOVE_L]  = "L",
    187 	[MOVE_L2] = "L2",
    188 	[MOVE_L3] = "L'",
    189 	[MOVE_F]  = "F",
    190 	[MOVE_F2] = "F2",
    191 	[MOVE_F3] = "F'",
    192 	[MOVE_B]  = "B",
    193 	[MOVE_B2] = "B2",
    194 	[MOVE_B3] = "B'",
    195 };
    196 
    197 STATIC const char *transstr[] = {
    198 	[TRANS_UFr] = "rotation UF",
    199 	[TRANS_UFm] = "mirrored UF",
    200 	[TRANS_ULr] = "rotation UL",
    201 	[TRANS_ULm] = "mirrored UL",
    202 	[TRANS_UBr] = "rotation UB",
    203 	[TRANS_UBm] = "mirrored UB",
    204 	[TRANS_URr] = "rotation UR",
    205 	[TRANS_URm] = "mirrored UR",
    206 	[TRANS_DFr] = "rotation DF",
    207 	[TRANS_DFm] = "mirrored DF",
    208 	[TRANS_DLr] = "rotation DL",
    209 	[TRANS_DLm] = "mirrored DL",
    210 	[TRANS_DBr] = "rotation DB",
    211 	[TRANS_DBm] = "mirrored DB",
    212 	[TRANS_DRr] = "rotation DR",
    213 	[TRANS_DRm] = "mirrored DR",
    214 	[TRANS_RUr] = "rotation RU",
    215 	[TRANS_RUm] = "mirrored RU",
    216 	[TRANS_RFr] = "rotation RF",
    217 	[TRANS_RFm] = "mirrored RF",
    218 	[TRANS_RDr] = "rotation RD",
    219 	[TRANS_RDm] = "mirrored RD",
    220 	[TRANS_RBr] = "rotation RB",
    221 	[TRANS_RBm] = "mirrored RB",
    222 	[TRANS_LUr] = "rotation LU",
    223 	[TRANS_LUm] = "mirrored LU",
    224 	[TRANS_LFr] = "rotation LF",
    225 	[TRANS_LFm] = "mirrored LF",
    226 	[TRANS_LDr] = "rotation LD",
    227 	[TRANS_LDm] = "mirrored LD",
    228 	[TRANS_LBr] = "rotation LB",
    229 	[TRANS_LBm] = "mirrored LB",
    230 	[TRANS_FUr] = "rotation FU",
    231 	[TRANS_FUm] = "mirrored FU",
    232 	[TRANS_FRr] = "rotation FR",
    233 	[TRANS_FRm] = "mirrored FR",
    234 	[TRANS_FDr] = "rotation FD",
    235 	[TRANS_FDm] = "mirrored FD",
    236 	[TRANS_FLr] = "rotation FL",
    237 	[TRANS_FLm] = "mirrored FL",
    238 	[TRANS_BUr] = "rotation BU",
    239 	[TRANS_BUm] = "mirrored BU",
    240 	[TRANS_BRr] = "rotation BR",
    241 	[TRANS_BRm] = "mirrored BR",
    242 	[TRANS_BDr] = "rotation BD",
    243 	[TRANS_BDm] = "mirrored BD",
    244 	[TRANS_BLr] = "rotation BL",
    245 	[TRANS_BLm] = "mirrored BL",
    246 };
    247 
    248 static uint8_t inverse_trans_table[48] = {
    249 	[TRANS_UFr] = TRANS_UFr,
    250 	[TRANS_UFm] = TRANS_UFm,
    251 	[TRANS_ULr] = TRANS_URr,
    252 	[TRANS_ULm] = TRANS_ULm,
    253 	[TRANS_UBr] = TRANS_UBr,
    254 	[TRANS_UBm] = TRANS_UBm,
    255 	[TRANS_URr] = TRANS_ULr,
    256 	[TRANS_URm] = TRANS_URm,
    257 	[TRANS_DFr] = TRANS_DFr,
    258 	[TRANS_DFm] = TRANS_DFm,
    259 	[TRANS_DLr] = TRANS_DLr,
    260 	[TRANS_DLm] = TRANS_DRm,
    261 	[TRANS_DBr] = TRANS_DBr,
    262 	[TRANS_DBm] = TRANS_DBm,
    263 	[TRANS_DRr] = TRANS_DRr,
    264 	[TRANS_DRm] = TRANS_DLm,
    265 	[TRANS_RUr] = TRANS_FRr,
    266 	[TRANS_RUm] = TRANS_FLm,
    267 	[TRANS_RFr] = TRANS_LFr,
    268 	[TRANS_RFm] = TRANS_RFm,
    269 	[TRANS_RDr] = TRANS_BLr,
    270 	[TRANS_RDm] = TRANS_BRm,
    271 	[TRANS_RBr] = TRANS_RBr,
    272 	[TRANS_RBm] = TRANS_LBm,
    273 	[TRANS_LUr] = TRANS_FLr,
    274 	[TRANS_LUm] = TRANS_FRm,
    275 	[TRANS_LFr] = TRANS_RFr,
    276 	[TRANS_LFm] = TRANS_LFm,
    277 	[TRANS_LDr] = TRANS_BRr,
    278 	[TRANS_LDm] = TRANS_BLm,
    279 	[TRANS_LBr] = TRANS_LBr,
    280 	[TRANS_LBm] = TRANS_RBm,
    281 	[TRANS_FUr] = TRANS_FUr,
    282 	[TRANS_FUm] = TRANS_FUm,
    283 	[TRANS_FRr] = TRANS_RUr,
    284 	[TRANS_FRm] = TRANS_LUm,
    285 	[TRANS_FDr] = TRANS_BUr,
    286 	[TRANS_FDm] = TRANS_BUm,
    287 	[TRANS_FLr] = TRANS_LUr,
    288 	[TRANS_FLm] = TRANS_RUm,
    289 	[TRANS_BUr] = TRANS_FDr,
    290 	[TRANS_BUm] = TRANS_FDm,
    291 	[TRANS_BRr] = TRANS_LDr,
    292 	[TRANS_BRm] = TRANS_RDm,
    293 	[TRANS_BDr] = TRANS_BDr,
    294 	[TRANS_BDm] = TRANS_BDm,
    295 	[TRANS_BLr] = TRANS_RDr,
    296 	[TRANS_BLm] = TRANS_LDm,
    297 };
    298 
    299 static uint8_t trans_move_table[48][3] = {
    300 	[TRANS_UFr] = { MOVE_U, MOVE_R, MOVE_F },
    301 	[TRANS_UFm] = { MOVE_U, MOVE_L, MOVE_F },
    302 	[TRANS_ULr] = { MOVE_U, MOVE_F, MOVE_L },
    303 	[TRANS_ULm] = { MOVE_U, MOVE_F, MOVE_R },
    304 	[TRANS_UBr] = { MOVE_U, MOVE_L, MOVE_B },
    305 	[TRANS_UBm] = { MOVE_U, MOVE_R, MOVE_B },
    306 	[TRANS_URr] = { MOVE_U, MOVE_B, MOVE_R },
    307 	[TRANS_URm] = { MOVE_U, MOVE_B, MOVE_L },
    308 	[TRANS_DFr] = { MOVE_D, MOVE_L, MOVE_F },
    309 	[TRANS_DFm] = { MOVE_D, MOVE_R, MOVE_F },
    310 	[TRANS_DLr] = { MOVE_D, MOVE_B, MOVE_L },
    311 	[TRANS_DLm] = { MOVE_D, MOVE_B, MOVE_R },
    312 	[TRANS_DBr] = { MOVE_D, MOVE_R, MOVE_B },
    313 	[TRANS_DBm] = { MOVE_D, MOVE_L, MOVE_B },
    314 	[TRANS_DRr] = { MOVE_D, MOVE_F, MOVE_R },
    315 	[TRANS_DRm] = { MOVE_D, MOVE_F, MOVE_L },
    316 	[TRANS_RUr] = { MOVE_R, MOVE_F, MOVE_U },
    317 	[TRANS_RUm] = { MOVE_L, MOVE_F, MOVE_U },
    318 	[TRANS_RFr] = { MOVE_R, MOVE_D, MOVE_F },
    319 	[TRANS_RFm] = { MOVE_L, MOVE_D, MOVE_F },
    320 	[TRANS_RDr] = { MOVE_R, MOVE_B, MOVE_D },
    321 	[TRANS_RDm] = { MOVE_L, MOVE_B, MOVE_D },
    322 	[TRANS_RBr] = { MOVE_R, MOVE_U, MOVE_B },
    323 	[TRANS_RBm] = { MOVE_L, MOVE_U, MOVE_B },
    324 	[TRANS_LUr] = { MOVE_L, MOVE_B, MOVE_U },
    325 	[TRANS_LUm] = { MOVE_R, MOVE_B, MOVE_U },
    326 	[TRANS_LFr] = { MOVE_L, MOVE_U, MOVE_F },
    327 	[TRANS_LFm] = { MOVE_R, MOVE_U, MOVE_F },
    328 	[TRANS_LDr] = { MOVE_L, MOVE_F, MOVE_D },
    329 	[TRANS_LDm] = { MOVE_R, MOVE_F, MOVE_D },
    330 	[TRANS_LBr] = { MOVE_L, MOVE_D, MOVE_B },
    331 	[TRANS_LBm] = { MOVE_R, MOVE_D, MOVE_B },
    332 	[TRANS_FUr] = { MOVE_F, MOVE_L, MOVE_U },
    333 	[TRANS_FUm] = { MOVE_F, MOVE_R, MOVE_U },
    334 	[TRANS_FRr] = { MOVE_F, MOVE_U, MOVE_R },
    335 	[TRANS_FRm] = { MOVE_F, MOVE_U, MOVE_L },
    336 	[TRANS_FDr] = { MOVE_F, MOVE_R, MOVE_D },
    337 	[TRANS_FDm] = { MOVE_F, MOVE_L, MOVE_D },
    338 	[TRANS_FLr] = { MOVE_F, MOVE_D, MOVE_L },
    339 	[TRANS_FLm] = { MOVE_F, MOVE_D, MOVE_R },
    340 	[TRANS_BUr] = { MOVE_B, MOVE_R, MOVE_U },
    341 	[TRANS_BUm] = { MOVE_B, MOVE_L, MOVE_U },
    342 	[TRANS_BRr] = { MOVE_B, MOVE_D, MOVE_R },
    343 	[TRANS_BRm] = { MOVE_B, MOVE_D, MOVE_L },
    344 	[TRANS_BDr] = { MOVE_B, MOVE_L, MOVE_D },
    345 	[TRANS_BDm] = { MOVE_B, MOVE_R, MOVE_D },
    346 	[TRANS_BLr] = { MOVE_B, MOVE_U, MOVE_L },
    347 	[TRANS_BLm] = { MOVE_B, MOVE_U, MOVE_R },
    348 };