-
Notifications
You must be signed in to change notification settings - Fork 0
/
CubeReader.java
612 lines (602 loc) · 20.5 KB
/
CubeReader.java
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
/**
* Class used to get the input cube configuration from the input string.
* This class can be rewritten and replaced in order to support
* another way of cube state specification (e.g. graphical).
*/
import java.util.ArrayList;
public final class CubeReader {
// leading and trailing move characters
private static final char[] freeMove = {
0, 'U', 'D', 'F', 'B', 'L', 'R', 'E', 'S', 'M'
};
private static final char[] freeMoveMask = {
'M', 'S', 'E', 'R', 'L', 'B', 'F', 'D', 'U'
};
// textual representation of edges
private static final String[] edgeString = {
"UF", "UR", "UB", "UL", "DF", "DR", "DB", "DL", "FR", "FL", "BR", "BL",
"FU", "RU", "BU", "LU", "FD", "RD", "BD", "LD", "RF", "LF", "RB", "LB"
};
// textual representation of corners
private static final String[] cornString = {
"UFR", "URB", "UBL", "ULF", "DRF", "DFL", "DLB", "DBR",
"FRU", "RBU", "BLU", "LFU", "RFD", "FLD", "LBD", "BRD",
"RUF", "BUR", "LUB", "FUL", "FDR", "LDF", "BDL", "RDB",
// orientation is determined only by the first character
"URF", "UBR", "ULB", "UFL", "DFR", "DLF", "DBL", "DRB",
"FUR", "RUB", "BUL", "LUF", "RDF", "FDL", "LDB", "BDR",
"RFU", "BRU", "LBU", "FLU", "FRD", "LFD", "BLD", "RBD"
};
private int[] e = new int[12]; // locations and orientations of the edges
private int[] c = new int[8]; // locations and orientations of the corners
private int[] ep = new int[12]; // locations of the edges
private int[] cp = new int[8]; // locations of the corners
private int[] eo = new int[12]; // orientations of the edges
private int[] co = new int[8]; // orientations of the corners
private int[] ep1 = new int[12]; // mark of locations of the edges
private int[] cp1 = new int[8]; // mark of locations of the corners
private int[] eo1 = new int[12]; // mark of orientations of the edges
private int[] co1 = new int[8]; // mark of orientations of the corners
private Solution solution; // phase B solving algorithm
private CubeState state; // characteristic values of the cube state
private Options options; // various options
public String init(Options options, String input) {
this.options = options;
state = new CubeState();
String r = enter(input);
if (r != "OK")
return r;
r = check();
if (r != "OK")
return r;
preprocess();
return "OK";
}
public void solve(ArrayList<String> ar) {
Transform transform = new Transform(state, options.findOptimal);
Turn turn = Turn.turn(options.metric);
Prune prune = new Prune(transform, turn, state, options.findOptimal);
TurnList turnList = new TurnList(state.turnMask);
solution = new Solution(transform, prune, turn, turnList);
solution.solve(state, options, ar);
}
private String enter(String input) {
// read input from a user
this.input = input;
inputPos = 0;
state.turnMask = 0;
state.freeMove = 0;
state.freeMoveMask = 0;
state.symmetry = SliceTurnTransform.ID;
// reading the textual representation of 12 edges
String[] edgeStr = new String[12];
for (int i = 0; i < 12; i++) {
edgeStr[i] = getInputToken();
if (edgeStr[i].equals("Q") || edgeStr[i].equals("q"))
return "QUIT";
if (edgeStr[i].equals("N") || edgeStr[i].equals("n"))
return "NEW";
if (edgeStr[i].equals(""))
return "Insufficient number of edges";
if (edgeStr[i].charAt(0) == '$' && edgeStr[i].length() == 2) {
int sym = edgeStr[i].charAt(1) - 'A';
if (sym < 0 || sym > 23)
return "Invalid symmetry format";
state.symmetry = sym;
i--;
}
else if (edgeStr[i].charAt(0) == '~' && edgeStr[i].length() > 1) {
int rev = 0;
for (int j = 1; j < edgeStr[i].length(); j++) {
for (int k = 0; k < freeMove.length; k++) {
if (freeMove[k] == edgeStr[i].charAt(j)) {
rev = rev * 10 + k;
break;
}
if (k == freeMove.length - 1)
return "Invalid character '" + edgeStr[i].charAt(j) + "' in initial move: " + edgeStr[i];
}
}
while (rev != 0) {
state.freeMove = state.freeMove * 10 + rev % 10;
rev /= 10;
}
for (int j = 1; j < edgeStr[i].length(); j++) {
for (int k = 0; k < freeMoveMask.length; k++) {
if (freeMoveMask[k] == edgeStr[i].charAt(j)) {
state.freeMoveMask = state.freeMoveMask | (1 << k);
break;
}
if (k == freeMoveMask.length - 1)
return "Invalid character '" + edgeStr[i].charAt(j) + "' in end move: " + edgeStr[i];
}
}
i--;
}
else if (edgeStr[i].charAt(0) >= '0' && edgeStr[i].charAt(0) <= '7') {
state.turnMask = state.turnMask & 0777077 | ((edgeStr[i].charAt(0) - '0') << 6);
if (edgeStr[i].length() > 1 && edgeStr[i].charAt(1) >= '0' && edgeStr[i].charAt(1) <= '7')
state.turnMask = state.turnMask & 0777707 | ((edgeStr[i].charAt(1) - '0') << 3);
if (edgeStr[i].length() > 2 && edgeStr[i].charAt(2) >= '0' && edgeStr[i].charAt(2) <= '7')
state.turnMask = state.turnMask & 0777770 | (edgeStr[i].charAt(2) - '0');
if (edgeStr[i].length() > 3 && edgeStr[i].charAt(3) >= '0' && edgeStr[i].charAt(3) <= '7')
state.turnMask = state.turnMask & 0077777 | ((edgeStr[i].charAt(3) - '0') << 15);
if (edgeStr[i].length() > 4 && edgeStr[i].charAt(4) >= '0' && edgeStr[i].charAt(4) <= '7')
state.turnMask = state.turnMask & 0707777 | ((edgeStr[i].charAt(4) - '0') << 12);
if (edgeStr[i].length() > 5 && edgeStr[i].charAt(5) >= '0' && edgeStr[i].charAt(5) <= '7')
state.turnMask = state.turnMask & 0770777 | ((edgeStr[i].charAt(5) - '0') << 9);
i--;
}
else if (edgeStr[i].equals("!!"))
for (; i < 12; i++)
edgeStr[i] = "!";
else if (edgeStr[i].equals("@!!"))
for (; i < 12; i++)
edgeStr[i] = "@!";
else if (edgeStr[i].equals("??"))
for (; i < 12; i++)
edgeStr[i] = "?";
else if (edgeStr[i].equals("@??"))
for (; i < 12; i++)
edgeStr[i] = "@?";
}
// reading the textual representation of 8 corners
String[] cornStr = new String[8];
for (int i = 0; i < 8; i++) {
cornStr[i] = getInputToken();
if (cornStr[i].equals("Q") || cornStr[i].equals("q"))
return "QUIT";
if (cornStr[i].equals("N") || cornStr[i].equals("n"))
return "NEW";
if (cornStr[i].equals(""))
return "Insufficient number of corners";
if (cornStr[i].equals("!!"))
for (; i < 8; i++)
cornStr[i] = "!";
else if (cornStr[i].equals("@!!"))
for (; i < 8; i++)
cornStr[i] = "@!";
else if (cornStr[i].equals("??"))
for (; i < 8; i++)
cornStr[i] = "?";
else if (cornStr[i].equals("@??"))
for (; i < 8; i++)
cornStr[i] = "@?";
}
// filling the edge locations and orientations to e[12], eo*[12], ep*[12]
// eo1[] is not completely filled (we do not know which edge is '@?')
// we fill locations that are not set later
for (int i = 0; i < 12; i++) {
e[i] = 0;
ep[i] = 0;
eo[i] = 0;
ep1[i] = 1; // ok for unknown
eo1[i] = -1; // fixed later for unknown
}
for (int i = 0; i < 12; i++) {
if (edgeStr[i].equals("!")) {
e[i] = i + 1;
ep[i] = i + 1;
eo[i] = 1;
ep1[i] = 0;
eo1[i] = 0;
}
else if (edgeStr[i].equals("@!")) {
e[i] = i + 1;
ep[i] = i + 1;
ep1[i] = 0;
eo1[i] = 1;
}
else if (edgeStr[i].equals("-!")) {
e[i] = i + 12 + 1;
ep[i] = i + 1;
eo[i] = 2;
ep1[i] = 0;
eo1[i] = 0;
}
else if (edgeStr[i].equals("?")) {
eo[i] = 1;
}
else if (edgeStr[i].equals("@?")) {
}
else if (edgeStr[i].equals("-?")) {
eo[i] = 2;
}
else if (edgeStr[i].charAt(0) == '@') {
boolean found = false;
for (int j = 0; j < edgeString.length; j++) {
if (edgeStr[i].substring(1).equals(edgeString[j])) {
e[i] = j % 12 + 1;
ep[i] = (e[i] - 1) % 12 + 1;
ep1[ep[i] - 1] = 0;
eo1[ep[i] - 1] = 1;
found = true;
break;
}
}
if (!found)
return "Improper edge: " + edgeStr[i];
}
else if (edgeStr[i].charAt(0) == '-') {
boolean found = false;
for (int j = 0; j < edgeString.length; j++) {
if (edgeStr[i].substring(1).equals(edgeString[j])) {
e[i] = (j + 12) % 24 + 1;
ep[i] = (e[i] - 1) % 12 + 1;
eo[i] = (e[i] - 1) / 12 + 1;
ep1[ep[i] - 1] = 0;
eo1[ep[i] - 1] = 0;
found = true;
break;
}
}
if (!found)
return "Improper edge: " + edgeStr[i];
}
else {
boolean found = false;
for (int j = 0; j < edgeString.length; j++) {
if (edgeStr[i].equals(edgeString[j])) {
e[i] = j + 1;
ep[i] = (e[i] - 1) % 12 + 1;
eo[i] = (e[i] - 1) / 12 + 1;
ep1[ep[i] - 1] = 0;
eo1[ep[i] - 1] = 0;
found = true;
break;
}
}
if (!found)
return "Improper edge: " + edgeStr[i];
}
}
// fix the rest of eo1[]
// it is not really correct, since it represents only one of many states
// however, it is handled correctly in the solving algorithm
for (int i = 0; i < 12; i++) {
if (ep[i] == 0) { // where we the index to eo1[] is not known
if (eo[i] != 0) { // and the edge is oriented
boolean found = false;
for (int j = 0; j < 12; j++) {
if (eo1[j] == -1) {
eo1[j] = 0;
found = true;
break;
}
}
if (!found)
return "Bad edge format";
}
else { // the edge is not oriented
boolean found = false;
for (int j = 0; j < 12; j++) {
if (eo1[j] == -1) {
eo1[j] = 1;
found = true;
break;
}
}
if (!found)
return "Bad edge format";
}
}
}
for (int i = 0; i < 12; i++)
if (eo1[i] == -1)
return "Bad edge format";
// filling the corner locations and orientations to c[12], co*[12], cp*[12]
// co1[] is not completely filled (we do not know which corner is '@?')
// we fill locations that are not set later
for (int i = 0; i < 8; i++) {
c[i] = 0;
cp[i] = 0;
co[i] = 0;
cp1[i] = 1; // ok for unknown
co1[i] = -1; // fixed later for unknown
}
for (int i = 0; i < 8; i++) {
if (cornStr[i].equals("!")) {
c[i] = i + 1;
cp[i] = i + 1;
co[i] = 1;
cp1[i] = 0;
co1[i] = 0;
}
else if (cornStr[i].equals("@!")) {
c[i] = i + 1;
cp[i] = i + 1;
cp1[i] = 0;
co1[i] = 1;
}
else if (cornStr[i].equals("+!")) {
c[i] = i + 8 + 1;
cp[i] = i + 1;
co[i] = 2;
cp1[i] = 0;
co1[i] = 0;
}
else if (cornStr[i].equals("-!")) {
c[i] = i + 16 + 1;
cp[i] = i + 1;
co[i] = 3;
cp1[i] = 0;
co1[i] = 0;
}
else if (cornStr[i].equals("?")) {
co[i] = 1;
}
else if (cornStr[i].equals("@?")) {
}
else if (cornStr[i].equals("+?")) {
co[i] = 2;
}
else if (cornStr[i].equals("-?")) {
co[i] = 3;
}
else if (cornStr[i].charAt(0) == '@') {
boolean found = false;
for (int j = 0; j < cornString.length; j++) {
if (cornStr[i].substring(1).equals(cornString[j])) {
c[i] = j % 24 % 8 + 1;
cp[i] = (c[i] - 1) % 8 + 1;
cp1[cp[i] - 1] = 0;
co1[cp[i] - 1] = 1;
found = true;
break;
}
}
if (!found)
return "Improper corner: " + cornStr[i];
}
else if (cornStr[i].charAt(0) == '+') {
boolean found = false;
for (int j = 0; j < cornString.length; j++) {
if (cornStr[i].substring(1).equals(cornString[j])) {
c[i] = (j + 8) % 24 + 1;
cp[i] = (c[i] - 1) % 8 + 1;
co[i] = (c[i] - 1) / 8 + 1;
cp1[cp[i] - 1] = 0;
co1[cp[i] - 1] = 0;
found = true;
break;
}
}
if (!found)
return "Improper corner: " + cornStr[i];
}
else if (cornStr[i].charAt(0) == '-') {
boolean found = false;
for (int j = 0; j < cornString.length; j++) {
if (cornStr[i].substring(1).equals(cornString[j])) {
c[i] = (j + 16) % 24 + 1;
cp[i] = (c[i] - 1) % 8 + 1;
co[i] = (c[i] - 1) / 8 + 1;
cp1[cp[i] - 1] = 0;
co1[cp[i] - 1] = 0;
found = true;
break;
}
}
if (!found)
return "Improper corner: " + cornStr[i];
}
else {
boolean found = false;
for (int j = 0; j < cornString.length; j++) {
if (cornStr[i].equals(cornString[j])) {
c[i] = j % 24 + 1;
cp[i] = (c[i] - 1) % 8 + 1;
co[i] = (c[i] - 1) / 8 + 1;
cp1[cp[i] - 1] = 0;
co1[cp[i] - 1] = 0;
found = true;
break;
}
}
if (!found)
return "Improper corner: " + cornStr[i];
}
}
// fix the rest of co1[]
// it is not really correct, since it represents only one of many states
// however, it is handled correctly in the solving algorithm
for (int i = 0; i < 8; i++) {
if (cp[i] == 0) { // where we didn't know the index to the co1[]
if (co[i] != 0) { // and the corner is oriented
boolean found = false;
for (int j = 0; j < 8; j++) {
if (co1[j] == -1) {
co1[j] = 0;
found = true;
break;
}
}
if (!found)
return "Bad corner format";
}
else { // where the corner is not oriented
boolean found = false;
for (int j = 0; j < 8; j++) {
if (co1[j] == -1) {
co1[j] = 1;
found = true;
break;
}
}
if (!found)
return "Bad corner format";
}
}
}
for (int i = 0; i < 8; i++)
if (co1[i] == -1)
return "Bad corner format";
return "OK";
}
private String check() {
Comb edgeComb = new Comb(12);
edgeComb.fill(ep);
if (!edgeComb.checkVar())
return "Bad edges";
Comb cornComb = new Comb(8);
cornComb.fill(cp);
if (!cornComb.checkVar())
return "Bad corners";
int cornParity = cornComb.varParity(); // parity of corner permutation (0, 1, -1, -2)
int edgeParity = edgeComb.varParity(); // parity of edge permutation (0, 1, -1, -2)
// for the fully specified state the parities must be the same
// else must not depend on positions of min. 2 cubies of the same type
if (cornParity == -1)
return "Specify the position of the last corner";
if (edgeParity == -1)
return "Specify the position of the last edge";
if (cornParity >= 0 && edgeParity >= 0 && cornParity != edgeParity)
return "Swap any two edges or any two corners";
int flip = 0;
for (int i = 0; i < 12; i++) { // number of flips of edges
if (eo[i] == 0) {
flip = -1;
break;
}
flip += eo[i] - 1;
}
if (flip >= 0 && flip % 2 != 0) // must be even (edges are fliped in pairs)
return "Flip any edge";
int twist = 0;
for (int i = 0; i < 8; i++) { // number of clockwise twists of corners
if (co[i] == 0) {
twist = -1;
break;
}
twist += co[i] - 1;
}
if (twist >= 0 && twist % 3 != 0) // twist mod 3 must be 0
return "Twist any corner " + (twist % 3 == 1 ? "counter " : "") + "clock-wise";
return "OK";
}
private void preprocess() {
state.minimum = 101;
state.depth = 0;
// corner-twist processing
int cornOri = 0;
int cornPos = 0;
for (int i = 0; i < 8; i++) {
cornOri |= co1[i] << i;
cornPos |= cp1[i] << i;
}
cornOri |= cornPos << 8; // we have to specify which oriented corners are not positioned
state.coActive = cornOri;
state.cActive = cornPos;
PackCornTwist cornTwist = new PackCornTwist(cornOri);
cornTwist.fill(co);
state.cornTwist = cornTwist.pack();
// edge-flip processing
int edgeOri = 0;
int edgePos = 0;
for (int i = 0; i < 12; i++) {
edgeOri |= eo1[i] << i;
edgePos |= ep1[i] << i;
}
edgeOri |= edgePos << 12; // we have to specify which oriented edges are not positioned
state.eoActive = edgeOri;
state.eActive = edgePos;
PackEdgeFlip edgeFlip = new PackEdgeFlip(edgeOri);
edgeFlip.fill(eo);
state.edgeFlip = edgeFlip.pack();
// midge-location processing
int[] edgeTemp = new int[12];
for (int i = 0; i < 12; i++)
edgeTemp[i] = (ep[i] <= 8 ? 0 : 1); // 0 - Ux Dx, 1 - FR FL BR BL
PackEdgeLoc edgeLoc = new PackEdgeLoc(edgePos);
edgeLoc.fill(edgeTemp);
state.edgeLoc = edgeLoc.pack();
// corner-permutation processing
PackCornPerm cornPerm = new PackCornPerm(cornPos);
cornPerm.fill(cp);
state.cornPerm = cornPerm.pack();
// middle-slice-edge-permutation processing
for (int i = 0; i < 12; i++) {
switch (ep[i] - 1) {
case 8: // FR
edgeTemp[i] = 1;
break;
case 9: // FL
edgeTemp[i] = 2 - (edgePos >> 8 & 1); // FR
break;
case 10: // BR
edgeTemp[i] = 3 - (edgePos >> 8 & 1) - (edgePos >> 9 & 1); // FR FL
break;
case 11: // BL
edgeTemp[i] = 4 - (edgePos >> 8 & 1) - (edgePos >> 9 & 1) - (edgePos >> 10 & 1); // FR FL BR
break;
default:
edgeTemp[i] = 0;
}
}
PackMidgePos midgePos = new PackMidgePos(edgePos);
midgePos.fill(edgeTemp);
state.midgePos = midgePos.pack();
// up-slice-edge-permutation processing
for (int i = 0; i < 12; i++) {
switch (ep[i] - 1) {
case 0: // UF
edgeTemp[i] = 1;
break;
case 1: // UR
edgeTemp[i] = 2 - (edgePos & 1); // UF
break;
case 2: // UB
edgeTemp[i] = 3 - (edgePos & 1) - (edgePos >> 1 & 1); // UF UR
break;
case 3: // UL
edgeTemp[i] = 4 - (edgePos & 1) - (edgePos >> 1 & 1) - (edgePos >> 2 & 1); // UF UR UB
break;
default:
edgeTemp[i] = 0;
}
}
PackUEdgePos uEdgePos = new PackUEdgePos(edgePos);
uEdgePos.fill(edgeTemp);
state.uEdgePos = uEdgePos.pack();
// down-slice-edge-permutation processing
for (int i = 0; i < 12; i++) {
switch (ep[i] - 1) {
case 4: // DF
edgeTemp[i] = 1;
break;
case 5: // DR
edgeTemp[i] = 2 - (edgePos >> 4 & 1); // DF
break;
case 6: // DB
edgeTemp[i] = 3 - (edgePos >> 4 & 1) - (edgePos >> 5 & 1); // DF DR
break;
case 7: // DL
edgeTemp[i] = 4 - (edgePos >> 4 & 1) - (edgePos >> 5 & 1) - (edgePos >> 6 & 1); // DF DR DB
break;
default:
edgeTemp[i] = 0;
}
}
PackDEdgePos dEdgePos = new PackDEdgePos(edgePos);
dEdgePos.fill(edgeTemp);
state.dEdgePos = dEdgePos.pack();
}
private String input;
private int inputPos;
private String getInputToken() {
String s = "";
while (inputPos < input.length() && input.charAt(inputPos) <= 32)
inputPos++;
while (inputPos < input.length() && input.charAt(inputPos) > 32) {
s += input.charAt(inputPos);
inputPos++;
}
return s;
}
public static String padInt(int number, int length) {
String s = "" + number;
while (s.length() < length)
s = " " + s;
return s;
}
}