-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsq1opt.cpp
More file actions
1825 lines (1740 loc) · 51 KB
/
sq1opt.cpp
File metadata and controls
1825 lines (1740 loc) · 51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* SQUARE-1 OPTIMISER version 2.1
* by Jaap Scherphuis, jaapsch@yahoo.com, copyright 2003-2011
* and Michael Gottlieb, qqwref@gmail.com, copyright 2023-2024
*/
#include <fstream>
#include <iostream>
#include <cstring>
#include <ctime>
#define NUMHALVES 13
#define NUMLAYERS 158
#define NUMSHAPES 7356
#define FILESTT "sq1stt.dat"
#define FILESCTE "sq1scte.dat"
#define FILESCTC "sq1sctc.dat"
#define FILEP1U "sq1p1u.dat"
#define FILEP2U "sq1p2u.dat"
#define FILEP1W "sq1p1w.dat"
#define FILEP2W "sq1p2w.dat"
#define FILEP1A "sq1p1a.dat"
#define FILEP2A "sq1p2a.dat"
#define TURN_METRIC 0
#define TWIST_METRIC 1
#define ANGLE_METRIC 2
const char* errors[]={
"Unrecognised command line switch.", //1
"Too many command line arguments.",
"Input file not found.",//3
"Bracket ) expected.",//4
"Bottom layer turn expected.",//5
"Comma expected.",//6
"Top layer turn expected.",//7
"Bracket ( expected.",//"8
"Position should be 16 or 17 characters.",//9
"Expected A-H or 1-8.",//10
"Expected - or /.",//11
"Twist is blocked by corner.",//12
"Can't parse input as position string or movelist.",//13
"Unexpected bracket (.",//14
"Number expected.",//15
"Twist / expected.",//16
"Position string has too many copies of a piece.",//17
"Can't stay in cube shape and also use 2gen.",//18
"Position can't be solved with these constraints",//19
};
const int KARNOTATION_LEN = 109;
const std::string KARNOTATION[KARNOTATION_LEN][2]={
{"U", "3,0"},
{"U'", "9,0"},
{"U2", "6,0"},
{"D", "0,3"},
{"D'", "0,9"},
{"D2", "0,6"},
{"u", "2,&"},
{"u'", "^,1"},
{"d", "&,2"},
{"d'", "1,^"},
{"E", "3,9"},
{"E'", "9,3"},
{"e", "3,3"},
{"e'", "9,9"},
{"F", "4,1"},
{"F'", "8,&"},
{"f", "1,4"},
{"f'", "&,8"},
{"M", "1,1"},
{"M'", "&,&"},
{"m", "2,2"},
{"m'", "^,^"},
{"u2", "5,&"},
{"u2'", "7,1"},
{"d2", "&,5"},
{"d2'", "1,7"},
{"T", "2,8"},
{"T'", "^,4"},
{"t", "4,^"},
{"t'", "8,2"},
{"W ", "3,0/9,0/"},
{"W' ", "9,0/3,0/"},
{"B ", "0,3/0,9/"},
{"B' ", "0,9/0,3/"},
{"w ", "2,&/^,1/"},
{"w' ", "^,1/2,&/"},
{"b ", "&,2/1,^/"},
{"b' ", "1,^/&,2/"},
{"E\\ ", "3,0/0,9/"},
{"E\\' ", "9,0/0,3/"},
{"e\\ ", "3,0/0,3/"},
{"e\\' ", "9,0/0,9/"},
{"F2 ", "4,1/8,&/"},
{"F2' ", "8,&/4,1/"},
{"f2 ", "1,4/&,8/"},
{"f2' ", "&,8/1,4/"},
{"U3 ", "3,0/9,0/3,0/"},
{"U3' ", "9,0/3,0/9,0/"},
{"D3 ", "0,3/0,9/0,3/"},
{"D3' ", "0,9/0,3/0,9/"},
{"u3 ", "2,&/^,1/2,&/"},
{"u3' ", "^,1/2,&/^,1/"},
{"u4 ", "2,&/^,1/2,&/^,1/"},
{"u4' ", "^,1/2,&/^,1/2,&/"},
{"d3 ", "&,2/1,^/&,2/"},
{"d3' ", "1,^/&,2/1,^/"},
{"d4", "&,2/1,^/&,2/&,2"},
{"d4' ", "1,^/&,2/1,^/&,2/"},
{"UU", "1,0/5,&/9,0/1,1/9,0/&,0"},
{"UU'", "1,0/2,&/1,1/2,&/7,1/&,0"},
{"FV", "0,&/1,^/&,2/1,^/&,2/0,1"},
{"VF", "1,0/2,&/^,1/2,&/^,1/&,0"},
{" JJ ", "/0,9/3,3/9,0/"},
{" jJ ", "/0,9/3,3/9,0/"},
{" Jj ", "/0,9/3,3/9,0/"},
{" jj ", "/0,9/3,3/9,0/"},
{" bJJ ", "/9,0/3,3/0,9/"},
{" bjJ ", "/9,0/3,3/0,9/"},
{" bJj ", "/9,0/3,3/0,9/"},
{" bjj ", "/9,0/3,3/0,9/"},
{" JN ", "/0,9/0,3/0,9/0,3/"},
{" jN ", "/0,9/0,3/0,9/0,3/"},
{" Jn ", "/0,9/0,3/0,9/0,3/"},
{" jn ", "/0,9/0,3/0,9/0,3/"},
{" NN ", "/9,3/3,9/"},
{" Nn ", "/9,3/3,9/"},
{" nN ", "/9,3/3,9/"},
{" nn ", "/9,3/3,9/"},
{" NJ ", "/3,0/9,0/3,0/9,0/"},
{" nJ ", "/3,0/9,0/3,0/9,0/"},
{" Nj ", "/3,0/9,0/3,0/9,0/"},
{" nj ", "/3,0/9,0/3,0/9,0/"},
{" 3Adj ", "/3,0/&,&/^,1/"},
{" 03Adj ", "/0,3/&,&/1,^/"},
{" JR ", "/9,9/2,&/^,1/3,3/"},
{" jR ", "/9,9/2,&/^,1/3,3/"},
{" Jr ", "/9,9/1,^/&,2/3,3/"},
{" jr ", "/9,9/1,^/&,2/3,3/"},
{" RJ ", "/3,3/1,^/&,2/9,9/"},
{" rJ ", "/3,3/2,&/^,1/9,9/"},
{" Rj ", "/3,3/1,^/&,2/9,9/"},
{" rj ", "/3,3/2,&/^,1/9,9/"},
{" bRJ ", "/9,9/^,1/2,&/3,3/"},
{"brJ ", "1,0/9,9/&,2/1,^/3,3/&,0/"},
{"bRj ", "0,&/9,9/^,1/2,&/3,3/0,1/"},
{"brj ", "1,&/9,9/&,2/1,^/3,3/&,1/"},
{"RR ", "1,0/2,&/^,4/5,&/^,1/&,0/"},
{"rr ", "0,&/^,1/5,&/^,4/2,&/0,1/"},
{"pJ", "0&/^,1/2,2/0,9/0,1"},
{"pj", "0,&/1,^/2,2/9,0/0,1"},
{"pN", "1,0/2,8/^,4/&,0"},
{"fpJ", "1,0/2,&/^,^/0,3/&,0"},
{"AA ", "1,0/0,9/2,2/0,9/^,4/&,0/"},
{"aa", "0&/1,^/2,2/1,^/8,2/0,1"},
{"TT", "1,0/5,&/9,0/^,^/0,3/&,0"},
{"OppOpp", "1,0/&,&/6,0/1,1/&,0"},
{"FF", "1,0/0,9/2,2/0,9/1,1/9,3/&,0"},
{"M2", "1,0/&,&/0,1"},
{"m2", "1,0/5,&/7,1/&,0"}
};
int verbosity = 5;
bool generator=false;
bool usenegative=false;
bool usebrackets=false;
bool karnotation=false;
int metric = TURN_METRIC;
int maxX = 6;
int maxY = 6;
int maxTotal = 12;
class HalfLayer {
public:
int pieces, turn, nPieces;
HalfLayer(int p, int t) {
int nEdges=0;
pieces = p;
for(int i=0, m=1; i<6; i++, m<<=1){
if( (pieces&m)!=0 ) nEdges++;
}
nPieces=3+nEdges/2;
turn=t;
}
};
class Layer {
public:
HalfLayer& h1, & h2;
int turnt, turnb;
int nPieces;
bool turnParityOdd;
bool turnParityOddb;
int pieces;
int tpieces, bpieces; // result after turn
Layer( HalfLayer& p1, HalfLayer& p2): h1(p1), h2(p2) {
pieces = (h1.pieces<<6)+h2.pieces;
nPieces = h1.nPieces + h2.nPieces;
int m=1;
for(turnt=1; turnt<6; turnt++){
if( (h1.turn&h2.turn&m)!=0 ) break;
m<<=1;
}
if( turnt==6 ) turnb=6;
else{
m=1<<4;
for(turnb=1; turnb<5; turnb++){
if( (h1.turn&h2.turn&m)!=0 ) break;
m>>=1;
}
}
tpieces=pieces;
int nEdges=0;
for( int i=0; i<turnt; i++ ){
if( (tpieces&1)!=0 ) { tpieces+=(1<<12); nEdges++; }
tpieces>>=1;
}
//find out parity of that layer turn
// Is odd cycle if even # pieces, and odd number passes seam
// Note (turn+edges)/2 = number of pieces crossing seam
turnParityOdd = (nPieces&1)==0 && ((turnt+nEdges)&2)!=0;
bpieces=pieces;
nEdges=0;
for( int i=0; i<turnb; i++ ){
bpieces<<=1;
if( (bpieces&(1<<12))!=0 ) { bpieces-=(1<<12)-1; nEdges++; }
}
//find out parity of that layer turn
// Is odd cycle if even # pieces, and odd number passes seam
// Note (turn+edges)/2 = number of pieces crossing seam
turnParityOddb = (nPieces&1)==0 && ((turnb+nEdges)&2)!=0;
}
};
class Sq1Shape {
public:
Layer& topl, &botl;
int pieces;
bool parityOdd;
int tpieces[4];
bool tparity[4];
Sq1Shape( Layer& l1, Layer& l2, bool p) : topl(l1), botl(l2) {
parityOdd=p;
pieces = (l1.pieces<<12)+l2.pieces;
tpieces[0] = (l1.tpieces<<12)+l2. pieces;
tpieces[1] = (l1. pieces<<12)+l2.bpieces;
tpieces[2] = (l1.h1.pieces<<18)+(l2.h1.pieces<<12)+(l1.h2.pieces<<6)+(l2.h2.pieces);
// calculate mirrored shape
tpieces[3] = 0;
for( int m=1, i=0; i<24; i++,m<<=1){
tpieces[3]<<=1;
if( (pieces&m)!=0 ) tpieces[3]++;
}
tparity[0] = parityOdd^l1.turnParityOdd;
tparity[1] = parityOdd^l2.turnParityOddb;
tparity[2] = parityOdd^( (l1.h2.nPieces&1)!=0 && (l2.h1.nPieces&1)!=0 );
tparity[3] = parityOdd;
}
};
class ChoiceTable {
public:
unsigned char choice2Idx[256];
unsigned char idx2Choice[70];
ChoiceTable(){
unsigned char nc=0;
for( int i=0; i<255; i++ ) choice2Idx[i]=255;
for( int i=1; i<255; i<<=1 ){
for( int j=i+i; j<255; j<<=1 ){
for( int k=j+j; k<255; k<<=1 ){
for( int l=k+k; l<255; l<<=1 ){
choice2Idx[i+j+k+l]=nc;
idx2Choice[nc++]=(unsigned char)(i+j+k+l);
}
}
}
}
}
};
class ShapeTranTable {
public:
int nShape;
Sq1Shape* shapeList[NUMSHAPES];
int (*tranTable)[4];
HalfLayer* hl[NUMHALVES];
Layer* ll[NUMLAYERS];
ShapeTranTable(){
//first build list of possible halflayers
int hi[]={ 0, 3,12,48, 9,36,33, 15,39,51,57,60, 63};
int ht[]={42, 43,46,58,45,54,53, 47,55,59,61,62, 63};
for( int i=0; i<NUMHALVES; i++ ){ hl[i]=new HalfLayer(hi[i],ht[i]); }
//Now build list of possible Layers
int lll=0;
for( int i=0; i<NUMHALVES; i++ ){
for( int j=0; j<NUMHALVES; j++ ){
if( hl[i]->nPieces + hl[j]->nPieces<=10 ){
ll[lll++]=new Layer( *hl[i], *hl[j] );
}
}
}
//Now build list of all possible shapes
nShape=0;
for( int i=0; i<lll; i++ ){
for( int j=0; j<lll; j++ ){
if( ll[i]->nPieces + ll[j]->nPieces==16 ){
shapeList[nShape++]=new Sq1Shape( *ll[i], *ll[j], true );
shapeList[nShape++]=new Sq1Shape( *ll[i], *ll[j], false );
}
}
}
// At last we can calculate full transition table
tranTable = new int[NUMSHAPES][4];
// see if can be found on file
std::ifstream is(FILESTT, std::ios::binary);
if( is.fail() ){
// no file. calculate table.
for( int i=0; i<nShape; i++ ){
//effect on shape of each move, incuding reflection
for( int m=0; m<4; m++ ){
for( int j=0; j<nShape; j++ ){
if( shapeList[i]->tpieces[m] == shapeList[j]->pieces &&
shapeList[i]->tparity[m] == shapeList[j]->parityOdd ){
tranTable[i][m]=j;
break;
}
}
}
}
// save to file
std::ofstream os(FILESTT, std::ios::binary);
os.write( (char*)tranTable, nShape*4*sizeof(int) );
}else{
// read from file
nShape = NUMSHAPES;
is.read( (char*)tranTable, nShape*4*sizeof(int) );
}
}
~ShapeTranTable(){
for( int i=0; i<NUMHALVES; i++ ){ delete hl[i]; }
for( int i=0; i<NUMLAYERS; i++ ){ delete ll[i]; }
for( int i=0; i<nShape; i++ ){ delete shapeList[i]; }
delete[] tranTable;
}
inline int getShape(int s, bool p){
for( int i=0; i<nShape; i++){
if( shapeList[i]->pieces == s && shapeList[i]->parityOdd==p ) return i;
}
return -1;
}
inline int getTopTurn(int s){
return shapeList[s]->topl.turnt;
}
inline int getBotTurn(int s){
return shapeList[s]->botl.turnb;
}
};
class ShapeColPos {
ShapeTranTable &stt;
ChoiceTable &ct;
int shapeIx;
int colouring; //24bit string
bool edgesFlag;
public:
ShapeColPos( ShapeTranTable& stt0, ChoiceTable& ct0)
: stt(stt0), ct(ct0) {}
void set( int shp, int col, bool edges )
{
// col is 8 bit colouring of one type of piece.
// edges set then edge colouring, else corner colouring
// get full 24 bit colouring.
int c=ct.idx2Choice[col];
shapeIx = shp;
edgesFlag = edges;
colouring=0;
int s=stt.shapeList[shapeIx]->pieces;
if( edges ){
for( int m=1, i=0, n=1; i<24; m<<=1, i++){
if( (s&m)!=0 ) {
if( (c&n)!=0 ) colouring |= m;
n<<=1;
}
}
}else{
for( int m=3, i=0, n=1; i<24; m<<=1, i++){
if( (s&m)==0 ) {
if( (c&n)!=0 ) colouring |= m;
n<<=1;
m<<=1; i++;
}
}
}
}
void domove(int m){
const int botmask = (1<<12)-1;
const int topmask = (1<<24)-(1<<12);
const int botrmask = (1<<12)-(1<<6);
const int toprmask = (1<<18)-(1<<12);
const int leftmask = botmask+topmask-botrmask-toprmask;
if( m==0 ){
int tn=stt.getTopTurn(shapeIx);
int b=colouring&botmask;
int t=colouring&topmask;
t+=(t>>12);
t<<=(12-tn);
colouring = b + (t&topmask);
}else if( m==1 ){
int tn=stt.getBotTurn(shapeIx);
int b=colouring&botmask;
int t=colouring&topmask;
b+=(b<<12);
b>>=(12-tn);
colouring = t + (b&botmask);
}else if( m==2 ){
int b=colouring&botrmask;
int t=colouring&toprmask;
colouring = (colouring&leftmask) + (t>>6) + (b<<6);
}
shapeIx=stt.tranTable[shapeIx][m];
}
unsigned char getColIdx(){
int c=0,n=1;
int s=stt.shapeList[shapeIx]->pieces;
if( edgesFlag ){
for( int m=1, i=0; i<24; m<<=1, i++){
if( (s&m)!=0 ) {
if( (colouring&m)!=0 ) c |= n;
n<<=1;
}
}
}else{
for( int m=3, i=0; i<24; m<<=1, i++){
if( (s&m)==0 ) {
if( (colouring&m)!=0 ) c |= n;
n<<=1;
m<<=1; i++;
}
}
}
return(ct.choice2Idx[c]);
}
};
class ShpColTranTable {
public:
char (*tranTable)[70][3];
ShapeTranTable& stt;
ChoiceTable& ct;
ShpColTranTable( ShapeTranTable& stt0, ChoiceTable& ct0, bool edges )
: stt(stt0), ct(ct0)
{
ShapeColPos p(stt,ct);
tranTable = new char[NUMSHAPES][70][3];
// see if can be found on file
std::ifstream is( edges? FILESCTE : FILESCTC, std::ios::binary);
if( is.fail() ){
// no file. calculate table.
// Calculate transition table
int i,j,m;
for( m=0; m<3; m++ ){
for( i=0; i<NUMSHAPES; i++ ){
for( j=0; j<70; j++){
p.set(i,j,edges);
p.domove(m);
tranTable[i][j][m]=p.getColIdx();
if( p.getColIdx()==255 ){
exit(0);
}
}
}
}
// save to file
std::ofstream os(edges? FILESCTE : FILESCTC, std::ios::binary);
os.write( (char*)tranTable, NUMSHAPES*3*70*sizeof(char) );
}else{
// read from file
is.read( (char*)tranTable, NUMSHAPES*3*70*sizeof(char) );
}
}
~ShpColTranTable(){
delete[] tranTable;
}
};
// FullPosition holds position with each piece individually specified.
// Pieces 0-7 are corners and appear twice in a row. Pieces 8-15 are edges and appear once
// Piece numbers below 0 are partially specified corners. Based on the value modulo 3, it's a
// top corner (0), bottom corner (-2), or any corner (-1).
// Piece numbers above 15 are partially specified edges. Based on the value modulo 3, it's
// top edge (0), bottom edge (1), or any edge (2).
class FullPosition {
public:
int pos[24];
int middle;
FullPosition(){ reset(); }
void reset(){
middle=1;
for( int i=0; i<24; i++)
pos[i]="AAIBBJCCKDDLMEENFFOGGPHH"[i]-'A';
}
void print(){
for(int i=0; i<24; i++){
if (pos[i] < 0) {
std::cout<<"UWV"[(-pos[i])%3];
} else if (pos[i] > 15) {
std::cout<<"XYZ"[pos[i]%3];
} else {
std::cout<<"ABCDEFGH12345678"[pos[i]];
}
if( pos[i]<8 ) i++;
}
std::cout<<"/ -"[middle+1];
}
void random(int twoGen, bool keepCubeShape){
middle = (rand()&1)!=0?-1:1;
do{
//make starting position
int tmp[16];
for( int i=0; i<8; i++) {
tmp[2*i + (i>3?1:0)] = i;
tmp[2*i + (i>3?0:1)] = 8+i;
}
// shuffle
if (keepCubeShape) {
bool parity = false;
int cornersToMix = twoGen==1 ? 6 : 8;
int edgesToMix = twoGen==1 ? 7 : 8;
for (int i=0; i<cornersToMix; i++) {
int j = i + rand() % (cornersToMix - i);
int k = tmp[2*i + (i>3?1:0)];
tmp[2*i + (i>3?1:0)] = tmp[2*j + (j>3?1:0)];
tmp[2*j + (j>3?1:0)] = k;
if (i!=j) parity ^= true;
}
for (int i=0; i<edgesToMix; i++) {
int j = i + rand() % (edgesToMix - i);
int k = tmp[2*i + (i>3?0:1)];
tmp[2*i + (i>3?0:1)] = tmp[2*j + (j>3?0:1)];
tmp[2*j + (j>3?0:1)] = k;
if (i!=j) parity ^= true;
}
if (parity) {
int k = tmp[0];
tmp[0] = tmp[2];
tmp[2] = k;
}
} else {
int nToMix = twoGen==2 ? 12 : (twoGen==1 ? 13 : 16);
for( int i=0;i<nToMix; i++){
int j=rand()%(nToMix-i);
int k=tmp[i];tmp[i]=tmp[i+j];tmp[i+j]=k;
}
}
//convert to position array
for(int i=0, j=0;i<16;i++){
pos[j++]=tmp[i];
if( tmp[i]<8 ) pos[j++]=tmp[i];
}
// if p2g and keeping cubeshape, are the corners solvable in 2gen? if not, try again
if (twoGen == 1 && keepCubeShape) {
if (!has2GenCorners()) {
pos[6] = pos[5]; continue; // fail the condition
}
}
// ABF. if keeping cube shape, adjust both; otherwise, if p2g, adjust D only
if (keepCubeShape) {
if ((rand()&1)!=0) {
tmp[0] = pos[11];
for (int i=10; i>=0; i--) {
pos[i+1] = pos[i];
}
pos[0] = tmp[0];
}
if ((rand()&1)!=0) {
tmp[0] = pos[12];
for (int i=12; i<=22; i++) {
pos[i] = pos[i+1];
}
pos[23] = tmp[0];
}
} else if (twoGen == 1 && (rand()&1)!=0 && pos[11]!=pos[12]) {
// in pseudo 2gen, with 50% chance, if the layers are validly separated, do a (0,-1)
tmp[0] = pos[12];
for (int i=12; i<=22; i++) {
pos[i] = pos[i+1];
}
pos[23] = tmp[0];
}
// test twistable
}while( pos[5]==pos[6] || pos[11]==pos[12] || pos[17]==pos[18] || pos[12]==pos[23]);
}
void set(int p[],int m){
for(int i=0;i<24;i++)pos[i]=p[i];
middle=m;
};
void doTop(int m){
m%=12;
if(m<0)m+=12;
while(m>0){
int c=pos[11];
for(int i=11;i>0;i--) pos[i]=pos[i-1];
pos[0]=c;
m--;
}
}
void doBot(int m){
m%=12;
if(m<0)m+=12;
while(m>0){
int c=pos[23];
for(int i=23;i>12;i--) pos[i]=pos[i-1];
pos[12]=c;
m--;
}
}
bool doTwist(){
if( !isTwistable() ) return false;
for(int i=6;i<12;i++){
int c=pos[i];
pos[i]=pos[i+6];
pos[i+6]=c;
}
middle=-middle;
return true;
}
bool isTwistable(){
return( pos[0]!=pos[11] && pos[5]!=pos[6] && pos[12]!=pos[23] && pos[17]!=pos[18] );
}
int getShape(){
int s=0;
for(int m=1<<23,i=0; i<24; i++,m>>=1){
if(pos[i]>=8) s|=m;
}
return(s);
}
bool getParityOdd(){
bool p=false;
for(int i=0; i<24; i++){
for(int j=i; j<24; j++){
if( pos[j]<pos[i]) p=!p;
if(pos[j]<8)j++;
}
if(pos[i]<8)i++;
}
return(p);
}
int getEdgeColouring(int cl){
const int clp[3][4]={ { 8, 9,10,11}, { 8, 9,13,14}, {15,14,10, 9} };
int c=0;
int cnt=0;
int m=(cl!=2)?1<<7:1;
for(int i=0; i<24; i++){
if( pos[i]>=8 ){
for(int j=0; j<4; j++){
if( pos[i]==clp[cl][j] || (pos[i]>15 && pos[i]%3==0 && cl==0)) { // edge up
c|=m;
cnt++;
break;
}
}
if(cl!=2) m>>=1; else m<<=1;
}
}
if (cnt==4) return c;
else return -1;
}
int getCornerColouring(int cl){
const int clp[3][4]={ {0,1,2,3}, {0,1,5,6}, {7,6,2,1} };
int c=0;
int cnt=0;
int m=(cl!=2)?1<<7:1;
for(int i=0; i<24; i++){
if( pos[i]<8 ){
for(int j=0; j<4; j++){
if( pos[i]==clp[cl][j] || (pos[i]<0 && pos[i]%3==0 && cl==0)) { // corner up
c|=m;
cnt++;
break;
}
}
if(cl!=2) m>>=1; else m<<=1;
i++;
}
}
if (cnt==4) return c;
else return -1;
}
bool parseNumberForward(const char*inp, int& ix, int& num){
bool min = false;
num = 0;
while( inp[ix]==' ' ) ix++;
if( inp[ix]=='-') {
min=true;
ix++;
}
if( inp[ix]<'0' || inp[ix]>'9' ) return true;
while( inp[ix]>='0' && inp[ix]<='9' ){
num =num*10+(inp[ix]-'0');
ix++;
}
if( min ) num = -num;
while( inp[ix]==' ' ) ix++;
return false;
}
bool parseNumberBackward(const char*inp, int& ix, int& num){
int digvalue = 1;
num = 0;
while( ix>=0 && inp[ix]==' ' ) ix--;
if( ix<0 ) return true;
if( inp[ix]<'0' || inp[ix]>'9' ) return true;
while( ix>=0 && inp[ix]>='0' && inp[ix]<='9' ){
num =num+digvalue*(inp[ix]-'0');
digvalue*=10;
ix--;
}
if( ix>=0 && inp[ix]=='-'){
num = -num;
ix--;
}
while( ix>=0 && inp[ix]==' ' ) ix++;
return false;
}
int parseInput( const char* inp ){
// scan characters
const char* t=inp;
int f=0;
while(*t){
if( *t == ',' || *t == '(' || *t == ')' || *t == '9' || *t == '0' ){
f|=1; // cannot be position string, but may be movelist
}else if( (*t>='a' && *t<='h') || (*t>='A' && *t<='H') || (*t>='u' && *t<='z') || (*t>='U' && *t<='Z') ){
f|=2; // cannot be movelist, but may be position string
}else if( *t!='/' && *t!='-' && (*t<'1' || *t>'8') ){
f|=3; // cannot be either
}
t++;
}
if( f==3 || f==0 ){
return(13);
}
reset();
int lw=0,lu=0;
if( f==1 && !generator){
// solution move sequence. start parsing from end
int md=0;
int i=strlen(inp)-1;
while( i>=0 ){
while( i>=0 && inp[i]==' ' ) i--;
if( md==0 ){ // parsing any move
if(inp[i]=='/') md = 1;
else md = 2;
}else if( md==1 ){
if(inp[i--]!='/') return 16;
if(!doTwist()) return 12;
lu++;lw++;
md=2;
}else if( md==2 ){
int m = 0;
bool br=false;
if( inp[i]==')' ) { i--; br=true; }
// parsing bot turn
if( parseNumberBackward(inp, i, m) ) return 5;
m%=12;
doBot(-m);
if(m!=0) lu++;
if( i<0 || inp[i--]!=',' ) return 6;
// parsing top turn
if( parseNumberBackward(inp, i, m) ) return 7;
m%=12;
doTop(-m);
if(m!=0) lu++;
if( br && ( i<0 || inp[i--]!='(' )) return 8;
md--;
}
}
if( !isTwistable() ) return 12;
if( verbosity>=2) std::cout<<"Input:"<<inp<<" ["<<lw<<"|"<<lu<<"]"<<std::endl;
}else if( f==1 ){
// generating move sequence. start parsing from beginning
int md=0;
int i=0;
while( inp[i]!=0 ){
while( inp[i]==' ' ) i++;
if( md==0 ){ // parsing any move
if(inp[i]=='/') md = 1;
else md = 2;
}else if( md==1 ){
if(inp[i++]!='/') return 16;
if(!doTwist()) return 12;
lu++;lw++;
md=2;
}else if( md==2 ){
int m = 0;
bool br=false;
if( inp[i]=='(' ) { i++; br=true; }
// parsing top turn
if( parseNumberForward(inp, i, m) ) return 7;
m%=12;
doTop(m);
if(m!=0) lu++;
if( inp[i++]!=',' ) return 6;
// parsing bot turn
if( parseNumberForward(inp, i, m) ) return 5;
m%=12;
doBot(m);
if(m!=0) lu++;
if( br && inp[i++]!=')' ) return 4;
md--;
}
}
if( !isTwistable() ) return 12;
if( verbosity>=2) std::cout<<"Input:"<<inp<<" ["<<lw<<"|"<<lu<<"]"<<std::endl;
}else{
// position
if( strlen(inp)!=16 && strlen(inp)!=17 ) return(9);
int pieceCount[16]; // track counts of each piece, so we can detect multiples of one piece
int cecount[6]; // track total [up, down, all] + 3*[corners, edges]
for (int i=0; i<16; i++) pieceCount[i] = 0;
for (int i=0; i<6; i++) cecount[i] = 0;
int j=0;
int pi[24];
// we can't reuse a piece number because two of the same number means a corner, so
// each partially defined piece gets a separate set of 3 possible values
int nextPartialCorner = -3;
int nextPartialEdge = 18;
for( int i=0; i<16; i++){
int k=inp[i];
if(k>='a' && k<='z') k+=('A'-'a');
if(k>='A' && k<='H') k-='A';
else if(k>='1' && k<='8') k-='1'-8;
else if(k>='U' && k<='W') {
k+=(nextPartialCorner-'U');
nextPartialCorner -= 3;
}
else if(k>='X' && k<='Z') {
k+=(nextPartialEdge-'X');
nextPartialEdge += 3;
}
else return(10);
pi[j++] = k;
if (k>=0 && k<=15) pieceCount[k]++;
if (k<8) {
pi[j++] = k;
cecount[2]++;
if ((k<0 && k%3==0) || (k>=0 && k<=3)) cecount[0]++; // corner up
if ((k<0 && k%3==-2) || (k>=4 && k<=7)) cecount[1]++; // corner down
} else {
cecount[5]++;
if ((k>15 && k%3==0) || (k>=8 && k<=11)) cecount[3]++; // edge up
if ((k>15 && k%3==1) || (k>=12 && k<=15)) cecount[4]++; // edge down
}
}
for (int i=0; i<16; i++) {
if (pieceCount[i] > 1) return(17);
}
if (cecount[0] > 4 || cecount[1] > 4 || cecount[2] > 8 || cecount[3] > 4 || cecount[4] > 4 || cecount[5] > 8) return 17;
int midLayer=0;
if( strlen(inp)==17 ){
int k=inp[16];
if( k!='-' && k!='/' ) return(11);
midLayer = (k=='-') ? 1 : -1;
}
set(pi,midLayer);
}
return(0);
}
// assuming we're in a square/square shape, check if the corners are solvable with 2gen
bool has2GenCorners(){
// get corners
int tmp[6];
int j=0;
for (int i=0; i<18; i++) {
if (pos[i]<8) {
if (j%2 == 0) tmp[j/2] = pos[i];
j++;
}
}
// place D corners - if we find a D corner on U, AUF and then insert
int found_d = -1;
for (int i=0; i<4; i++) if(tmp[i]>3) found_d = i;
if (found_d > -1) {
int tmp2[4];
for (int i=0; i<4; i++) tmp2[i] = tmp[i];
for (int i=0; i<4; i++) tmp[i] = tmp2[(i + found_d) % 4];
int k = tmp[0]; tmp[0] = tmp[4]; tmp[4] = k;
k = tmp[2]; tmp[2] = tmp[3]; tmp[3] = k;
}
found_d = -1;
for (int i=0; i<4; i++) if(tmp[i]>3) found_d = i;
if (found_d > -1) {
int tmp2[4];
for (int i=0; i<4; i++) tmp2[i] = tmp[i];
for (int i=0; i<4; i++) tmp[i] = tmp2[(i + found_d) % 4];
int k = tmp[0]; tmp[0] = tmp[5]; tmp[5] = k;
k = tmp[1]; tmp[1] = tmp[2]; tmp[2] = k;
}
// adjust if D corners are swapped, then AUF
if (tmp[4] == 5 && tmp[5] == 4) {
tmp[4] = 4; tmp[5] = 5;
int k = tmp[0]; tmp[0] = tmp[2]; tmp[2] = k;
}
int found_u = -1;
for (int i=0; i<4; i++) if(tmp[i]==0) found_u = i;
if (found_u > -1) {
int tmp2[4];
for (int i=0; i<4; i++) tmp2[i] = tmp[i];
for (int i=0; i<4; i++) tmp[i] = tmp2[(i + found_u) % 4];
}
if (tmp[0] == 0 && tmp[1] == 1 && tmp[2] == 2 && tmp[3] == 3 && tmp[4] == 4 && tmp[5] == 5) return true;
return false;
}
bool singleMatch(int posI, int solvedI) {
if (posI == solvedI) return true;
if (posI>15 && posI%3==0 && solvedI >= 8 && solvedI <= 11) return true; // edge up
if (posI>15 && posI%3==1 && solvedI >= 12 && solvedI <= 15) return true; // edge down
if (posI<0 && posI%3==0 && solvedI >= 0 && solvedI <= 3) return true; // corner up
if (posI<0 && posI%3==-2 && solvedI >= 4 && solvedI <= 7) return true; // corner down
if (posI>15 && posI%3==2 && solvedI >= 8 && solvedI <= 15) return true; // edge any
if (posI<0 && posI%3==-1 && solvedI >= 0 && solvedI <= 7) return true; // corner any
return false;
}
bool matchesSolved() {
int solved[24] = {0, 0, 8, 1, 1, 9, 2, 2, 10, 3, 3, 11, 12, 4, 4, 13, 5, 5, 14, 6, 6, 15, 7, 7};
for (int i=0; i<24; i++) {
if (!singleMatch(pos[i], solved[i])) return false;
}
return true;
}
bool isPartial() {
for (int i=0; i<24; i++) {
if (pos[i] < 0 || pos[i] > 15) return true;
}
return false;
}
};
//pruning table for combination of shape,edgecolouring,cornercolouring.
class PrunTable {
public:
char (*table)[70][70];
ShapeTranTable& stt;
ShpColTranTable& scte;
ShpColTranTable& sctc;
PrunTable( FullPosition& p0, int cl, ShapeTranTable& stt0, ShpColTranTable& scte0, ShpColTranTable& sctc0)
: stt(stt0), scte(scte0), sctc(sctc0)
{
// Calculate pruning table
table = new char[NUMSHAPES][70][70];
const char *fname;
if(metric == TURN_METRIC){
fname = (cl==0)? FILEP1U : FILEP2U;
} else if (metric == ANGLE_METRIC) {
fname = (cl==0)? FILEP1A : FILEP2A;
} else {
fname = (cl==0)? FILEP1W : FILEP2W;
}
// see if can be found on file
std::ifstream is( fname, std::ios::binary );
if( is.fail() ){
// no file. calculate table.
// clear table
for( int i0=0; i0<NUMSHAPES; i0++ ){
for( int i1=0; i1<70; i1++){
for( int i2=0; i2<70; i2++){
table[i0][i1][i2]=0;
}}}
//set start position
int s0 = stt.getShape(p0.getShape(),p0.getParityOdd());
int e0 = p0.getEdgeColouring(cl);
int c0 = p0.getCornerColouring(cl);
e0 = scte0.ct.choice2Idx[e0];
c0 = sctc0.ct.choice2Idx[c0];
if (metric == TURN_METRIC || metric == ANGLE_METRIC){
table[s0][e0][c0]=1;
}else{
setAll(s0,e0,c0,1);
}
char l=1;
int n=1;
int last_nonzero=-1;