diff options
Diffstat (limited to 'old/2021-02-18-piecefilter/src/cube.c')
| -rw-r--r-- | old/2021-02-18-piecefilter/src/cube.c | 198 |
1 files changed, 0 insertions, 198 deletions
diff --git a/old/2021-02-18-piecefilter/src/cube.c b/old/2021-02-18-piecefilter/src/cube.c deleted file mode 100644 index 9235c03..0000000 --- a/old/2021-02-18-piecefilter/src/cube.c +++ /dev/null | |||
| @@ -1,198 +0,0 @@ | |||
| 1 | #include "cube.h" | ||
| 2 | |||
| 3 | char edge_string[12][5] = | ||
| 4 | { "UF", "UL", "UB", "UR", "DF", "DL", "DB", "DR", "FR", "FL", "BL", "BR" }; | ||
| 5 | char corner_string[8][5] = { "UFR","UFL","UBL","UBR","DFR","DFL","DBL","DBR" }; | ||
| 6 | char center_string[6][5] = { "U", "D", "R", "L", "F", "B" }; | ||
| 7 | |||
| 8 | int epe_solved[] = {FR, FL, BL, BR}; | ||
| 9 | int eps_solved[] = {UL, UR, DL, DR}; | ||
| 10 | int epm_solved[] = {UF, UB, DF, DB}; | ||
| 11 | |||
| 12 | PieceFilter fAll = {true,true,true,true,true,true,true,true,true,true,true}; | ||
| 13 | |||
| 14 | Cube blank_cube() { | ||
| 15 | Cube c = {0}; | ||
| 16 | return c; | ||
| 17 | } | ||
| 18 | |||
| 19 | /* Return axis (ud=0, rl=1, fb=0) of center c */ | ||
| 20 | int center_axis(int c) { | ||
| 21 | if (c == U_center || c == D_center) | ||
| 22 | return 0; | ||
| 23 | if (c == R_center || c == L_center) | ||
| 24 | return 1; | ||
| 25 | return 2; | ||
| 26 | } | ||
| 27 | |||
| 28 | /* Return slice (e=0, s=1, m=2) to which e belongs */ | ||
| 29 | int edge_slice(int e) { | ||
| 30 | if (e == FR || e == FL || e == BL || e == BR) | ||
| 31 | return 0; | ||
| 32 | if (e == UR || e == UL || e == DR || e == DL) | ||
| 33 | return 1; | ||
| 34 | return 2; | ||
| 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 | bool equal(Cube c1, Cube c2) { | ||
| 119 | return c1.eofb == c2.eofb && c1.epose == c2.epose && | ||
| 120 | c1.eposs == c2.eposs && c1.eposm == c2.eposm && | ||
| 121 | c1.coud == c2.coud && c1.cp == c2.cp && | ||
| 122 | c1.cpos == c2.cpos; | ||
| 123 | } | ||
| 124 | |||
| 125 | bool solvable(Cube cube) { | ||
| 126 | /* Since we memorize orientation truncating the last digit, we only need to | ||
| 127 | * check that the permutations have the correct sign. */ | ||
| 128 | CubeArray arr = {0}; | ||
| 129 | cube_to_arrays(cube, &arr, fAll); | ||
| 130 | return (perm_sign(arr.ep,12) ^ perm_sign(arr.cpos,6)) == perm_sign(arr.cp,8); | ||
| 131 | } | ||
| 132 | |||
| 133 | bool is_solved(Cube cube) { | ||
| 134 | return !cube.eofb && !cube.coud && !cube.cp && | ||
| 135 | !cube.epose && !cube.eposs && !cube.eposm && cube.cpos; | ||
| 136 | } | ||
| 137 | |||
| 138 | void print_cube(Cube cube) { | ||
| 139 | CubeArray arr = {0}; | ||
| 140 | cube_to_arrays(cube, &arr, fAll); | ||
| 141 | |||
| 142 | for (int i = 0; i < 12; i++) printf(" %s ", edge_string[arr.ep[i]]); | ||
| 143 | printf("\n"); | ||
| 144 | for (int i = 0; i < 12; i++) printf(" %c ", arr.eofb[i] + '0'); | ||
| 145 | printf("\n"); | ||
| 146 | for (int i = 0; i < 8; i++) printf("%s ", corner_string[arr.cp[i]]); | ||
| 147 | printf("\n"); | ||
| 148 | for (int i = 0; i < 8; i++) printf(" %c ", arr.coud[i] + '0'); | ||
| 149 | printf("\n"); | ||
| 150 | for (int i = 0; i < 6; i++) printf(" %s ", center_string[arr.cpos[i]]); | ||
| 151 | printf("\n"); | ||
| 152 | } | ||
| 153 | |||
| 154 | Cube inverse_cube(Cube cube) { | ||
| 155 | CubeArray arr = {0}, inv = {0}; | ||
| 156 | cube_to_arrays(cube, &arr, fAll); | ||
| 157 | |||
| 158 | for (int i = 0; i < 12; i++) { | ||
| 159 | inv.ep[arr.ep[i]] = i; | ||
| 160 | inv.eofb[arr.ep[i]] = arr.eofb[i]; | ||
| 161 | inv.eorl[arr.ep[i]] = arr.eorl[i]; | ||
| 162 | inv.eoud[arr.ep[i]] = arr.eoud[i]; | ||
| 163 | } | ||
| 164 | for (int i = 0; i < 8; i++) { | ||
| 165 | inv.cp[arr.cp[i]] = i; | ||
| 166 | inv.coud[arr.cp[i]] = arr.coud[i]; | ||
| 167 | inv.corl[arr.cp[i]] = arr.corl[i]; | ||
| 168 | inv.cofb[arr.cp[i]] = arr.cofb[i]; | ||
| 169 | } | ||
| 170 | for (int i = 0; i < 6; i++) | ||
| 171 | inv.cpos[arr.cpos[i]] = i; | ||
| 172 | |||
| 173 | return arrays_to_cube(inv, fAll); | ||
| 174 | } | ||
| 175 | |||
| 176 | Cube compose_via_arrays(CubeArray arr2, Cube c1, PieceFilter f) { | ||
| 177 | /* This is basically the same as the move_cubearray function above */ | ||
| 178 | CubeArray arr1 = {0}; | ||
| 179 | cube_to_arrays(c1, &arr1, fAll); | ||
| 180 | |||
| 181 | apply_permutation(arr2.ep, arr1.ep, 12); | ||
| 182 | apply_permutation(arr2.ep, arr1.eofb, 12); | ||
| 183 | apply_permutation(arr2.ep, arr1.eorl, 12); | ||
| 184 | apply_permutation(arr2.ep, arr1.eoud, 12); | ||
| 185 | sum_arrays_mod(arr2.eofb, arr1.eofb, 12, 2); | ||
| 186 | sum_arrays_mod(arr2.eorl, arr1.eorl, 12, 2); | ||
| 187 | sum_arrays_mod(arr2.eoud, arr1.eoud, 12, 2); | ||
| 188 | apply_permutation(arr2.cp, arr1.cp, 8); | ||
| 189 | apply_permutation(arr2.cp, arr1.coud, 8); | ||
| 190 | apply_permutation(arr2.cp, arr1.corl, 8); | ||
| 191 | apply_permutation(arr2.cp, arr1.cofb, 8); | ||
| 192 | sum_arrays_mod(arr2.coud, arr1.coud, 8, 3); | ||
| 193 | sum_arrays_mod(arr2.corl, arr1.corl, 8, 3); | ||
| 194 | sum_arrays_mod(arr2.cofb, arr1.cofb, 8, 3); | ||
| 195 | apply_permutation(arr2.cpos, arr1.cpos, 6); | ||
| 196 | |||
| 197 | return arrays_to_cube(arr1, fAll); | ||
| 198 | } | ||
