diff options
Diffstat (limited to 'old/2021-05-26-before-restyle/cube.c')
| -rw-r--r-- | old/2021-05-26-before-restyle/cube.c | 293 |
1 files changed, 0 insertions, 293 deletions
diff --git a/old/2021-05-26-before-restyle/cube.c b/old/2021-05-26-before-restyle/cube.c deleted file mode 100644 index 603c66a..0000000 --- a/old/2021-05-26-before-restyle/cube.c +++ /dev/null | |||
| @@ -1,293 +0,0 @@ | |||
| 1 | #include "cube.h" | ||
| 2 | |||
| 3 | typedef struct { | ||
| 4 | int ep[12],eofb[12],eorl[12],eoud[12],cp[8],coud[8],corl[8],cofb[8],cpos[6]; | ||
| 5 | } CubeArrayAllocated; | ||
| 6 | |||
| 7 | void allocate_cubearray(CubeArray *arr, CubeArrayAllocated *all); | ||
| 8 | |||
| 9 | char edge_string[12][5] = | ||
| 10 | { "UF", "UL", "UB", "UR", "DF", "DL", "DB", "DR", "FR", "FL", "BL", "BR" }; | ||
| 11 | char corner_string[8][5] = { "UFR","UFL","UBL","UBR","DFR","DFL","DBL","DBR" }; | ||
| 12 | char center_string[6][5] = { "U", "D", "R", "L", "F", "B" }; | ||
| 13 | |||
| 14 | int epe_solved[4] = {FR, FL, BL, BR}; | ||
| 15 | int eps_solved[4] = {UL, UR, DL, DR}; | ||
| 16 | int epm_solved[4] = {UF, UB, DF, DB}; | ||
| 17 | |||
| 18 | PieceFilter pf_all = {true,true,true,true,true,true,true,true,true,true,true}, | ||
| 19 | pf_cpos = { .cpos = true }, pf_cp = { .cp = true }, | ||
| 20 | pf_ep = { .epose = true, .eposs = true, .eposm = true }, | ||
| 21 | pf_e = {.epose=true}, pf_s={.eposs=true}, pf_m={.eposm=true}, | ||
| 22 | pf_eo = { .eofb = true, .eorl = true, .eoud = true }, | ||
| 23 | pf_co = { .coud = true, .cofb = true, .corl = true }; | ||
| 24 | |||
| 25 | void allocate_cubearray(CubeArray *arr, CubeArrayAllocated *all) { | ||
| 26 | arr->ep = all->ep; | ||
| 27 | arr->eofb = all->eofb; | ||
| 28 | arr->eorl = all->eorl; | ||
| 29 | arr->eoud = all->eoud; | ||
| 30 | arr->cp = all->cp; | ||
| 31 | arr->coud = all->coud; | ||
| 32 | arr->corl = all->corl; | ||
| 33 | arr->cofb = all->cofb; | ||
| 34 | arr->cpos = all->cpos; | ||
| 35 | } | ||
| 36 | |||
| 37 | void cube_to_arrays(Cube cube, CubeArray *arr, PieceFilter f) { | ||
| 38 | /* ep is the hardest */ | ||
| 39 | if (f.epose || f.eposs || f.eposm) | ||
| 40 | for (int i = 0; i < 12; i++) arr->ep[i] = -1; | ||
| 41 | if (f.epose) { | ||
| 42 | int epe[4], epose[12]; | ||
| 43 | index_to_perm(cube.epose % factorial(4), 4, epe); | ||
| 44 | index_to_subset(cube.epose / factorial(4), 12, 4, epose); | ||
| 45 | for (int i = 0, ie = 0; i < 12; i++) | ||
| 46 | if (epose[i]) arr->ep[i] = epe_solved[epe[ie++]]; | ||
| 47 | } | ||
| 48 | if (f.eposs) { | ||
| 49 | int eps[4], eposs[12]; | ||
| 50 | index_to_perm(cube.eposs % factorial(4), 4, eps); | ||
| 51 | index_to_subset(cube.eposs / factorial(4), 12, 4, eposs); | ||
| 52 | for (int i = 0; i < 4; i++) swap(&eposs[eps_solved[i]], &eposs[i+8]); | ||
| 53 | for (int i = 0, is = 0; i < 12; i++) | ||
| 54 | if (eposs[i]) arr->ep[i] = eps_solved[eps[is++]]; | ||
| 55 | } | ||
| 56 | if (f.eposm) { | ||
| 57 | int epm[4], eposm[12]; | ||
| 58 | index_to_perm(cube.eposm % factorial(4), 4, epm); | ||
| 59 | index_to_subset(cube.eposm / factorial(4), 12, 4, eposm); | ||
| 60 | for (int i = 0; i < 4; i++) swap(&eposm[epm_solved[i]], &eposm[i+8]); | ||
| 61 | for (int i = 0, im = 0; i < 12; i++) | ||
| 62 | if (eposm[i]) arr->ep[i] = epm_solved[epm[im++]]; | ||
| 63 | } | ||
| 64 | |||
| 65 | /* All the others */ | ||
| 66 | if (f.eofb) int_to_sum_zero_array(cube.eofb, 2, 12, arr->eofb); | ||
| 67 | if (f.eorl) int_to_sum_zero_array(cube.eorl, 2, 12, arr->eorl); | ||
| 68 | if (f.eoud) int_to_sum_zero_array(cube.eoud, 2, 12, arr->eoud); | ||
| 69 | if (f.cp) index_to_perm( cube.cp, 8, arr->cp); | ||
| 70 | if (f.coud) int_to_sum_zero_array(cube.coud, 3, 8, arr->coud); | ||
| 71 | if (f.corl) int_to_sum_zero_array(cube.corl, 3, 8, arr->corl); | ||
| 72 | if (f.cofb) int_to_sum_zero_array(cube.cofb, 3, 8, arr->cofb); | ||
| 73 | if (f.cpos) index_to_perm( cube.cpos, 6, arr->cpos); | ||
| 74 | } | ||
| 75 | |||
| 76 | Cube arrays_to_cube(CubeArray arr, PieceFilter f) { | ||
| 77 | Cube ret = {0}; | ||
| 78 | |||
| 79 | /* Again, ep is the hardest part */ | ||
| 80 | if (f.epose) { | ||
| 81 | int epe[4], epose[12] = {0,0,0,0,0,0,0,0,0,0,0,0}; | ||
| 82 | for (int i = 0, ie = 0; i < 12; i++) | ||
| 83 | for (int j = 0; j < 4; j++) | ||
| 84 | if (arr.ep[i] == epe_solved[j]) | ||
| 85 | { epe[ie++] = j; epose[i] = 1; } | ||
| 86 | ret.epose = factorial(4)*subset_to_index(epose,12,4)+perm_to_index(epe,4); | ||
| 87 | } | ||
| 88 | if (f.eposs) { | ||
| 89 | int eps[4], eposs[12] = {0,0,0,0,0,0,0,0,0,0,0,0}; | ||
| 90 | for (int i = 0, is = 0; i < 12; i++) | ||
| 91 | for (int j = 0; j < 4; j++) | ||
| 92 | if (arr.ep[i] == eps_solved[j]) | ||
| 93 | { eps[is++] = j; eposs[i] = 1; } | ||
| 94 | for (int i = 0; i < 4; i++) swap(&eposs[eps_solved[i]], &eposs[i+8]); | ||
| 95 | ret.eposs = factorial(4)*subset_to_index(eposs,12,4)+perm_to_index(eps,4); | ||
| 96 | } | ||
| 97 | if (f.eposm) { | ||
| 98 | int epm[4], eposm[12] = {0,0,0,0,0,0,0,0,0,0,0,0}; | ||
| 99 | for (int i = 0, im = 0; i < 12; i++) | ||
| 100 | for (int j = 0; j < 4; j++) | ||
| 101 | if (arr.ep[i] == epm_solved[j]) | ||
| 102 | { epm[im++] = j; eposm[i] = 1; } | ||
| 103 | for (int i = 0; i < 4; i++) swap(&eposm[epm_solved[i]], &eposm[i+8]); | ||
| 104 | ret.eposm = factorial(4)*subset_to_index(eposm,12,4)+perm_to_index(epm,4); | ||
| 105 | } | ||
| 106 | if (f.eofb) ret.eofb = digit_array_to_int(arr.eofb, 11, 2); | ||
| 107 | if (f.eorl) ret.eorl = digit_array_to_int(arr.eorl, 11, 2); | ||
| 108 | if (f.eoud) ret.eoud = digit_array_to_int(arr.eoud, 11, 2); | ||
| 109 | if (f.cp) ret.cp = perm_to_index( arr.cp, 8 ); | ||
| 110 | if (f.coud) ret.coud = digit_array_to_int(arr.coud, 7, 3); | ||
| 111 | if (f.corl) ret.corl = digit_array_to_int(arr.corl, 7, 3); | ||
| 112 | if (f.cofb) ret.cofb = digit_array_to_int(arr.cofb, 7, 3); | ||
| 113 | if (f.cpos) ret.cpos = perm_to_index( arr.cpos, 6 ); | ||
| 114 | |||
| 115 | return ret; | ||
| 116 | } | ||
| 117 | |||
| 118 | int piece_orientation(Cube cube, int piece, char *orientation) { | ||
| 119 | int arr[12], n, b; | ||
| 120 | uint16_t x; | ||
| 121 | if (!strcmp(orientation, "eofb")) { x = cube.eofb; n = 12; b = 2; } else | ||
| 122 | if (!strcmp(orientation, "eorl")) { x = cube.eorl; n = 12; b = 2; } else | ||
| 123 | if (!strcmp(orientation, "eoud")) { x = cube.eoud; n = 12; b = 2; } else | ||
| 124 | if (!strcmp(orientation, "coud")) { x = cube.coud; n = 8; b = 3; } else | ||
| 125 | if (!strcmp(orientation, "corl")) { x = cube.corl; n = 8; b = 3; } else | ||
| 126 | if (!strcmp(orientation, "cofb")) { x = cube.cofb; n = 8; b = 3; } | ||
| 127 | else return -1; | ||
| 128 | |||
| 129 | int_to_sum_zero_array(x, b, n, arr); | ||
| 130 | if (piece < n) | ||
| 131 | return arr[piece]; | ||
| 132 | return -1; | ||
| 133 | } | ||
| 134 | |||
| 135 | Center center_at(Cube cube, Center c) { | ||
| 136 | static CubeArrayAllocated all = {0}; | ||
| 137 | CubeArray arr = {0}; | ||
| 138 | allocate_cubearray(&arr, &all); | ||
| 139 | cube_to_arrays(cube, &arr, pf_cpos); | ||
| 140 | return arr.cpos[c]; | ||
| 141 | } | ||
| 142 | |||
| 143 | Edge edge_at(Cube cube, Edge e) { | ||
| 144 | static CubeArrayAllocated all = {0}; | ||
| 145 | CubeArray arr = {0}; | ||
| 146 | allocate_cubearray(&arr, &all); | ||
| 147 | cube_to_arrays(cube, &arr, pf_ep); | ||
| 148 | return arr.ep[e]; | ||
| 149 | } | ||
| 150 | |||
| 151 | Corner corner_at(Cube cube, Corner c) { | ||
| 152 | static CubeArrayAllocated all = {0}; | ||
| 153 | CubeArray arr = {0}; | ||
| 154 | allocate_cubearray(&arr, &all); | ||
| 155 | cube_to_arrays(cube, &arr, pf_cp); | ||
| 156 | return arr.cp[c]; | ||
| 157 | } | ||
| 158 | |||
| 159 | bool block_solved(Cube cube, Block block) { | ||
| 160 | static CubeArrayAllocated all = {0}; | ||
| 161 | CubeArray arr = {0}; | ||
| 162 | allocate_cubearray(&arr, &all); | ||
| 163 | cube_to_arrays(cube, &arr, pf_all); | ||
| 164 | |||
| 165 | bool ret = true; | ||
| 166 | |||
| 167 | for (int i = 0; i < 12; i++) | ||
| 168 | ret = ret && !(block.edge[i] && (arr.ep[i] != i || arr.eofb[i])); | ||
| 169 | for (int i = 0; i < 8; i++) | ||
| 170 | ret = ret && !(block.corner[i] && (arr.cp[i] != i || arr.coud[i])); | ||
| 171 | for (int i = 0; i < 6; i++) | ||
| 172 | ret = ret && !(block.center[i] && arr.cpos[i] != i); | ||
| 173 | |||
| 174 | return ret; | ||
| 175 | } | ||
| 176 | |||
| 177 | bool equal(Cube c1, Cube c2) { | ||
| 178 | return c1.eofb == c2.eofb && c1.epose == c2.epose && | ||
| 179 | c1.eposs == c2.eposs && c1.eposm == c2.eposm && | ||
| 180 | c1.coud == c2.coud && c1.cp == c2.cp && | ||
| 181 | c1.cpos == c2.cpos; | ||
| 182 | } | ||
| 183 | |||
| 184 | bool is_solved(Cube cube) { | ||
| 185 | /* TODO: might return true if cube is not solvable but looks solved form one | ||
| 186 | of the incompatible interpretations (e.g. eofb and ep solved, but | ||
| 187 | eorl not solve) */ | ||
| 188 | return !cube.eofb && !cube.coud && !cube.cp && | ||
| 189 | !cube.epose && !cube.eposs && !cube.eposm && !cube.cpos; | ||
| 190 | } | ||
| 191 | |||
| 192 | void print_cube(Cube cube) { | ||
| 193 | static CubeArrayAllocated all = {0}; | ||
| 194 | CubeArray arrx = {0}; | ||
| 195 | allocate_cubearray(&arrx, &all); | ||
| 196 | cube_to_arrays(cube, &arrx, pf_all); | ||
| 197 | |||
| 198 | for (int i = 0; i < 12; i++) printf(" %s ", edge_string[arrx.ep[i]]); | ||
| 199 | printf("\n"); | ||
| 200 | for (int i = 0; i < 12; i++) printf(" %c ", arrx.eofb[i] + '0'); | ||
| 201 | printf("\n"); | ||
| 202 | for (int i = 0; i < 8; i++) printf("%s ", corner_string[arrx.cp[i]]); | ||
| 203 | printf("\n"); | ||
| 204 | for (int i = 0; i < 8; i++) printf(" %c ", arrx.coud[i] + '0'); | ||
| 205 | printf("\n"); | ||
| 206 | for (int i = 0; i < 6; i++) printf(" %s ", center_string[arrx.cpos[i]]); | ||
| 207 | printf("\n"); | ||
| 208 | } | ||
| 209 | |||
| 210 | Cube admissible_ep(Cube cube, PieceFilter f) { | ||
| 211 | static CubeArrayAllocated all = {0}; | ||
| 212 | CubeArray arrx = {0}; | ||
| 213 | allocate_cubearray(&arrx, &all); | ||
| 214 | cube_to_arrays(cube, &arrx, f); | ||
| 215 | |||
| 216 | bool used[12] = {0}; | ||
| 217 | for (int i = 0; i < 12; i++) | ||
| 218 | if (arrx.ep[i] != -1) | ||
| 219 | used[arrx.ep[i]] = true; | ||
| 220 | for (int i = 0, j = 0; i < 12; i++) { | ||
| 221 | while (j < 11 && used[j]) j++; | ||
| 222 | if (arrx.ep[i] == -1) | ||
| 223 | arrx.ep[i] = j++; | ||
| 224 | } | ||
| 225 | |||
| 226 | return arrays_to_cube(arrx, pf_ep); | ||
| 227 | } | ||
| 228 | |||
| 229 | Cube inverse_cube(Cube cube) { | ||
| 230 | static CubeArrayAllocated all = {0}, invall = {0}; | ||
| 231 | CubeArray arrx = {0}, invx = {0}; | ||
| 232 | allocate_cubearray(&arrx, &all); | ||
| 233 | allocate_cubearray(&invx, &invall); | ||
| 234 | |||
| 235 | cube_to_arrays(cube, &arrx, pf_all); | ||
| 236 | |||
| 237 | for (int i = 0; i < 12; i++) { | ||
| 238 | invx.ep[arrx.ep[i]] = i; | ||
| 239 | invx.eofb[arrx.ep[i]] = arrx.eofb[i]; | ||
| 240 | invx.eorl[arrx.ep[i]] = arrx.eorl[i]; | ||
| 241 | invx.eoud[arrx.ep[i]] = arrx.eoud[i]; | ||
| 242 | } | ||
| 243 | for (int i = 0; i < 8; i++) { | ||
| 244 | invx.cp[arrx.cp[i]] = i; | ||
| 245 | invx.coud[arrx.cp[i]] = (3 - arrx.coud[i])%3; | ||
| 246 | invx.corl[arrx.cp[i]] = (3 - arrx.corl[i])%3; | ||
| 247 | invx.cofb[arrx.cp[i]] = (3 - arrx.cofb[i])%3; | ||
| 248 | } | ||
| 249 | for (int i = 0; i < 6; i++) | ||
| 250 | invx.cpos[arrx.cpos[i]] = i; | ||
| 251 | |||
| 252 | return arrays_to_cube(invx, pf_all); | ||
| 253 | } | ||
| 254 | |||
| 255 | Cube move_via_arrays(CubeArray arr, Cube c, PieceFilter f) { | ||
| 256 | static CubeArrayAllocated all = {0}; | ||
| 257 | CubeArray arrx = {0}; | ||
| 258 | allocate_cubearray(&arrx, &all); | ||
| 259 | |||
| 260 | cube_to_arrays(c, &arrx, f); | ||
| 261 | |||
| 262 | if (f.epose || f.eposs || f.eposm) | ||
| 263 | apply_permutation( arr.ep, arrx.ep, 12 ); | ||
| 264 | if (f.eofb) { apply_permutation( arr.ep, arrx.eofb, 12 ); | ||
| 265 | sum_arrays_mod( arr.eofb, arrx.eofb, 12, 2 ); } | ||
| 266 | if (f.eorl) { apply_permutation( arr.ep, arrx.eorl, 12 ); | ||
| 267 | sum_arrays_mod( arr.eorl, arrx.eorl, 12, 2 ); } | ||
| 268 | if (f.eoud) { apply_permutation( arr.ep, arrx.eoud, 12 ); | ||
| 269 | sum_arrays_mod( arr.eoud, arrx.eoud, 12, 2 ); } | ||
| 270 | if (f.cp) apply_permutation( arr.cp, arrx.cp, 8 ); | ||
| 271 | if (f.coud) { apply_permutation( arr.cp, arrx.coud, 8 ); | ||
| 272 | sum_arrays_mod( arr.coud, arrx.coud, 8, 3 ); } | ||
| 273 | if (f.corl) { apply_permutation( arr.cp, arrx.corl, 8 ); | ||
| 274 | sum_arrays_mod( arr.corl, arrx.corl, 8, 3 ); } | ||
| 275 | if (f.cofb) { apply_permutation( arr.cp, arrx.cofb, 8 ); | ||
| 276 | sum_arrays_mod( arr.cofb, arrx.cofb, 8, 3 ); } | ||
| 277 | if (f.cpos) apply_permutation( arr.cpos, arrx.cpos, 6 ); | ||
| 278 | |||
| 279 | return arrays_to_cube(arrx, f); | ||
| 280 | } | ||
| 281 | |||
| 282 | Cube compose_filtered(Cube c2, Cube c1, PieceFilter f) { | ||
| 283 | static CubeArrayAllocated all = {0}; | ||
| 284 | CubeArray arrx = {0}; | ||
| 285 | allocate_cubearray(&arrx, &all); | ||
| 286 | |||
| 287 | cube_to_arrays(c2, &arrx, f); | ||
| 288 | return move_via_arrays(arrx, c1, f); | ||
| 289 | } | ||
| 290 | |||
| 291 | Cube compose(Cube c2, Cube c1) { | ||
| 292 | return compose_filtered(c2, c1, pf_all); | ||
| 293 | } | ||
