-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path666l2c.js
968 lines (895 loc) · 22.7 KB
/
666l2c.js
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
/* 666l2c.js - 666 last two centres solver
version 0.3 (2017-10-12)
Copyright 2017; you may do anything (anything!) not covered by copyright law (because this is not an
EULA, duh?). You may also copy, modify, publish, distribute, sublicense or sell copies of this code,
subject to the inclusion of this copyright notice in all copies or substantial portions of the code.
(basically the MIT licence but with cruft removed, because who reads that crap)
*/
'use strict';
function counter(A)
{
let counts = [];
for (let a of A) counts[a] = (counts[a] || 0) + 1;
return counts;
}
/* Combinatoric functions */
function C(n, k)
{
if (k < 0 || k > n) return 0;
if (k === 0 || k === n) return 1;
let c = 1;
for (let i = 0; i < k; i++)
{
c = (c * (n-i) / (i+1)) | 0;
}
return c;
}
function comb_to_index(l)
{
let bits = l.length;
let ones = 0;
for (let i = 0; i < bits; i++) ones += +(l[i] === 1);
let zeros = bits - ones;
if (zeros === 0 || ones === 0 || bits === 1) return 0;
let b = C(bits-1, ones);
let ind = 0;
for (let i = 0; zeros > 0 && ones > 0 && bits > 1; i++)
{
bits--;
if (l[i] === 0)
{
b = b * --zeros / bits;
}
else // l[i] === 1
{
ind += b;
b = b * ones-- / bits;
}
}
return ind;
}
function index_to_comb(ind, ones, bits)
{
let zeros = bits - ones;
let b = C(bits-1 , ones);
let l = [];
let n = bits-1;
for (let i = 0; i < n; i++)
{
bits--;
if (ind < b)
{
l.push(0);
b = b * --zeros / bits;
}
else
{
l.push(1);
ind -= b;
b = b * ones-- / bits;
}
}
l.push(ones);
return l;
}
function compose(A, B)
{
let C = [];
for (let i = 0; i < B.length; i++) C[i] = A[B[i]];
return C;
}
function permutation_from_cycle(cycle, n)
{
let perm = [];
for (let i = 0; i < n; i++) perm[i] = i;
for (let i = 0; i < cycle.length; i++)
{
perm[cycle[i]] = cycle[(i + 1) % cycle.length];
}
return perm;
}
function permutation_from_cycles(cycles, n)
{
let perm = [];
for (let i = 0; i < n; i++) perm[i] = i;
for (let cycle of cycles)
{
for (let i = 0; i < cycle.length; i++)
{
perm[cycle[i]] = cycle[(i + 1) % cycle.length];
}
}
return perm;
}
function unsparsify_list(d, n)
{
let l = Array(n).fill(0);
for (let k in d) l[k] = d[k];
return l;
}
function compose_move(move0, move1)
{
let move = [];
for (let i = 0; i < 4; i++) move[i] = compose(move0[i], move1[i]);
move[4] = move0[4] ^ move1[4];
return move;
}
/* The basic moves */
/*
e 0 0 1 1 e
e 0 0 1 1 e
U
e 3 3 2 2 e
e 3 3 2 2 e
e 4 4 5 5 e
e 4 4 5 5 e
F
e 7 7 6 6 e
e 7 7 6 6 e
Speffz ordering on the pieces. We use the following order of the orbits:
0: inner X (six pieces)
1: left oblique (U-3L-2B orbit; six pieces)
2: right oblique (U-3R-2B orbit; six pieces)
3: outer X (six pieces)
We also include a 4-bit bitfield to indicate how the alignment changes.
*/
let move_U = [
[3, 0, 1, 2, 4, 5, 6, 7],
[3, 0, 1, 2, 4, 5, 6, 7],
[3, 0, 1, 2, 4, 5, 6, 7],
[3, 0, 1, 2, 4, 5, 6, 7],
0
];
let move_2R = [
permutation_from_cycles([], 8),
permutation_from_cycles([[1, 5]], 8),
permutation_from_cycles([[2, 6]], 8),
permutation_from_cycles([[1, 5], [2, 6]], 8),
1
];
let move_3R = [
permutation_from_cycles([[1, 5], [2, 6]], 8),
permutation_from_cycles([[2, 6]], 8),
permutation_from_cycles([[1, 5]], 8),
permutation_from_cycles([], 8),
2
];
let move_4R = [
permutation_from_cycles([[0, 4], [3, 7]], 8),
permutation_from_cycles([[0, 4]], 8),
permutation_from_cycles([[3, 7]], 8),
permutation_from_cycles([], 8),
4
];
let move_5R = [
permutation_from_cycles([], 8),
permutation_from_cycles([[3, 7]], 8),
permutation_from_cycles([[0, 4]], 8),
permutation_from_cycles([[0, 4], [3, 7]], 8),
8
];
let move_2r = move_2R;
let move_3r = compose_move(move_2r, move_3R);
let move_4r = compose_move(move_3r, move_4R);
let move_5r = compose_move(move_4r, move_5R);
let moves = [move_U, move_2R, move_3R, move_4R, move_5R];
let move_names = ['U', '2R', '3R', '4R', '5R'];
let id = compose_move(move_2R, move_2R);
let moves_full = [];
for (let i = 0; i < moves.length; i++)
{
moves_full[i] = [id];
for (let j = 1; j < 4; j++) moves_full[i][j] = compose_move(moves_full[i][j-1], moves[i]);
}
function generate_random_state()
{
let state = [];
for (let i = 0; i < 4; i++) state[i] = index_to_comb(Math.floor(Math.random()*C(8, 4)), 4, 8);
state[4] = Math.floor(Math.random()*16);
return state;
}
let solved_state = [[0,0,0,0,1,1,1,1],[0,0,0,0,1,1,1,1],[0,0,0,0,1,1,1,1],[0,0,0,0,1,1,1,1],0];
/* Human interface stuff */
function normalise_move_sequence(move_sequence)
{
let new_move_sequence = [];
let alignment = 0;
for (let [m, r] of move_sequence)
{
if (m === 0)
{
new_move_sequence.push([m, r]);
}
else
{
let parity = ((alignment >> (m-1)) & 1);
new_move_sequence.push([m, r - 2*parity]);
alignment ^= (1 << (m-1));
}
}
return new_move_sequence;
}
function stringify_move_sequence(move_sequence)
{
let suffixes = ["0", "", "2", "'"];
let s = [];
for (let [m, r] of normalise_move_sequence(move_sequence))
{
r = (r + 4) % 4;
s.push(move_names[m] + suffixes[r]);
}
return s.join(' ');
}
function print_move_sequence(move_sequence)
{
console.log(stringify_move_sequence(move_sequence));
}
function print_state_ansi(state)
{
let colours = ['\x1b[42m \x1b[0m', '\x1b[48;5;208m \x1b[0m', ' '];
let u = [
state[3][0], state[1][0], state[2][1], state[3][1],
state[2][0], state[0][0], state[0][1], state[1][1],
state[1][3], state[0][3], state[0][2], state[2][2],
state[3][3], state[2][3], state[1][2], state[3][2],
];
let f = [
state[3][4], state[1][4], state[2][5], state[3][5],
state[2][4], state[0][4], state[0][5], state[1][5],
state[1][7], state[0][7], state[0][6], state[2][6],
state[3][7], state[2][7], state[1][6], state[3][6],
];
for (let i = 0; i < 4; i++)
{
for (let j = 0; j < 4; j++)
{
putstr(colours[u[i*4+j]]);
}
print('');
}
print('==========');
for (let i = 0; i < 4; i++)
{
for (let j = 0; j < 4; j++)
{
putstr(colours[f[i*4+j]]);
}
print('');
}
}
function print_state(state)
{
let colours = ['. ', '# ', ' '];
let u = [
state[3][0], state[1][0], state[2][1], state[3][1],
state[2][0], state[0][0], state[0][1], state[1][1],
state[1][3], state[0][3], state[0][2], state[2][2],
state[3][3], state[2][3], state[1][2], state[3][2],
];
let f = [
state[3][4], state[1][4], state[2][5], state[3][5],
state[2][4], state[0][4], state[0][5], state[1][5],
state[1][7], state[0][7], state[0][6], state[2][6],
state[3][7], state[2][7], state[1][6], state[3][6],
];
for (let i = 0; i < 4; i++)
{
for (let j = 0; j < 4; j++)
{
putstr(colours[u[i*4+j]]);
}
print('');
}
print('=======');
for (let i = 0; i < 4; i++)
{
for (let j = 0; j < 4; j++)
{
putstr(colours[f[i*4+j]]);
}
print('');
}
}
function arrayify_state(state)
{
let u = [
[state[3][0], state[1][0], state[2][1], state[3][1]],
[state[2][0], state[0][0], state[0][1], state[1][1]],
[state[1][3], state[0][3], state[0][2], state[2][2]],
[state[3][3], state[2][3], state[1][2], state[3][2]],
];
let f = [
[state[3][4], state[1][4], state[2][5], state[3][5]],
[state[2][4], state[0][4], state[0][5], state[1][5]],
[state[1][7], state[0][7], state[0][6], state[2][6]],
[state[3][7], state[2][7], state[1][6], state[3][6]],
];
return [u, f];
}
function apply_move_sequence(state, move_sequence)
{
for (let [m, r] of move_sequence)
{
for (let i = 0; i < r; i++) state = compose_move(state, moves[m]);
}
return state;
}
function generate_random_state_scramble(slice)
{
let S = generate_random_state();
if (!slice) S[4] = 0;
return invert_move_sequence(solve(S));
}
function invert_move_sequence(move_sequence)
{
let out = [];
for (let [m, r] of move_sequence)
{
out.unshift([m, m === 0 ? (4-r) % 4 : r]);
}
return out;
}
function generate_state_from_template(template)
{
// 0 = U, 1 = F, 2 = unspecified
let S = [, , , , 0];
for (let orbit_ind = 0; orbit_ind < 4; orbit_ind++)
{
let orbit_template = template[orbit_ind];
let u = orbit_template.filter(x => x === 0).length;
let f = orbit_template.filter(x => x === 1).length;
if (u > 4 || f > 4)
{
// invalid template, so do whatever
S[orbit_ind] = index_to_comb(Math.floor(Math.random() * 70), 4, 8);
continue;
}
let remaining = index_to_comb(Math.floor(Math.random() * C(8-u-f,4-f)), 4-f, 8-u-f);
S[orbit_ind] = orbit_template.slice();
for (let i = 0, j = 0; i < 8; i++)
{
if (S[orbit_ind][i] < 2) continue;
S[orbit_ind][i] = remaining[j];
j++;
}
}
return S;
}
function generate_scramble_sequence_from_template(template)
{
let S = generate_state_from_template(template);
//console.log(arrayify_state(S));
return stringify_sign(invert_move_sequence(solve_big(S)));
}
/* Alg scoring
The move sequence is converted to block turns first, with penalties applied to some moves.
Base value: 10
Half turn: +2
Non-outer block turn: +5
*/
function convert_move_sequence(move_sequence)
{
let ms = normalise_move_sequence(move_sequence);
let obtm_ms = [];
let i = 0;
while (i < ms.length)
{
let [m, r] = ms[i];
// U moves never need changing
if (m === 0)
{
obtm_ms.push([m, r]);
i++;
continue;
}
let layers = [0, 0, 0, 0];
while (i < ms.length && ms[i][0] !== 0)
{
[m, r] = ms[i];
layers[m-1] += r;
i++;
}
if (layers[3] !== 0) obtm_ms.push([4, layers[3]]);
if (layers[2] !== layers[3]) obtm_ms.push([3, layers[2]-layers[3]]);
if (layers[1] !== layers[2]) obtm_ms.push([2, layers[1]-layers[2]]);
if (layers[0] !== layers[1]) obtm_ms.push([1, layers[0]-layers[1]]);
}
return obtm_ms;
}
function stringify_sign(move_sequence)
{
let ms = normalise_move_sequence(move_sequence);
let s = [];
let i = 0;
let suffixes = {'-2': "2'", '-1': "'", '0': '0', '1': '', '2': '2'};
while (i < ms.length)
{
let [m, r] = ms[i];
if (m === 0)
{
s.push('U' + ['0', '', '2', "'"][r]);
i++;
continue;
}
let layers = [0, 0, 0, 0];
while (i < ms.length && ms[i][0] !== 0)
{
[m, r] = ms[i];
layers[m-1] += r;
i++;
}
let nonzero = layers.map(x => !!x);
let first = nonzero.indexOf(true), last = nonzero.lastIndexOf(true);
// no-op
if (first === -1) continue;
// single layer
if (first === last)
{
r = layers[first];
switch (first)
{
case 0: s.push('r' + suffixes[r]); break;
case 1: s.push('3R' + suffixes[r]); break;
case 2: s.push('3L' + suffixes[-r]); break;
case 3: s.push('l' + suffixes[-r]); break;
}
continue;
}
// single block
if (layers.slice(first, last+1).every(x => (x === layers[first])))
{
r = layers[first];
// every layer
if (first === 0 && last === 3)
{
s.push('x' + suffixes[r]);
continue;
}
// right outer block
if (first === 0)
{
s.push((last+2) + 'r' + suffixes[r]);
continue;
}
// left outer block
if (last === 3)
{
s.push((5-first) + 'l' + suffixes[-r]);
continue;
}
// not an outer block (i.e. 3-4r)
s.push(`${first+2}-${last+2}r` + suffixes[r]);
continue;
}
if (layers[3] !== layers[2]) s.push('l' + suffixes[layers[2]-layers[3]]);
if (layers[2] !== 0) s.push('3l' + suffixes[-layers[2]]);
if (layers[1] !== 0) s.push('3r' + suffixes[layers[1]]);
if (layers[0] !== layers[1]) s.push('r' + suffixes[layers[0]-layers[1]]);
}
return s.join(' ');
}
function stringify_obtm_move_sequence(move_sequence)
{
let s = [];
let move_names = ['U', 'r', '3r', '4r', 'x'];
for (let [m, r] of move_sequence)
{
let suffix = '';
if (m === 0) suffix = ['0', '', '2', "'"][r];
else
{
if (r > 1) suffix = r + '';
else if (r === -1) suffix = "'";
else if (r < -1) suffix = (-r) + "'";
}
s.push(move_names[m] + suffix);
}
return s.join(' ');
}
function score_move_sequence(move_sequence)
{
let ms = convert_move_sequence(move_sequence);
if (ms.length > 0 && ms[0][0] === 0) return ms.length-1;
return ms.length;
}
function score_move_sequence2(move_sequence)
{
let score = 0;
let sign = stringify_sign(move_sequence).replace(/2'/g, '2').split(' ');
let first = true;
for (let move of sign)
{
let move_score = 10;
let r = 1;
switch (move[move.length-1])
{
case "'": r = -1; move = move.substring(0, move.length-1); break;
case '2': r = 2; move = move.substring(0, move.length-1); move_score += 2; break;
}
switch (move)
{
case 'U': if (first) move_score = 0; break;
case '3R':
case '3L':
case '3-4r': move_score += 5; break;
case 'x': move_score = 0; break;
//default: move_score = 1000000; break;
}
score += move_score;
first = false;
}
return score;
}
function choose_nice_algs(generator)
{
let optimal_sstm;
let lowest = 999999;
const THRESHOLD = 18;
let list = [];
while (true)
{
let {value: sol, done} = generator.next();
if (optimal_sstm === undefined) optimal_sstm = sol.length;
if (sol.length > optimal_sstm + 6) break;
let score = score_move_sequence2(sol);
if (score < lowest)
{
lowest = score;
list = list.filter(x => (x[1] - lowest < THRESHOLD));
}
if (score - lowest < THRESHOLD)
{
list.push([sol, score]);
}
}
list.sort((x, y) => (x[1] - y[1]));
return list.map(x => x[0]);
}
/* Solver logic
We generate move tables for the four orbits of centres separately, then mash them together.
Pruning tables used:
- inner X + outer X + all parity
(78400 states)
- left oblique + right oblique + all parity
(78400 states)
- inner X + left oblique + right oblique
(343000 states)
- outer X + right oblique + left oblique
(343000 states; exactly the same as the previous table)
*/
function index_full(state)
{
let index0 = (comb_to_index(state[0]) +
70 * comb_to_index(state[3]) +
4900 * state[4]);
let index1 = (comb_to_index(state[1]) +
70 * comb_to_index(state[2]) +
4900 * state[4]);
return [index0, index1];
}
function index_big(state)
{
let index0 = (comb_to_index(state[0]) +
70 * comb_to_index(state[3]) +
4900 * state[4]);
let index1 = (comb_to_index(state[1]) +
70 * comb_to_index(state[2]) +
4900 * state[4]);
let index2 = (comb_to_index(state[0]) +
70 * comb_to_index(state[1]) +
4900 * comb_to_index(state[2]));
let index3 = (comb_to_index(state[3]) +
70 * comb_to_index(state[2]) +
4900 * comb_to_index(state[1]));
return [index0, index1, index2, index3];
}
function solve(state)
{
let mtables = [generate_xparity_mtable(), generate_obparity_mtable()];
let ptables = [generate_xparity_ptable(), generate_obparity_ptable()];
return ida_solve(index_full(state), mtables, ptables);
}
function solve_gen(state)
{
let mtables = [generate_xparity_mtable(), generate_obparity_mtable()];
let ptables = [generate_xparity_ptable(), generate_obparity_ptable()];
return ida_solve_gen(index_full(state), mtables, ptables);
}
function solve_big(state)
{
let mtables = [generate_xparity_mtable(), generate_obparity_mtable()];
mtables = mtables.concat(generate_xoo_mtables());
let ptables = [generate_xparity_ptable(), generate_obparity_ptable()];
ptables[2] = ptables[3] = generate_xoo_ptable();
return ida_solve(index_big(state), mtables, ptables);
}
function solve_gen_big(state)
{
let mtables = [generate_xparity_mtable(), generate_obparity_mtable()];
mtables = mtables.concat(generate_xoo_mtables());
let ptables = [generate_xparity_ptable(), generate_obparity_ptable()];
ptables[2] = ptables[3] = generate_xoo_ptable();
return ida_solve_gen(index_big(state), mtables, ptables);
}
let tables = {};
function generate_orbit_mtables()
{
if (tables.orbit_mtables) return tables.orbit_mtables;
let mtables = [];
let C8_4 = C(8, 4);
for (let i = 0; i < 4; i++)
{
mtables[i] = Array(C8_4);
for (let j = 0; j < C8_4; j++)
{
mtables[i][j] = Array(moves.length);
}
}
for (let ind = 0; ind < C8_4; ind++)
{
let comb = index_to_comb(ind, 4, 8);
for (let i = 0; i < 4; i++)
{
for (let m = 0; m < moves.length; m++)
{
let new_comb = compose(comb, moves[m][i]);
mtables[i][ind][m] = comb_to_index(new_comb);
}
}
}
return tables.orbit_mtables = mtables;
}
function generate_xparity_mtable()
{
if (tables.xparity_mtable) return tables.xparity_mtable;
// inner X, outer X, parity
let mtable = [];
let orbit_mtables = generate_orbit_mtables();
for (let i = 0; i < 70; i++)
{
for (let j = 0; j < 70; j++)
{
for (let p = 0; p < 16; p++)
{
let ind = i + 70*j + 4900*p;
mtable[ind] = [];
for (let m = 0; m < moves.length; m++)
{
let I = orbit_mtables[0][i][m];
let J = orbit_mtables[3][j][m];
let P = p ^ moves[m][4];
mtable[ind][m] = I + 70*J + 4900*P;
}
}
}
}
return tables.xparity_mtable = mtable;
}
function generate_obparity_mtable()
{
if (tables.obparity_mtable) return tables.obparity_mtable;
// left oblique, right oblique, parity
let mtable = [];
let orbit_mtables = generate_orbit_mtables();
for (let i = 0; i < 70; i++)
{
for (let j = 0; j < 70; j++)
{
for (let p = 0; p < 16; p++)
{
let ind = i + 70*j + 4900*p;
mtable[ind] = [];
for (let m = 0; m < moves.length; m++)
{
let I = orbit_mtables[1][i][m];
let J = orbit_mtables[2][j][m];
let P = p ^ moves[m][4];
mtable[ind][m] = I + 70*J + 4900*P;
}
}
}
}
return tables.obparity_mtable = mtable;
}
function generate_xoo_mtables()
{
if (tables.xoo_mtables) return tables.xoo_mtables;
// two tables!
// 0: inner x, left oblique, right oblique
// 1: outer x, right oblique, left oblique
// (we use the opposite order in the second table because then we can use automorphism magic
// to generate only one pruning table)
let mtable0 = [], mtable1 = [];
let orbit_mtables = generate_orbit_mtables();
for (let i = 0; i < 70; i++)
{
for (let j = 0; j < 70; j++)
{
for (let k = 0; k < 70; k++)
{
let ind = i + 70*j + 4900*k;
mtable0[ind] = [];
mtable1[ind] = [];
for (let m = 0; m < moves.length; m++)
{
let I = orbit_mtables[0][i][m];
let J = orbit_mtables[1][j][m];
let K = orbit_mtables[2][k][m];
let m1 = m !== 0 ? ((m-1)^1)+1 : 0;
mtable0[ind][m] = mtable1[ind][m1] = I + 70*J + 4900*K;
}
}
}
}
return tables.xoo_mtables = [mtable0, mtable1];
}
function generate_xparity_ptable()
{
if (tables.xparity_ptable) return tables.xparity_ptable;
let mtable = generate_xparity_mtable();
let goal_states = [0];
return tables.xparity_ptable = bfs(mtable, goal_states);
}
function generate_partialxparity_ptable()
{
if (tables.partialxparity_ptable) return tables.partialxparity_ptable;
let mtable = generate_xparity_mtable();
let goal_states = [];
for (let i = 0; i < 70; i++) goal_states[i] = 70*i;
return tables.partialxparity_ptable = bfs(mtable, goal_states);
}
function generate_obparity_ptable()
{
if (tables.obparity_ptable) return tables.obparity_ptable;
let mtable = generate_obparity_mtable();
let goal_states = [0];
return tables.obparity_ptable = bfs(mtable, goal_states);
}
function generate_partialobparity_ptable()
{
if (tables.partialobparity_ptable) return tables.partialobparity_ptable;
let mtable = generate_obparity_mtable();
let goal_states = [];
for (let i = 0; i < 70; i++)
{
let c = index_to_comb(i, 4, 8);
let j = comb_to_index([c[3], c[0], c[1], c[2], c[7], c[4], c[5], c[6]]);
goal_states[i] = i + 70*j;
}
return tables.partialobparity_ptable = bfs(mtable, goal_states);
}
function generate_xoo_ptable()
{
if (tables.xoo_ptable) return tables.xoo_ptable;
let mtable = generate_xoo_mtables()[0];
return tables.xoo_ptable = bfs(mtable, [0]);
}
function binary_search(A, x)
{
let lo = 0, hi = A.length-1;
while (hi - lo > 1)
{
// invariants: hi - lo >= 2; x > A[lo-1]; x < A[hi+1]
let mid = (lo + hi) >> 1; // lo < mid < hi
if (x > A[mid]) lo = mid + 1;
else hi = mid;
}
return x === A[lo] || x === A[hi];
}
function bfs(mtable, goal_states)
{
let N = mtable.length;
let nmoves = mtable[0].length;
let ptable = Array(N).fill(-1);
let queue = goal_states.slice(), new_queue = [];
let depth = 0;
while (queue.length > 0)
{
new_queue.length = 0;
for (let state of queue)
{
if (ptable[state] !== -1) continue;
ptable[state] = depth;
for (let move_index = 0; move_index < nmoves; move_index++)
{
let new_state = mtable[state][move_index];
while (new_state != state)
{
new_queue.push(new_state);
new_state = mtable[new_state][move_index];
}
}
}
[queue, new_queue] = [new_queue, queue];
depth += 1;
}
return ptable;
}
function ida_solve(indices, mtables, ptables)
{
let ncoords = indices.length;
let bound = 0;
for (let i = 0; i < ncoords; i++) bound = Math.max(bound, ptables[i][indices[i]]);
while (true)
{
let path = ida_search(indices, mtables, ptables, bound, -1);
if (path !== undefined) return path;
bound++;
}
}
function ida_search(indices, mtables, ptables, bound, last)
{
let ncoords = indices.length;
let nmoves = mtables[0][0].length;
let heuristic = 0;
for (let i = 0; i < ncoords; i++) heuristic = Math.max(heuristic, ptables[i][indices[i]]);
if (heuristic > bound) return;
if (bound === 0 || heuristic === 0) return [];
for (let m = 0; m < nmoves; m++)
{
if (m === last) continue;
let new_indices = indices.slice();
for (let c = 0; c < ncoords; c++) new_indices[c] = mtables[c][indices[c]][m];
let r = 1;
while (indices.some((_, i) => indices[i] != new_indices[i]))
{
let subpath = ida_search(new_indices, mtables, ptables, bound-1, m);
if (subpath !== undefined) return [[m, r]].concat(subpath);
for (let c = 0; c < ncoords; c++)
{
new_indices[c] = mtables[c][new_indices[c]][m];
}
r++;
}
}
return;
}
function* ida_solve_gen(indices, mtables, ptables)
{
let ncoords = indices.length;
let bound = 0;
for (let i = 0; i < ncoords; i++) bound = Math.max(bound, ptables[i][indices[i]]);
while (true)
{
yield* ida_search_gen(indices, mtables, ptables, bound, -1);
bound++;
}
}
function* ida_search_gen(indices, mtables, ptables, bound, last)
{
let ncoords = indices.length;
let nmoves = mtables[0][0].length;
let heuristic = 0;
for (let i = 0; i < ncoords; i++) heuristic = Math.max(heuristic, ptables[i][indices[i]]);
if (heuristic > bound) return;
if (bound === 0)
{
yield [];
return;
}
if (heuristic === 0) return;
for (let m = 0; m < nmoves; m++)
{
if (m === last) continue;
if (m > 0 && last > 0 && m < last) continue; // enforce order on commuting moves
let new_indices = indices.slice();
for (let c = 0; c < ncoords; c++) new_indices[c] = mtables[c][indices[c]][m];
let r = 1;
while (indices.some((_, i) => indices[i] != new_indices[i]))
{
let subpath_gen = ida_search_gen(new_indices, mtables, ptables, bound-1, m);
while (true)
{
let {value: subpath, done} = subpath_gen.next();
if (done) break;
yield [[m, r]].concat(subpath);
}
for (let c = 0; c < ncoords; c++)
{
new_indices[c] = mtables[c][new_indices[c]][m];
}
r++;
}
}
return;
}