aboutsummaryrefslogtreecommitdiff
path: root/old/2020-unknown-date/cubeutils.c
diff options
context:
space:
mode:
authorSebastiano Tronto <sebastiano.tronto@gmail.com>2021-11-11 21:37:34 +0100
committerSebastiano Tronto <sebastiano.tronto@gmail.com>2021-11-11 21:37:34 +0100
commit3568412f8f230774d0d11d7ed1c897424f95d3ef (patch)
tree77223792d8c925a9b1fc32b3f4341e943b5f8209 /old/2020-unknown-date/cubeutils.c
parent67e1b5e6e6a2c917a2fe58a37a1382c982b1e5c5 (diff)
downloadnissy-3568412f8f230774d0d11d7ed1c897424f95d3ef.tar.gz
nissy-3568412f8f230774d0d11d7ed1c897424f95d3ef.zip
Rewritten from scratch. Welocme nissy 2.0!
Diffstat (limited to '')
-rw-r--r--old/2020-unknown-date/cubeutils.c100
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
6int epe_solved[] = {FR, FL, BL, BR};
7int eps_solved[] = {UL, UR, DL, DR};
8int epm_solved[] = {UF, UB, DF, DB};
9
10void 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
32void 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
56bool 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
67Cube *solved_cube() {
68 static Cube cube = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
69 return &cube;
70}
71
72bool 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
78char *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}

Generated with cgit - Back to sebastiano.tronto.net