diff options
Diffstat (limited to 'old/2020-unknown-date/cubeutils.c')
| -rw-r--r-- | old/2020-unknown-date/cubeutils.c | 100 |
1 files changed, 100 insertions, 0 deletions
diff --git a/old/2020-unknown-date/cubeutils.c b/old/2020-unknown-date/cubeutils.c new file mode 100644 index 0000000..4f0a060 --- /dev/null +++ b/old/2020-unknown-date/cubeutils.c | |||
| @@ -0,0 +1,100 @@ | |||
| 1 | #include <stdio.h> | ||
| 2 | #include "utils.h" | ||
| 3 | #include "pieces.h" | ||
| 4 | #include "cubeutils.h" | ||
| 5 | |||
| 6 | int epe_solved[] = {FR, FL, BL, BR}; | ||
| 7 | int eps_solved[] = {UL, UR, DL, DR}; | ||
| 8 | int epm_solved[] = {UF, UB, DF, DB}; | ||
| 9 | |||
| 10 | void cube_to_ep_array(Cube *cube, int ep[12]) { | ||
| 11 | int epe[4], eps[4], epm[4]; | ||
| 12 | index_to_perm(cube->epose % factorial(4), 4, epe); | ||
| 13 | index_to_perm(cube->eposs % factorial(4), 4, eps); | ||
| 14 | index_to_perm(cube->eposm % factorial(4), 4, epm); | ||
| 15 | |||
| 16 | int epose[12], eposs[12], eposm[12]; | ||
| 17 | index_to_subset(cube->epose / factorial(4), 12, 4, epose); | ||
| 18 | index_to_subset(cube->eposs / factorial(4), 12, 4, eposs); | ||
| 19 | index_to_subset(cube->eposm / factorial(4), 12, 4, eposm); | ||
| 20 | for (int i = 0; i < 4; i++) { | ||
| 21 | swap(&eposs[eps_solved[i]], &eposs[i+8]); | ||
| 22 | swap(&eposm[epm_solved[i]], &eposm[i+8]); | ||
| 23 | } | ||
| 24 | |||
| 25 | for (int i = 0, ie = 0, is = 0, im = 0; i < 12; i++) { | ||
| 26 | if (epose[i]) ep[i] = epe_solved[epe[ie++]]; | ||
| 27 | if (eposs[i]) ep[i] = eps_solved[eps[is++]]; | ||
| 28 | if (eposm[i]) ep[i] = epm_solved[epm[im++]]; | ||
| 29 | } | ||
| 30 | } | ||
| 31 | |||
| 32 | void ep_array_to_epos(int ep[12], Cube *cube) { | ||
| 33 | int epe[4], eps[4], epm[4]; | ||
| 34 | int epose[12] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; | ||
| 35 | int eposs[12] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; | ||
| 36 | int eposm[12] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; | ||
| 37 | for (int i = 0, ie = 0, is = 0, im = 0; i < 12; i++) { | ||
| 38 | for (int j = 0; j < 4; j++) { | ||
| 39 | if (ep[i] == epe_solved[j]) { epe[ie++] = j; epose[i] = 1; } | ||
| 40 | if (ep[i] == eps_solved[j]) { eps[is++] = j; eposs[i] = 1; } | ||
| 41 | if (ep[i] == epm_solved[j]) { epm[im++] = j; eposm[i] = 1; } | ||
| 42 | } | ||
| 43 | } | ||
| 44 | for (int i = 0; i < 4; i++) { | ||
| 45 | swap(&eposs[eps_solved[i]], &eposs[i+8]); | ||
| 46 | swap(&eposm[epm_solved[i]], &eposm[i+8]); | ||
| 47 | } | ||
| 48 | cube->epose = factorial(4) * subset_to_index(epose, 12, 4) | ||
| 49 | + perm_to_index(epe, 4); | ||
| 50 | cube->eposs = factorial(4) * subset_to_index(eposs, 12, 4) | ||
| 51 | + perm_to_index(eps, 4); | ||
| 52 | cube->eposm = factorial(4) * subset_to_index(eposm, 12, 4) | ||
| 53 | + perm_to_index(epm, 4); | ||
| 54 | } | ||
| 55 | |||
| 56 | bool solvable(Cube *cube) { | ||
| 57 | /* Since we memorize orientation truncating the last digit, we only need to | ||
| 58 | * check that the permutations have the correct sign. */ | ||
| 59 | /* TODO: add check that every integer is in range */ | ||
| 60 | int ep[12], cp[12], c[6]; | ||
| 61 | cube_to_ep_array(cube, ep); | ||
| 62 | index_to_perm(cube->cp, 8, cp); | ||
| 63 | index_to_perm(cube->centerpos, 6, c); | ||
| 64 | return (perm_sign(ep, 12) ^ perm_sign(c, 6)) == perm_sign(cp, 8); | ||
| 65 | } | ||
| 66 | |||
| 67 | Cube *solved_cube() { | ||
| 68 | static Cube cube = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; | ||
| 69 | return &cube; | ||
| 70 | } | ||
| 71 | |||
| 72 | bool solved(Cube *cube) { | ||
| 73 | return (cube->eofb == 0 && cube->coud == 0 && cube->cp == 0 && | ||
| 74 | cube->epose == 0 && cube->eposs == 0 && cube->eposm == 0 && | ||
| 75 | cube->centerpos == 0); | ||
| 76 | } | ||
| 77 | |||
| 78 | char *to_string(Cube *cube) { | ||
| 79 | int eo[12], co[8], ep[12], cp[8], cenpos[6]; | ||
| 80 | cube_to_eofb_array(cube->eofb, eo); | ||
| 81 | cube_to_coud_array(cube->coud, co); | ||
| 82 | cube_to_ep_array(cube, ep); | ||
| 83 | cube_to_cp_array(cube->cp, cp); | ||
| 84 | cube_to_centerpos_array(cube->centerpos, cenpos); | ||
| 85 | |||
| 86 | char *ret[1000]; | ||
| 87 | |||
| 88 | for (int i = 0; i < 12; i++) sprintf(ret, " %s ", edge_string[ep[i]]); | ||
| 89 | sprintf(ret, "\n"); | ||
| 90 | for (int i = 0; i < 12; i++) sprintf(ret, " %d ", eo[i]); | ||
| 91 | sprintf(ret, "\n"); | ||
| 92 | for (int i = 0; i < 8; i++) sprintf(ret, "%s ", corner_string[cp[i]]); | ||
| 93 | sprintf(ret, "\n"); | ||
| 94 | for (int i = 0; i < 8; i++) sprintf(ret, " %d ", co[i]); | ||
| 95 | sprintf(ret, "\n"); | ||
| 96 | for (int i = 0; i < 6; i++) sprintf(ret, " %s ", center_string[cenpos[i]]); | ||
| 97 | sprintf(ret, "\n"); | ||
| 98 | |||
| 99 | return ret; | ||
| 100 | } | ||
