aboutsummaryrefslogtreecommitdiff
path: root/old/2021-11-10-beforeremovingchecker/coord.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/2021-11-10-beforeremovingchecker/coord.c
parent67e1b5e6e6a2c917a2fe58a37a1382c982b1e5c5 (diff)
downloadnissy-3568412f8f230774d0d11d7ed1c897424f95d3ef.tar.gz
nissy-3568412f8f230774d0d11d7ed1c897424f95d3ef.zip
Rewritten from scratch. Welocme nissy 2.0!
Diffstat (limited to '')
-rw-r--r--old/2021-11-10-beforeremovingchecker/coord.c629
1 files changed, 629 insertions, 0 deletions
diff --git a/old/2021-11-10-beforeremovingchecker/coord.c b/old/2021-11-10-beforeremovingchecker/coord.c
new file mode 100644
index 0000000..343dfb3
--- /dev/null
+++ b/old/2021-11-10-beforeremovingchecker/coord.c
@@ -0,0 +1,629 @@
1#include "coord.h"
2
3static Cube antindex_eofb(uint64_t ind);
4static Cube antindex_eofbepos(uint64_t ind);
5static Cube antindex_epud(uint64_t ind);
6static Cube antindex_coud(uint64_t ind);
7static Cube antindex_corners(uint64_t ind);
8static Cube antindex_cp(uint64_t ind);
9static Cube antindex_cphtr(uint64_t);
10static Cube antindex_cornershtr(uint64_t ind);
11static Cube antindex_cornershtrfin(uint64_t ind);
12static Cube antindex_drud(uint64_t ind);
13static Cube antindex_drud_eofb(uint64_t ind);
14static Cube antindex_htr_drud(uint64_t ind);
15static Cube antindex_htrfin(uint64_t ind);
16
17static uint64_t index_eofb(Cube cube);
18static uint64_t index_eofbepos(Cube cube);
19static uint64_t index_epud(Cube cube);
20static uint64_t index_coud(Cube cube);
21static uint64_t index_corners(Cube cube);
22static uint64_t index_cp(Cube cube);
23static uint64_t index_cphtr(Cube cube);
24static uint64_t index_cornershtr(Cube cube);
25static uint64_t index_cornershtrfin(Cube cube);
26static uint64_t index_drud(Cube cube);
27static uint64_t index_drud_eofb(Cube cube);
28static uint64_t index_htr_drud(Cube cube);
29static uint64_t index_htrfin(Cube cube);
30
31static void init_cphtr_cosets();
32static void init_cphtr_left_cosets_bfs(int i, int c);
33static void init_cphtr_right_cosets_color(int i, int c);
34static void init_cornershtrfin();
35
36
37/* All sorts of useful costants and tables **********************************/
38
39static int cphtr_left_cosets[FACTORIAL8];
40static int cphtr_right_cosets[FACTORIAL8];
41static int cphtr_right_rep[BINOM8ON4*6];
42static int cornershtrfin_ind[FACTORIAL8];
43static int cornershtrfin_ant[24*24/6];
44
45/* Coordinates and their implementation **************************************/
46
47Coordinate
48coord_eofb = {
49 .index = index_eofb,
50 .cube = antindex_eofb,
51 .check = check_eofb,
52 .max = POW2TO11,
53 .ntrans = 1,
54};
55
56Coordinate
57coord_eofbepos = {
58 .index = index_eofbepos,
59 .cube = antindex_eofbepos,
60 .check = check_eofbepos,
61 .max = POW2TO11 * BINOM12ON4,
62 .ntrans = 1,
63};
64
65Coordinate
66coord_coud = {
67 .index = index_coud,
68 .cube = antindex_coud,
69 .check = check_coud,
70 .max = POW3TO7,
71 .ntrans = 1,
72};
73
74Coordinate
75coord_corners = {
76 .index = index_corners,
77 .cube = antindex_corners,
78 .check = check_corners,
79 .max = POW3TO7 * FACTORIAL8,
80 .ntrans = 1,
81};
82
83Coordinate
84coord_cp = {
85 .index = index_cp,
86 .cube = antindex_cp,
87 .check = check_cp,
88 .max = FACTORIAL8,
89 .ntrans = 1,
90};
91
92Coordinate
93coord_cphtr = {
94 .index = index_cphtr,
95 .cube = antindex_cphtr,
96 .check = check_cphtr,
97 .max = BINOM8ON4 * 6,
98 .ntrans = 1,
99};
100
101Coordinate
102coord_cornershtr = {
103 .index = index_cornershtr,
104 .cube = antindex_cornershtr,
105 .check = check_cornershtr,
106 .max = POW3TO7 * BINOM8ON4 * 6,
107 .ntrans = 1,
108};
109
110Coordinate
111coord_cornershtrfin = {
112 .index = index_cornershtrfin,
113 .cube = antindex_cornershtrfin,
114 .check = check_cp,
115 .max = 24*24/6,
116 .ntrans = 1,
117};
118
119Coordinate
120coord_epud = {
121 .index = index_epud,
122 .cube = antindex_epud,
123 .check = check_epud,
124 .max = FACTORIAL8,
125 .ntrans = 1,
126};
127
128Coordinate
129coord_drud = {
130 .index = index_drud,
131 .cube = antindex_drud,
132 .check = check_drud,
133 .max = POW2TO11 * POW3TO7 * BINOM12ON4,
134 .ntrans = 1,
135};
136
137Coordinate
138coord_htr_drud = {
139 .index = index_htr_drud,
140 .cube = antindex_htr_drud,
141 .check = check_drud,
142 .max = BINOM8ON4 * 6 * BINOM8ON4,
143 .ntrans = 1,
144};
145
146Coordinate
147coord_htrfin = {
148 .index = index_htrfin,
149 .cube = antindex_htrfin,
150 .check = check_htr,
151 .max = 24 * 24 * 24 *24 * 24 / 6, /* should be /12 but it's ok */
152 .ntrans = 1,
153};
154
155Coordinate
156coord_drud_eofb = {
157 .index = index_drud_eofb,
158 .cube = antindex_drud_eofb,
159 .check = check_drud,
160 .max = POW3TO7 * BINOM12ON4,
161 .ntrans = 1,
162};
163
164/* Functions *****************************************************************/
165
166static Cube
167antindex_eofb(uint64_t ind)
168{
169 return (Cube){ .eofb = ind, .eorl = ind, .eoud = ind };
170}
171
172static Cube
173antindex_eofbepos(uint64_t ind)
174{
175 Cube ret = {0};
176
177 ret.eofb = ind % POW2TO11;
178 ret.epose = (ind / POW2TO11) * 24;
179
180 return ret;
181}
182
183static Cube
184antindex_epud(uint64_t ind)
185{
186 static bool initialized = false;
187 static Cube epud_aux[FACTORIAL8];
188 int a[12];
189 uint64_t ui;
190 CubeArray arr;
191
192 if (!initialized) {
193 a[FR] = FR;
194 a[FL] = FL;
195 a[BL] = BL;
196 a[BR] = BR;
197 for (ui = 0; ui < FACTORIAL8; ui++) {
198 index_to_perm(ui, 8, a);
199 arr.ep = a;
200 epud_aux[ui] = arrays_to_cube(&arr, pf_ep);
201 }
202
203 initialized = true;
204 }
205
206 return epud_aux[ind];
207}
208
209static Cube
210antindex_coud(uint64_t ind)
211{
212 return (Cube){ .coud = ind, .corl = ind, .cofb = ind };
213}
214
215static Cube
216antindex_corners(uint64_t ind)
217{
218 Cube c = {0};
219
220 c.coud = ind / FACTORIAL8;
221 c.cp = ind % FACTORIAL8;
222
223 return c;
224}
225
226static Cube
227antindex_cp(uint64_t ind)
228{
229 Cube c = {0};
230
231 c.cp = ind;
232
233 return c;
234}
235
236static Cube
237antindex_cphtr(uint64_t ind)
238{
239 return (Cube) { .cp = cphtr_right_rep[ind] };
240}
241
242static Cube
243antindex_cornershtr(uint64_t ind)
244{
245 Cube c = antindex_cphtr(ind % (BINOM8ON4 * 6));
246
247 c.coud = ind / (BINOM8ON4 * 6);
248
249 return c;
250}
251
252static Cube
253antindex_cornershtrfin(uint64_t ind)
254{
255 return (Cube){ .cp = cornershtrfin_ant[ind] };
256}
257
258static Cube
259antindex_drud(uint64_t ind)
260{
261 uint64_t epos, eofb;
262 Cube c;
263
264 eofb = ind % POW2TO11;
265 epos = ind / (POW2TO11 * POW3TO7);
266 c = antindex_eofbepos(eofb + POW2TO11 * epos);
267
268 c.coud = (ind / POW2TO11) % POW3TO7;
269
270 return c;
271}
272
273static Cube
274antindex_drud_eofb(uint64_t ind)
275{
276 return antindex_drud(ind * POW2TO11);
277}
278
279static Cube
280antindex_htr_drud(uint64_t ind)
281{
282 Cube ret;
283
284 ret = antindex_cphtr(ind / BINOM8ON4);
285 ret.eposs = (ind % BINOM8ON4) * FACTORIAL4;
286
287 return ret;
288}
289
290static Cube
291antindex_htrfin(uint64_t ind)
292{
293 Cube ret;
294
295 ret = antindex_cornershtrfin(ind/(24*24*24));
296
297 ret.eposm = ind % 24;
298 ind /= 24;
299 ret.eposs = ind % 24;
300 ind /= 24;
301 ret.epose = ind % 24;
302
303 return ret;
304}
305
306bool
307check_centers(Cube cube)
308{
309 return cube.cpos == 0;
310}
311
312bool
313check_corners(Cube cube)
314{
315 return cube.cp == 0 && cube.coud == 0;
316}
317
318bool
319check_cp(Cube cube)
320{
321 return cube.cp == 0;
322}
323
324bool
325check_cphtr(Cube cube)
326{
327 return index_cphtr(cube) == 0;
328}
329
330bool
331check_cornershtr(Cube cube)
332{
333 return cube.coud == 0 && index_cphtr(cube) == 0;
334}
335
336bool
337check_coud(Cube cube)
338{
339 return cube.coud == 0;
340}
341
342bool
343check_drud(Cube cube)
344{
345 return cube.eofb == 0 && cube.eorl == 0 && cube.coud == 0;
346}
347
348bool
349check_htr(Cube cube)
350{
351 return check_cornershtr(cube) &&
352 cube.eofb == 0 && cube.eorl == 0 && cube.eoud == 0;
353}
354
355bool
356check_drudfin_noE(Cube cube)
357{
358 return cube.eposs == 0 && cube.eposm == 0 && cube.cp == 0;
359}
360
361bool
362check_eofb(Cube cube)
363{
364 return cube.eofb == 0;
365}
366
367bool
368check_eofbepos(Cube cube)
369{
370 return cube.eofb == 0 && cube.epose / 24 == 0;
371}
372
373bool
374check_epose(Cube cube)
375{
376 return cube.epose == 0;
377}
378
379bool
380check_epud(Cube cube)
381{
382 return cube.eposs == 0 && cube.eposm == 0;
383}
384
385bool
386check_ep(Cube cube)
387{
388 return cube.epose == 0 && cube.eposs == 0 && cube.eposm == 0;
389}
390
391bool
392check_khuge(Cube cube)
393{
394 return check_drud(cube) && cube.epose % 24 == 0;
395}
396
397bool
398check_nothing(Cube cube)
399{
400 return is_admissible(cube); /*TODO: maybe change?*/
401}
402
403static uint64_t
404index_eofb(Cube cube)
405{
406 return cube.eofb;
407}
408
409static uint64_t
410index_eofbepos(Cube cube)
411{
412 return (cube.epose / FACTORIAL4) * POW2TO11 + cube.eofb;
413}
414
415static uint64_t
416index_epud(Cube cube)
417{
418 uint64_t ret;
419 CubeArray *arr = new_cubearray(cube, pf_ep);
420
421 ret = perm_to_index(arr->ep, 8);
422 free_cubearray(arr, pf_ep);
423
424 return ret;
425}
426
427static uint64_t
428index_coud(Cube cube)
429{
430 return cube.coud;
431}
432
433static uint64_t
434index_corners(Cube cube)
435{
436 return cube.coud * FACTORIAL8 + cube.cp;
437}
438
439static uint64_t
440index_cp(Cube cube)
441{
442 return cube.cp;
443}
444
445static uint64_t
446index_cphtr(Cube cube)
447{
448 return cphtr_right_cosets[cube.cp];
449}
450
451static uint64_t
452index_cornershtr(Cube cube)
453{
454 return cube.coud * BINOM8ON4 * 6 + index_cphtr(cube);
455}
456
457static uint64_t
458index_cornershtrfin(Cube cube)
459{
460 return cornershtrfin_ind[cube.cp];
461}
462
463static uint64_t
464index_drud(Cube cube)
465{
466 uint64_t a, b, c;
467
468 a = cube.eofb;
469 b = cube.coud;
470 c = cube.epose / FACTORIAL4;
471
472 b *= POW2TO11;
473 c *= POW2TO11 * POW3TO7;
474
475 return a + b + c;
476}
477
478static uint64_t
479index_drud_eofb(Cube cube)
480{
481 return index_drud(cube) / POW2TO11;
482}
483
484static uint64_t
485index_htr_drud(Cube cube)
486{
487 return index_cphtr(cube) * BINOM8ON4 +
488 (cube.eposs / FACTORIAL4) % BINOM8ON4;
489}
490
491static uint64_t
492index_htrfin(Cube cube)
493{
494 uint64_t epe, eps, epm, cp, ep;
495
496 epe = cube.epose % 24;
497 eps = cube.eposs % 24;
498 epm = cube.eposm % 24;
499 ep = (epe * 24 + eps) *24 + epm;
500 cp = index_cornershtrfin(cube);
501
502 return cp * 24 * 24 * 24 + ep;
503}
504
505/* Init functions implementation *********************************************/
506
507/*
508 * There is certainly a better way to do this, but for now I just use
509 * a "graph coloring" algorithm to compute the left cosets, and I compose
510 * with every possible cp to get the right cosets (it is possible that I am
511 * mixing up left and right).
512 *
513 * For doing it better "Mathematically", we need 3 things:
514 * - Checking that cp separates the orbits (UFR,UBL,DFL,DBR) and the other
515 * This is easy and it is done in the commented function cphtr_cp().
516 * - Check that there is no ep/cp parity
517 * - Check that we are not in the "3c" case; this is the part I don't
518 * know how to do.
519 */
520static void
521init_cphtr_cosets()
522{
523 unsigned int i;
524 int c = 0, d = 0;
525
526 for (i = 0; i < FACTORIAL8; i++) {
527 cphtr_left_cosets[i] = -1;
528 cphtr_right_cosets[i] = -1;
529 }
530
531 /* First we compute left cosets with a bfs */
532 for (i = 0; i < FACTORIAL8; i++)
533 if (cphtr_left_cosets[i] == -1)
534 init_cphtr_left_cosets_bfs(i, c++);
535
536 /* Then we compute right cosets using compose() */
537 for (i = 0; i < FACTORIAL8; i++)
538 if (cphtr_right_cosets[i] == -1)
539 init_cphtr_right_cosets_color(i, d++);
540}
541
542static void
543init_cphtr_left_cosets_bfs(int i, int c)
544{
545 int j, jj, k, next[FACTORIAL8], next2[FACTORIAL8], n, n2;
546 Move moves[6] = {U2, D2, R2, L2, F2, B2};
547
548 n = 1;
549 next[0] = i;
550 cphtr_left_cosets[i] = c;
551
552 while (n != 0) {
553 for (j = 0, n2 = 0; j < n; j++) {
554 for (k = 0; k < 6; k++) {
555 /*jj = cp_mtable[moves[k]][next[j]];*/
556 /* TODO fix formatting */
557 jj = apply_move(moves[k], (Cube){.cp=next[j]}).cp;
558 if (cphtr_left_cosets[jj] == -1) {
559 cphtr_left_cosets[jj] = c;
560 next2[n2++] = jj;
561 }
562 }
563 }
564
565 for (j = 0; j < n2; j++)
566 next[j] = next2[j];
567 n = n2;
568 }
569}
570
571static void
572init_cphtr_right_cosets_color(int i, int d)
573{
574 int cp;
575 unsigned int j;
576
577 cphtr_right_rep[d] = i;
578 for (j = 0; j < FACTORIAL8; j++) {
579 if (cphtr_left_cosets[j] == 0) {
580 /* TODO: use antindexer, it's nicer */
581 cp = compose((Cube){.cp = i}, (Cube){.cp = j}).cp;
582 cphtr_right_cosets[cp] = d;
583 }
584 }
585}
586
587static void
588init_cornershtrfin()
589{
590 unsigned int i, j;
591 int n, c;
592 Move m;
593
594 for (i = 0; i < FACTORIAL8; i++)
595 cornershtrfin_ind[i] = -1;
596 cornershtrfin_ind[0] = 0;
597
598 /* 10-pass, I think 5 is enough, but just in case */
599 n = 1;
600 for (i = 0; i < 10; i++) {
601 for (j = 0; j < FACTORIAL8; j++) {
602 if (cornershtrfin_ind[j] == -1)
603 continue;
604 for (m = U; m < NMOVES; m++) {
605 if (moveset_htr(m)) {
606 c = apply_move(m, (Cube){.cp = j}).cp;
607 if (cornershtrfin_ind[c] == -1) {
608 cornershtrfin_ind[c] = n;
609 cornershtrfin_ant[n] = c;
610 n++;
611 }
612 }
613 }
614 }
615 }
616}
617
618void
619init_coord()
620{
621 static bool initialized = false;
622 if (initialized)
623 return;
624 initialized = true;
625
626 init_cphtr_cosets();
627 init_cornershtrfin();
628}
629

Generated with cgit - Back to sebastiano.tronto.net