@cubingcenter/scrambler 0.6.3 → 0.8.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +17 -4
- package/dist/core/api.d.ts.map +1 -1
- package/dist/core/api.js +10 -2
- package/dist/puzzles/cube2/solver/browser-loader.d.ts +4 -7
- package/dist/puzzles/cube2/solver/browser-loader.d.ts.map +1 -1
- package/dist/puzzles/cube2/solver/browser-loader.js +42 -14
- package/dist/puzzles/cube2/solver/generated-assets.d.ts.map +1 -1
- package/dist/puzzles/cube2/solver/generated-assets.js +8 -3
- package/dist/puzzles/cube2/solver/search.d.ts.map +1 -1
- package/dist/puzzles/cube2/solver/search.js +3 -3
- package/dist/puzzles/cube2/solver/tables.d.ts +6 -0
- package/dist/puzzles/cube2/solver/tables.d.ts.map +1 -1
- package/dist/puzzles/cube2/solver/tables.js +16 -2
- package/dist/puzzles/cube3/index.d.ts +1 -0
- package/dist/puzzles/cube3/index.d.ts.map +1 -1
- package/dist/puzzles/cube3/moves.d.ts.map +1 -1
- package/dist/puzzles/cube3/moves.js +104 -18
- package/dist/puzzles/cube3/scrambler.d.ts +16 -1
- package/dist/puzzles/cube3/scrambler.d.ts.map +1 -1
- package/dist/puzzles/cube3/scrambler.js +1 -1
- package/dist/puzzles/cube3/solver/browser-loader.d.ts.map +1 -1
- package/dist/puzzles/cube3/solver/browser-loader.js +48 -22
- package/dist/puzzles/cube3/solver/generated-assets.d.ts.map +1 -1
- package/dist/puzzles/cube3/solver/generated-assets.js +9 -3
- package/dist/puzzles/cube3/solver/index.d.ts +5 -3
- package/dist/puzzles/cube3/solver/index.d.ts.map +1 -1
- package/dist/puzzles/cube3/solver/index.js +2 -1
- package/dist/puzzles/cube3/solver/node-loader.js +9 -2
- package/dist/puzzles/cube3/solver/phase1.d.ts +37 -0
- package/dist/puzzles/cube3/solver/phase1.d.ts.map +1 -1
- package/dist/puzzles/cube3/solver/phase1.js +156 -15
- package/dist/puzzles/cube3/solver/phase2.d.ts +75 -0
- package/dist/puzzles/cube3/solver/phase2.d.ts.map +1 -1
- package/dist/puzzles/cube3/solver/phase2.js +279 -68
- package/dist/puzzles/cube3/solver/search-utils.d.ts +1 -1
- package/dist/puzzles/cube3/solver/search-utils.d.ts.map +1 -1
- package/dist/puzzles/cube3/solver/symmetry.d.ts +24 -0
- package/dist/puzzles/cube3/solver/symmetry.d.ts.map +1 -0
- package/dist/puzzles/cube3/solver/symmetry.js +74 -0
- package/dist/puzzles/cube3/solver/tables.d.ts +6 -1
- package/dist/puzzles/cube3/solver/tables.d.ts.map +1 -1
- package/dist/puzzles/cube3/solver/tables.js +14 -12
- package/dist/puzzles/cube3/validation.d.ts.map +1 -1
- package/dist/puzzles/cube3/validation.js +15 -7
- package/dist/puzzles/cube4/browser-worker-pool.d.ts +82 -0
- package/dist/puzzles/cube4/browser-worker-pool.d.ts.map +1 -0
- package/dist/puzzles/cube4/browser-worker-pool.js +205 -0
- package/dist/puzzles/cube4/browser-worker.d.ts +2 -0
- package/dist/puzzles/cube4/browser-worker.d.ts.map +1 -0
- package/dist/puzzles/cube4/browser-worker.js +31 -0
- package/dist/puzzles/cube4/index.d.ts +10 -2
- package/dist/puzzles/cube4/index.d.ts.map +1 -1
- package/dist/puzzles/cube4/index.js +6 -2
- package/dist/puzzles/cube4/moves.d.ts +5 -0
- package/dist/puzzles/cube4/moves.d.ts.map +1 -1
- package/dist/puzzles/cube4/moves.js +212 -11
- package/dist/puzzles/cube4/reduction/browser-loader.d.ts +29 -1
- package/dist/puzzles/cube4/reduction/browser-loader.d.ts.map +1 -1
- package/dist/puzzles/cube4/reduction/browser-loader.js +227 -32
- package/dist/puzzles/cube4/reduction/center2.d.ts +44 -3
- package/dist/puzzles/cube4/reduction/center2.d.ts.map +1 -1
- package/dist/puzzles/cube4/reduction/center2.js +152 -10
- package/dist/puzzles/cube4/reduction/edge3.d.ts +4 -0
- package/dist/puzzles/cube4/reduction/edge3.d.ts.map +1 -1
- package/dist/puzzles/cube4/reduction/edge3.js +23 -3
- package/dist/puzzles/cube4/reduction/finish3.d.ts +53 -0
- package/dist/puzzles/cube4/reduction/finish3.d.ts.map +1 -0
- package/dist/puzzles/cube4/reduction/finish3.js +393 -0
- package/dist/puzzles/cube4/reduction/generated/c3_pair_dist.bin +1 -0
- package/dist/puzzles/cube4/reduction/generated/center2-manifest.json +36 -0
- package/dist/puzzles/cube4/reduction/generated/center2-pruning-wide.bin +1 -0
- package/dist/puzzles/cube4/reduction/generated/hc-distances.bin +1 -0
- package/dist/puzzles/cube4/reduction/generated/hc-manifest.json +44 -0
- package/dist/puzzles/cube4/reduction/generated/hc-move.bin +0 -0
- package/dist/puzzles/cube4/reduction/hc-pdb.d.ts +52 -0
- package/dist/puzzles/cube4/reduction/hc-pdb.d.ts.map +1 -0
- package/dist/puzzles/cube4/reduction/hc-pdb.js +220 -0
- package/dist/puzzles/cube4/reduction/node-loader.d.ts +15 -1
- package/dist/puzzles/cube4/reduction/node-loader.d.ts.map +1 -1
- package/dist/puzzles/cube4/reduction/node-loader.js +199 -25
- package/dist/puzzles/cube4/reduction/pair-transversal.d.ts +28 -0
- package/dist/puzzles/cube4/reduction/pair-transversal.d.ts.map +1 -0
- package/dist/puzzles/cube4/reduction/pair-transversal.js +107 -0
- package/dist/puzzles/cube4/reduction/pairing-coordinate.js +5 -5
- package/dist/puzzles/cube4/reduction/phase3-search.d.ts +116 -7
- package/dist/puzzles/cube4/reduction/phase3-search.d.ts.map +1 -1
- package/dist/puzzles/cube4/reduction/phase3-search.js +458 -32
- package/dist/puzzles/cube4/reduction/rotation-fix.d.ts +28 -0
- package/dist/puzzles/cube4/reduction/rotation-fix.d.ts.map +1 -0
- package/dist/puzzles/cube4/reduction/rotation-fix.js +212 -0
- package/dist/puzzles/cube4/reduction/solve.d.ts +172 -2
- package/dist/puzzles/cube4/reduction/solve.d.ts.map +1 -1
- package/dist/puzzles/cube4/reduction/solve.js +1201 -180
- package/dist/puzzles/cube4/scramble-cache.d.ts +93 -0
- package/dist/puzzles/cube4/scramble-cache.d.ts.map +1 -0
- package/dist/puzzles/cube4/scramble-cache.js +150 -0
- package/dist/puzzles/cube4/scrambler.d.ts +201 -6
- package/dist/puzzles/cube4/scrambler.d.ts.map +1 -1
- package/dist/puzzles/cube4/scrambler.js +414 -176
- package/dist/puzzles/cube4/state.d.ts.map +1 -1
- package/dist/puzzles/cube4/state.js +4 -1
- package/dist/puzzles/cube4/worker-pool.d.ts +139 -0
- package/dist/puzzles/cube4/worker-pool.d.ts.map +1 -0
- package/dist/puzzles/cube4/worker-pool.js +405 -0
- package/dist/puzzles/cube4/worker-protocol.d.ts +58 -0
- package/dist/puzzles/cube4/worker-protocol.d.ts.map +1 -0
- package/dist/puzzles/cube4/worker-protocol.js +58 -0
- package/dist/puzzles/cube4/worker.d.ts +26 -0
- package/dist/puzzles/cube4/worker.d.ts.map +1 -0
- package/dist/puzzles/cube4/worker.js +49 -0
- package/dist/puzzles/pyraminx/solver/browser-loader.d.ts +4 -8
- package/dist/puzzles/pyraminx/solver/browser-loader.d.ts.map +1 -1
- package/dist/puzzles/pyraminx/solver/browser-loader.js +44 -15
- package/dist/puzzles/pyraminx/solver/generated-assets.d.ts.map +1 -1
- package/dist/puzzles/pyraminx/solver/generated-assets.js +8 -3
- package/dist/puzzles/pyraminx/solver/search.d.ts.map +1 -1
- package/dist/puzzles/pyraminx/solver/search.js +3 -6
- package/dist/puzzles/pyraminx/solver/tables.d.ts +6 -0
- package/dist/puzzles/pyraminx/solver/tables.d.ts.map +1 -1
- package/dist/puzzles/pyraminx/solver/tables.js +17 -0
- package/dist/puzzles/shared/compressed-fetch.d.ts +37 -1
- package/dist/puzzles/shared/compressed-fetch.d.ts.map +1 -1
- package/dist/puzzles/shared/compressed-fetch.js +226 -31
- package/dist/puzzles/shared/load-once.d.ts +16 -0
- package/dist/puzzles/shared/load-once.d.ts.map +1 -0
- package/dist/puzzles/shared/load-once.js +11 -0
- package/dist/puzzles/skewb/solver/browser-loader.d.ts +4 -11
- package/dist/puzzles/skewb/solver/browser-loader.d.ts.map +1 -1
- package/dist/puzzles/skewb/solver/browser-loader.js +44 -16
- package/dist/puzzles/skewb/solver/generated-assets.d.ts.map +1 -1
- package/dist/puzzles/skewb/solver/generated-assets.js +8 -3
- package/dist/puzzles/skewb/solver/search.js +3 -3
- package/dist/puzzles/skewb/solver/tables.d.ts +6 -0
- package/dist/puzzles/skewb/solver/tables.d.ts.map +1 -1
- package/dist/puzzles/skewb/solver/tables.js +17 -0
- package/dist/puzzles/sq1/solver/browser-loader.d.ts.map +1 -1
- package/dist/puzzles/sq1/solver/browser-loader.js +58 -26
- package/dist-cjs/core/api.js +10 -2
- package/dist-cjs/puzzles/cube2/solver/browser-loader.js +42 -14
- package/dist-cjs/puzzles/cube2/solver/generated-assets.js +8 -3
- package/dist-cjs/puzzles/cube2/solver/search.js +2 -2
- package/dist-cjs/puzzles/cube2/solver/tables.js +17 -3
- package/dist-cjs/puzzles/cube3/moves.js +104 -18
- package/dist-cjs/puzzles/cube3/scrambler.js +1 -1
- package/dist-cjs/puzzles/cube3/solver/browser-loader.js +48 -22
- package/dist-cjs/puzzles/cube3/solver/generated-assets.js +9 -3
- package/dist-cjs/puzzles/cube3/solver/index.js +8 -1
- package/dist-cjs/puzzles/cube3/solver/node-loader.js +9 -2
- package/dist-cjs/puzzles/cube3/solver/phase1.js +157 -15
- package/dist-cjs/puzzles/cube3/solver/phase2.js +276 -65
- package/dist-cjs/puzzles/cube3/solver/symmetry.js +80 -0
- package/dist-cjs/puzzles/cube3/solver/tables.js +14 -12
- package/dist-cjs/puzzles/cube3/validation.js +15 -7
- package/dist-cjs/puzzles/cube4/browser-worker-pool.js +211 -0
- package/dist-cjs/puzzles/cube4/index.js +23 -1
- package/dist-cjs/puzzles/cube4/moves.js +213 -11
- package/dist-cjs/puzzles/cube4/reduction/browser-loader.js +227 -32
- package/dist-cjs/puzzles/cube4/reduction/center2.js +155 -10
- package/dist-cjs/puzzles/cube4/reduction/edge3.js +24 -3
- package/dist-cjs/puzzles/cube4/reduction/finish3.js +398 -0
- package/dist-cjs/puzzles/cube4/reduction/generated/c3_pair_dist.bin +1 -0
- package/dist-cjs/puzzles/cube4/reduction/generated/center2-manifest.json +36 -0
- package/dist-cjs/puzzles/cube4/reduction/generated/center2-pruning-wide.bin +1 -0
- package/dist-cjs/puzzles/cube4/reduction/generated/hc-distances.bin +1 -0
- package/dist-cjs/puzzles/cube4/reduction/generated/hc-manifest.json +44 -0
- package/dist-cjs/puzzles/cube4/reduction/generated/hc-move.bin +0 -0
- package/dist-cjs/puzzles/cube4/reduction/hc-pdb.js +232 -0
- package/dist-cjs/puzzles/cube4/reduction/node-loader.js +200 -25
- package/dist-cjs/puzzles/cube4/reduction/pair-transversal.js +112 -0
- package/dist-cjs/puzzles/cube4/reduction/pairing-coordinate.js +5 -5
- package/dist-cjs/puzzles/cube4/reduction/phase3-search.js +460 -33
- package/dist-cjs/puzzles/cube4/reduction/rotation-fix.js +216 -0
- package/dist-cjs/puzzles/cube4/reduction/solve.js +1203 -172
- package/dist-cjs/puzzles/cube4/scramble-cache.js +156 -0
- package/dist-cjs/puzzles/cube4/scrambler.js +416 -174
- package/dist-cjs/puzzles/cube4/state.js +4 -1
- package/dist-cjs/puzzles/cube4/worker-pool.js +447 -0
- package/dist-cjs/puzzles/cube4/worker-protocol.js +62 -0
- package/dist-cjs/puzzles/pyraminx/solver/browser-loader.js +44 -15
- package/dist-cjs/puzzles/pyraminx/solver/generated-assets.js +8 -3
- package/dist-cjs/puzzles/pyraminx/solver/search.js +2 -5
- package/dist-cjs/puzzles/pyraminx/solver/tables.js +18 -1
- package/dist-cjs/puzzles/shared/compressed-fetch.js +229 -31
- package/dist-cjs/puzzles/shared/load-once.js +14 -0
- package/dist-cjs/puzzles/skewb/solver/browser-loader.js +44 -16
- package/dist-cjs/puzzles/skewb/solver/generated-assets.js +8 -3
- package/dist-cjs/puzzles/skewb/solver/search.js +2 -2
- package/dist-cjs/puzzles/skewb/solver/tables.js +18 -1
- package/dist-cjs/puzzles/sq1/solver/browser-loader.js +58 -26
- package/docs/development.md +19 -7
- package/package.json +17 -7
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 2,
|
|
3
|
+
"puzzle": "cube4",
|
|
4
|
+
"phase": "hc",
|
|
5
|
+
"moves": [
|
|
6
|
+
"U",
|
|
7
|
+
"U2",
|
|
8
|
+
"U'",
|
|
9
|
+
"R2",
|
|
10
|
+
"F",
|
|
11
|
+
"F2",
|
|
12
|
+
"F'",
|
|
13
|
+
"D",
|
|
14
|
+
"D2",
|
|
15
|
+
"D'",
|
|
16
|
+
"L2",
|
|
17
|
+
"B",
|
|
18
|
+
"B2",
|
|
19
|
+
"B'",
|
|
20
|
+
"Uw2",
|
|
21
|
+
"Rw2",
|
|
22
|
+
"Fw2"
|
|
23
|
+
],
|
|
24
|
+
"states": 2704156,
|
|
25
|
+
"reachableStates": 58800,
|
|
26
|
+
"reachableDense": 58800,
|
|
27
|
+
"maxDepth": 11,
|
|
28
|
+
"distances": {
|
|
29
|
+
"file": "hc-distances.bin",
|
|
30
|
+
"range": 2704156,
|
|
31
|
+
"checksum": "8720ec4a83eca33c3d56ae0ab8709c0b3d847c14d011b6dcc4413b28bd9dfa12"
|
|
32
|
+
},
|
|
33
|
+
"moveTable": {
|
|
34
|
+
"file": "hc-move.bin",
|
|
35
|
+
"range": 999600,
|
|
36
|
+
"checksum": "6bd1d8f36b77e0863425bced1dfb69df7a13dde4fb18ceeaeba00abf14519922"
|
|
37
|
+
},
|
|
38
|
+
"c3PairDist": {
|
|
39
|
+
"file": "c3_pair_dist.bin",
|
|
40
|
+
"range": 4233600,
|
|
41
|
+
"bytes": 2116800,
|
|
42
|
+
"checksum": "2d070dcd9cfa8ede09cf0def93b3b2ba05a7e7e5acf631d6bf25a65bf0057035"
|
|
43
|
+
}
|
|
44
|
+
}
|
|
Binary file
|
|
@@ -0,0 +1,232 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Half-class exact PDB (Phase-3 center heuristic, Fase 1 pivot).
|
|
4
|
+
*
|
|
5
|
+
* PIVOT NOTE: the original Fase-1 spec (3 exact face-pair color PDBs,
|
|
6
|
+
* C(24,4)xC(20,4) = 51M states each) is unworkable — verified that outer
|
|
7
|
+
* face turns never move centers across faces (only wide doubles do), so
|
|
8
|
+
* the exact-color orbit under the 17 search moves holds ~70 states and
|
|
9
|
+
* the bound would read "unknown" almost everywhere. This table instead
|
|
10
|
+
* tracks the EXACT arrangement of half-class bits ((color/3)^1: 12 ones,
|
|
11
|
+
* 12 zeros), which moves under every search move:
|
|
12
|
+
*
|
|
13
|
+
* states = C(24,12) = 2,704,156, nibble-packed (~1.35 MB)
|
|
14
|
+
*
|
|
15
|
+
* Center3 (29,400 states) is a quotient of this coordinate (rank-7 pair
|
|
16
|
+
* + rl-index + parity), so this PDB dominates it by construction:
|
|
17
|
+
* bound = max(ct3, edge, hc). BFS from solved with the 17 search moves.
|
|
18
|
+
*/
|
|
19
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
20
|
+
exports.HC_REACHABLE = exports.HC_PDB_STATES = exports.HC_PDB_MOVES = void 0;
|
|
21
|
+
exports.hcRank = hcRank;
|
|
22
|
+
exports.hcUnrank = hcUnrank;
|
|
23
|
+
exports.encodeHcPdb = encodeHcPdb;
|
|
24
|
+
exports.getHcPdbMoveMaps = getHcPdbMoveMaps;
|
|
25
|
+
exports.hcPdbDistance = hcPdbDistance;
|
|
26
|
+
exports.generateHcPdb = generateHcPdb;
|
|
27
|
+
exports.buildHcDense = buildHcDense;
|
|
28
|
+
exports.hcDenseIndex = hcDenseIndex;
|
|
29
|
+
exports.generateHcDenseMoves = generateHcDenseMoves;
|
|
30
|
+
const center3_js_1 = require("./center3.js");
|
|
31
|
+
const moves_js_1 = require("../moves.js");
|
|
32
|
+
exports.HC_PDB_MOVES = center3_js_1.CENTER3_MOVES.slice(0, 17);
|
|
33
|
+
/** C(24,12) = 2,704,156. */
|
|
34
|
+
exports.HC_PDB_STATES = 2704156;
|
|
35
|
+
const CNK = (() => {
|
|
36
|
+
const table = [];
|
|
37
|
+
for (let n = 0; n <= 24; n++) {
|
|
38
|
+
table[n] = [];
|
|
39
|
+
for (let k = 0; k <= 12; k++) {
|
|
40
|
+
if (k === 0 || k === n)
|
|
41
|
+
table[n][k] = 1;
|
|
42
|
+
else if (k > n)
|
|
43
|
+
table[n][k] = 0;
|
|
44
|
+
else
|
|
45
|
+
table[n][k] = table[n - 1][k - 1] + table[n - 1][k];
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
return table;
|
|
49
|
+
})();
|
|
50
|
+
function halfClass(color) {
|
|
51
|
+
return (Math.floor(color / 3) ^ 1) & 1;
|
|
52
|
+
}
|
|
53
|
+
/** C(24,12) rank of a 24-bit mask with popcount 12 (bit i = position i holds 1). */
|
|
54
|
+
function hcRank(mask) {
|
|
55
|
+
let rank = 0;
|
|
56
|
+
let k = 1;
|
|
57
|
+
for (let i = 0; i < 24; i++) {
|
|
58
|
+
if (mask & (1 << i)) {
|
|
59
|
+
rank += CNK[i][k];
|
|
60
|
+
k++;
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
return rank;
|
|
64
|
+
}
|
|
65
|
+
/** Inverse of hcRank: mask with popcount 12. */
|
|
66
|
+
function hcUnrank(rank) {
|
|
67
|
+
let mask = 0;
|
|
68
|
+
let r = rank;
|
|
69
|
+
for (let k = 12; k >= 1; k--) {
|
|
70
|
+
for (let v = 23; v >= 0; v--) {
|
|
71
|
+
const c = CNK[v][k];
|
|
72
|
+
if (c <= r) {
|
|
73
|
+
mask |= 1 << v;
|
|
74
|
+
r -= c;
|
|
75
|
+
break;
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
return mask;
|
|
80
|
+
}
|
|
81
|
+
/** Encode center colors to the half-class coordinate. */
|
|
82
|
+
function encodeHcPdb(centers) {
|
|
83
|
+
let mask = 0;
|
|
84
|
+
for (let i = 0; i < 24; i++) {
|
|
85
|
+
if (halfClass(centers[i]) === 1)
|
|
86
|
+
mask |= 1 << i;
|
|
87
|
+
}
|
|
88
|
+
return hcRank(mask);
|
|
89
|
+
}
|
|
90
|
+
/** Position permutation maps for the 17 moves (next[i] = prev[map[i]]). */
|
|
91
|
+
function buildMovePositionMaps() {
|
|
92
|
+
const maps = new Int32Array(exports.HC_PDB_MOVES.length * 24);
|
|
93
|
+
for (let m = 0; m < exports.HC_PDB_MOVES.length; m++) {
|
|
94
|
+
const { face, turns } = (0, moves_js_1.parseMove)(exports.HC_PDB_MOVES[m]);
|
|
95
|
+
const effect = moves_js_1.FACE_MOVE_EFFECTS[face];
|
|
96
|
+
let pos = Array.from({ length: 24 }, (_, i) => i);
|
|
97
|
+
for (let t = 0; t < turns; t++) {
|
|
98
|
+
const next = new Array(24);
|
|
99
|
+
for (let i = 0; i < 24; i++)
|
|
100
|
+
next[i] = pos[effect.ctperm[i]];
|
|
101
|
+
pos = next;
|
|
102
|
+
}
|
|
103
|
+
for (let i = 0; i < 24; i++)
|
|
104
|
+
maps[m * 24 + i] = pos[i];
|
|
105
|
+
}
|
|
106
|
+
return maps;
|
|
107
|
+
}
|
|
108
|
+
let cachedMoveMaps = null;
|
|
109
|
+
function getHcPdbMoveMaps() {
|
|
110
|
+
if (!cachedMoveMaps)
|
|
111
|
+
cachedMoveMaps = buildMovePositionMaps();
|
|
112
|
+
return cachedMoveMaps;
|
|
113
|
+
}
|
|
114
|
+
/** Nibble-packed lookup (high nibble = odd index). */
|
|
115
|
+
function hcPdbDistance(packed, coordinate) {
|
|
116
|
+
const byte = packed[coordinate >> 1];
|
|
117
|
+
return (coordinate & 1) === 0 ? byte & 0x0f : byte >> 4;
|
|
118
|
+
}
|
|
119
|
+
/**
|
|
120
|
+
* BFS from the solved half-class mask. Returns nibble-packed distances
|
|
121
|
+
* (0x0f = unreachable). ~2.7M states x 17 moves.
|
|
122
|
+
*/
|
|
123
|
+
function generateHcPdb(onProgress) {
|
|
124
|
+
const maps = getHcPdbMoveMaps();
|
|
125
|
+
const packed = new Uint8Array((exports.HC_PDB_STATES + 1) >> 1).fill(0xff);
|
|
126
|
+
const get = (c) => {
|
|
127
|
+
const b = packed[c >> 1];
|
|
128
|
+
return (c & 1) === 0 ? b & 0x0f : b >> 4;
|
|
129
|
+
};
|
|
130
|
+
const set = (c, d) => {
|
|
131
|
+
const i = c >> 1;
|
|
132
|
+
if ((c & 1) === 0)
|
|
133
|
+
packed[i] = (packed[i] & 0xf0) | (d & 0x0f);
|
|
134
|
+
else
|
|
135
|
+
packed[i] = (packed[i] & 0x0f) | ((d & 0x0f) << 4);
|
|
136
|
+
};
|
|
137
|
+
// Solved mask: U/R/F faces hold half-class 1.
|
|
138
|
+
let solvedMask = 0;
|
|
139
|
+
for (const i of [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11])
|
|
140
|
+
solvedMask |= 1 << i;
|
|
141
|
+
const solved = hcRank(solvedMask);
|
|
142
|
+
if (solved !== 0)
|
|
143
|
+
throw new Error(`solved rank != 0 (${solved})`);
|
|
144
|
+
const queue = new Int32Array(exports.HC_PDB_STATES);
|
|
145
|
+
let head = 0;
|
|
146
|
+
let tail = 0;
|
|
147
|
+
set(0, 0);
|
|
148
|
+
queue[tail++] = 0;
|
|
149
|
+
while (head < tail) {
|
|
150
|
+
const coord = queue[head++];
|
|
151
|
+
const d = get(coord);
|
|
152
|
+
const nd = d + 1;
|
|
153
|
+
if (nd > 14)
|
|
154
|
+
continue; // nibble range; 0x0f stays "unknown" (admissible)
|
|
155
|
+
const mask = hcUnrank(coord);
|
|
156
|
+
for (let m = 0; m < exports.HC_PDB_MOVES.length; m++) {
|
|
157
|
+
const base = m * 24;
|
|
158
|
+
let nmask = 0;
|
|
159
|
+
for (let i = 0; i < 24; i++) {
|
|
160
|
+
if (mask & (1 << maps[base + i]))
|
|
161
|
+
nmask |= 1 << i;
|
|
162
|
+
}
|
|
163
|
+
const next = hcRank(nmask);
|
|
164
|
+
if (get(next) === 0x0f) {
|
|
165
|
+
set(next, nd);
|
|
166
|
+
queue[tail++] = next;
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
if (onProgress && (head & 0x7ffff) === 0)
|
|
170
|
+
onProgress(head, exports.HC_PDB_STATES);
|
|
171
|
+
}
|
|
172
|
+
if (onProgress)
|
|
173
|
+
onProgress(tail, exports.HC_PDB_STATES);
|
|
174
|
+
return packed;
|
|
175
|
+
}
|
|
176
|
+
/** Number of reachable states (BFS orbit size). Fixed by the move set. */
|
|
177
|
+
exports.HC_REACHABLE = 58800;
|
|
178
|
+
/** Build the compact coordinate list + unpacked distances from the packed PDB. */
|
|
179
|
+
function buildHcDense(packed) {
|
|
180
|
+
let count = 0;
|
|
181
|
+
for (let c = 0; c < exports.HC_PDB_STATES; c++) {
|
|
182
|
+
if (hcPdbDistance(packed, c) !== 0x0f)
|
|
183
|
+
count++;
|
|
184
|
+
}
|
|
185
|
+
const coordinates = new Uint32Array(count);
|
|
186
|
+
const distances = new Uint8Array(count).fill(0x0f);
|
|
187
|
+
let denseIndex = 0;
|
|
188
|
+
for (let c = 0; c < exports.HC_PDB_STATES; c++) {
|
|
189
|
+
const distance = hcPdbDistance(packed, c);
|
|
190
|
+
if (distance === 0x0f)
|
|
191
|
+
continue;
|
|
192
|
+
coordinates[denseIndex] = c;
|
|
193
|
+
distances[denseIndex] = distance;
|
|
194
|
+
denseIndex++;
|
|
195
|
+
}
|
|
196
|
+
return { coordinates, moves: new Int32Array(0), distances, reachableCount: count };
|
|
197
|
+
}
|
|
198
|
+
/** Full HC coordinate -> dense index, or -1 when it is unreachable. */
|
|
199
|
+
function hcDenseIndex(dense, coordinate) {
|
|
200
|
+
let low = 0;
|
|
201
|
+
let high = dense.coordinates.length - 1;
|
|
202
|
+
while (low <= high) {
|
|
203
|
+
const middle = (low + high) >>> 1;
|
|
204
|
+
const value = dense.coordinates[middle];
|
|
205
|
+
if (value === coordinate)
|
|
206
|
+
return middle;
|
|
207
|
+
if (value < coordinate)
|
|
208
|
+
low = middle + 1;
|
|
209
|
+
else
|
|
210
|
+
high = middle - 1;
|
|
211
|
+
}
|
|
212
|
+
return -1;
|
|
213
|
+
}
|
|
214
|
+
/** Dense transition table: for each reachable state and move, dense index (or -1). */
|
|
215
|
+
function generateHcDenseMoves(dense) {
|
|
216
|
+
const maps = getHcPdbMoveMaps();
|
|
217
|
+
const moves = new Int32Array(dense.reachableCount * exports.HC_PDB_MOVES.length).fill(-1);
|
|
218
|
+
for (let di = 0; di < dense.coordinates.length; di++) {
|
|
219
|
+
const c = dense.coordinates[di];
|
|
220
|
+
const mask = hcUnrank(c);
|
|
221
|
+
for (let m = 0; m < exports.HC_PDB_MOVES.length; m++) {
|
|
222
|
+
const base = m * 24;
|
|
223
|
+
let nmask = 0;
|
|
224
|
+
for (let i = 0; i < 24; i++) {
|
|
225
|
+
if (mask & (1 << maps[base + i]))
|
|
226
|
+
nmask |= 1 << i;
|
|
227
|
+
}
|
|
228
|
+
moves[di * exports.HC_PDB_MOVES.length + m] = hcDenseIndex(dense, hcRank(nmask));
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
return moves;
|
|
232
|
+
}
|
|
@@ -1,19 +1,59 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
/**
|
|
3
3
|
* Node loader for the cube4 reduction tables (the committed .bin files).
|
|
4
|
-
* The manifests are per-phase (`center1-manifest.json`, ...)
|
|
4
|
+
* The manifests are per-phase (`center1-manifest.json`, ...) plus the shared
|
|
5
|
+
* `hc-manifest.json`, which also seals the optional `c3_pair_dist.bin`.
|
|
6
|
+
*
|
|
7
|
+
* Every .bin read is verified: sha256 checksum AND expected byte length
|
|
8
|
+
* (derived from the manifest `range` + the known packing of each table).
|
|
9
|
+
* All integrity errors name the file and the expected/actual sizes so a
|
|
10
|
+
* corrupted download fails loudly instead of silently producing divergent
|
|
11
|
+
* scrambles. browser-loader.ts performs the identical checks.
|
|
5
12
|
*/
|
|
6
13
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
7
14
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
8
15
|
};
|
|
9
16
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
exports.estimateCube4ReductionTableBytes = estimateCube4ReductionTableBytes;
|
|
10
18
|
exports.loadCube4ReductionTables = loadCube4ReductionTables;
|
|
11
19
|
const node_fs_1 = require("node:fs");
|
|
12
20
|
const node_module_1 = require("node:module");
|
|
13
21
|
const node_path_1 = __importDefault(require("node:path"));
|
|
14
22
|
const node_crypto_1 = require("node:crypto");
|
|
23
|
+
const center3_js_1 = require("./center3.js");
|
|
15
24
|
const edge3_js_1 = require("./edge3.js");
|
|
25
|
+
const hc_pdb_js_1 = require("./hc-pdb.js");
|
|
26
|
+
const TABLE_MEMORY_BUDGET_BYTES = 150 * 1024 * 1024;
|
|
27
|
+
/** Estimated committed table bytes per solver instance (excludes JS objects). */
|
|
28
|
+
function estimateCube4ReductionTableBytes(tables) {
|
|
29
|
+
return (tables.center1.distance.byteLength +
|
|
30
|
+
tables.center1.moveMaps.byteLength +
|
|
31
|
+
tables.center2.ctMove.byteLength +
|
|
32
|
+
tables.center2.rlMove.byteLength +
|
|
33
|
+
tables.center2.pruning.byteLength +
|
|
34
|
+
(tables.center2.pruningWide?.byteLength ?? 0) +
|
|
35
|
+
tables.center3.moveTable.byteLength +
|
|
36
|
+
tables.center3.pruning.byteLength +
|
|
37
|
+
tables.edge3.distances.byteLength +
|
|
38
|
+
(tables.hc?.moves.byteLength ?? 0) +
|
|
39
|
+
(tables.hc?.distances.byteLength ?? 0) +
|
|
40
|
+
(tables.hc?.coordinates.byteLength ?? 0) +
|
|
41
|
+
(tables.c3PairDist?.byteLength ?? 0));
|
|
42
|
+
}
|
|
43
|
+
function assertCube4ReductionTableBudget(tables) {
|
|
44
|
+
const bytes = estimateCube4ReductionTableBytes(tables);
|
|
45
|
+
if (bytes > TABLE_MEMORY_BUDGET_BYTES) {
|
|
46
|
+
throw new Error(`Cube4 reduction tables use ${(bytes / (1024 * 1024)).toFixed(1)} MiB, above the ` +
|
|
47
|
+
`${(TABLE_MEMORY_BUDGET_BYTES / (1024 * 1024)).toFixed(0)} MiB table budget.`);
|
|
48
|
+
}
|
|
49
|
+
}
|
|
16
50
|
const GENERATED_DIRECTORY = "puzzles/cube4/reduction/generated";
|
|
51
|
+
/**
|
|
52
|
+
* Byte length of `c3_pair_dist.bin`: CENTER3_STATES x 144 nibble entries
|
|
53
|
+
* (index = ct3 * 144 + f * 12 + h, see phase3-search.ts), nibble-packed.
|
|
54
|
+
* The same constant is enforced in browser-loader.ts.
|
|
55
|
+
*/
|
|
56
|
+
const C3_PAIR_DIST_BYTES = (center3_js_1.CENTER3_STATES * 144) / 2;
|
|
17
57
|
function resolveGeneratedAssetDirectory() {
|
|
18
58
|
const envDirectory = process.env.CUBINGCENTER_SCRAMBLER_TABLES_DIR;
|
|
19
59
|
if (envDirectory && (0, node_fs_1.existsSync)(node_path_1.default.join(envDirectory, "center1-manifest.json"))) {
|
|
@@ -46,48 +86,172 @@ function getAssetDirectoryCandidates() {
|
|
|
46
86
|
function sha256(buffer) {
|
|
47
87
|
return (0, node_crypto_1.createHash)("sha256").update(buffer).digest("hex");
|
|
48
88
|
}
|
|
49
|
-
function
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
89
|
+
function readManifest(file) {
|
|
90
|
+
let text;
|
|
91
|
+
try {
|
|
92
|
+
text = (0, node_fs_1.readFileSync)(file, "utf8");
|
|
93
|
+
}
|
|
94
|
+
catch (error) {
|
|
95
|
+
if (error.code === "ENOENT") {
|
|
96
|
+
throw new Error(`Table manifest missing: ${file} — run pnpm cube4:center1:tables etc. to regenerate the tables.`);
|
|
97
|
+
}
|
|
98
|
+
throw error;
|
|
99
|
+
}
|
|
100
|
+
try {
|
|
101
|
+
return JSON.parse(text);
|
|
102
|
+
}
|
|
103
|
+
catch (error) {
|
|
104
|
+
// Always name the file: a bare JSON.parse error is unactionable.
|
|
105
|
+
throw new Error(`Invalid table manifest JSON: ${file}: ${error instanceof Error ? error.message : String(error)}`);
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
function requireEntry(manifest, manifestPath, label) {
|
|
109
|
+
const entry = manifest[label];
|
|
110
|
+
if (!entry || typeof entry.file !== "string" || typeof entry.checksum !== "string") {
|
|
111
|
+
throw new Error(`Invalid table manifest ${manifestPath}: entry "${label}" must declare { file, checksum } — ` +
|
|
112
|
+
"run pnpm cube4:hc:tables etc. to regenerate the manifest.");
|
|
113
|
+
}
|
|
114
|
+
return entry;
|
|
115
|
+
}
|
|
116
|
+
/**
|
|
117
|
+
* Expected byte length of a table file from its manifest entry.
|
|
118
|
+
* `packing` matches how each table is serialized (see the load* functions).
|
|
119
|
+
*/
|
|
120
|
+
function expectedBytes(entry, packing) {
|
|
121
|
+
if (typeof entry.bytes === "number")
|
|
122
|
+
return entry.bytes;
|
|
123
|
+
if (typeof entry.range !== "number")
|
|
124
|
+
return undefined;
|
|
125
|
+
if (packing === "byte")
|
|
126
|
+
return entry.range;
|
|
127
|
+
if (packing === "nibble")
|
|
128
|
+
return Math.ceil(entry.range / 2);
|
|
129
|
+
return entry.range * 4;
|
|
130
|
+
}
|
|
131
|
+
function readChecked(file, expectedChecksum, expectedLength) {
|
|
132
|
+
let buffer;
|
|
133
|
+
try {
|
|
134
|
+
buffer = (0, node_fs_1.readFileSync)(file);
|
|
135
|
+
}
|
|
136
|
+
catch (error) {
|
|
137
|
+
if (error.code === "ENOENT") {
|
|
138
|
+
throw new Error(`Table file missing: ${file}` +
|
|
139
|
+
(expectedLength !== undefined ? ` (expected ${expectedLength} bytes)` : "") +
|
|
140
|
+
" — run pnpm cube4:center1:tables etc. to regenerate the tables.");
|
|
141
|
+
}
|
|
142
|
+
throw error;
|
|
143
|
+
}
|
|
144
|
+
if (expectedLength !== undefined && buffer.length !== expectedLength) {
|
|
145
|
+
throw new Error(`Table size mismatch: ${file} (expected ${expectedLength} bytes, got ${buffer.length})`);
|
|
146
|
+
}
|
|
147
|
+
const actual = sha256(buffer);
|
|
148
|
+
if (actual !== expectedChecksum) {
|
|
149
|
+
throw new Error(`Table checksum mismatch: ${file} (expected sha256 ${expectedChecksum}, got ${actual}, ${buffer.length} bytes)`);
|
|
53
150
|
}
|
|
54
151
|
return buffer;
|
|
55
152
|
}
|
|
153
|
+
function readCheckedEntry(file, entry, packing) {
|
|
154
|
+
return readChecked(file, entry.checksum, expectedBytes(entry, packing));
|
|
155
|
+
}
|
|
56
156
|
function readInt32(buffer) {
|
|
57
157
|
return new Int32Array(buffer.buffer.slice(buffer.byteOffset, buffer.byteOffset + buffer.byteLength));
|
|
58
158
|
}
|
|
59
159
|
function loadCenter1(assetDirectory) {
|
|
60
|
-
const
|
|
160
|
+
const manifestPath = node_path_1.default.join(assetDirectory, "center1-manifest.json");
|
|
161
|
+
const manifest = readManifest(manifestPath);
|
|
162
|
+
const distanceTable = requireEntry(manifest, manifestPath, "distanceTable");
|
|
163
|
+
const moveMaps = requireEntry(manifest, manifestPath, "moveMaps");
|
|
61
164
|
return {
|
|
62
|
-
distance: new Uint8Array(
|
|
63
|
-
moveMaps: readInt32(
|
|
165
|
+
distance: new Uint8Array(readCheckedEntry(node_path_1.default.join(assetDirectory, distanceTable.file), distanceTable, "nibble")),
|
|
166
|
+
moveMaps: readInt32(readCheckedEntry(node_path_1.default.join(assetDirectory, moveMaps.file), moveMaps, "int32"))
|
|
64
167
|
};
|
|
65
168
|
}
|
|
66
169
|
function loadCenter2(assetDirectory) {
|
|
67
|
-
const
|
|
68
|
-
const
|
|
69
|
-
const
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
170
|
+
const manifestPath = node_path_1.default.join(assetDirectory, "center2-manifest.json");
|
|
171
|
+
const manifest = readManifest(manifestPath);
|
|
172
|
+
const ctMove = requireEntry(manifest, manifestPath, "ctMove");
|
|
173
|
+
const rlMove = requireEntry(manifest, manifestPath, "rlMove");
|
|
174
|
+
const pruningTable = requireEntry(manifest, manifestPath, "pruningTable");
|
|
175
|
+
// Tolerates older assets without the wide table (wide searches throw
|
|
176
|
+
// a clear error only when actually requested).
|
|
177
|
+
const pruningWideEntry = manifest.pruningWideTable
|
|
178
|
+
? requireEntry(manifest, manifestPath, "pruningWideTable")
|
|
179
|
+
: undefined;
|
|
180
|
+
function unpackPruning(buffer) {
|
|
181
|
+
const pruning = new Uint8Array(450450);
|
|
182
|
+
for (let i = 0; i < 450450; i++) {
|
|
183
|
+
const byte = buffer[i >> 1];
|
|
184
|
+
pruning[i] = (i & 1) === 0 ? byte & 0x0f : byte >> 4;
|
|
185
|
+
}
|
|
186
|
+
return pruning;
|
|
73
187
|
}
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
188
|
+
const pruningBuffer = readCheckedEntry(node_path_1.default.join(assetDirectory, pruningTable.file), pruningTable, "nibble");
|
|
189
|
+
const tables = {
|
|
190
|
+
ctMove: readInt32(readCheckedEntry(node_path_1.default.join(assetDirectory, ctMove.file), ctMove, "int32")),
|
|
191
|
+
rlMove: readInt32(readCheckedEntry(node_path_1.default.join(assetDirectory, rlMove.file), rlMove, "int32")),
|
|
192
|
+
pruning: unpackPruning(pruningBuffer)
|
|
78
193
|
};
|
|
194
|
+
if (pruningWideEntry) {
|
|
195
|
+
const wideBuffer = readCheckedEntry(node_path_1.default.join(assetDirectory, pruningWideEntry.file), pruningWideEntry, "nibble");
|
|
196
|
+
tables.pruningWide = unpackPruning(wideBuffer);
|
|
197
|
+
}
|
|
198
|
+
return tables;
|
|
79
199
|
}
|
|
80
200
|
function loadCenter3(assetDirectory) {
|
|
81
|
-
const
|
|
201
|
+
const manifestPath = node_path_1.default.join(assetDirectory, "center3-manifest.json");
|
|
202
|
+
const manifest = readManifest(manifestPath);
|
|
203
|
+
const moveTable = requireEntry(manifest, manifestPath, "moveTable");
|
|
204
|
+
const pruningTable = requireEntry(manifest, manifestPath, "pruningTable");
|
|
82
205
|
return {
|
|
83
|
-
moveTable: readInt32(
|
|
84
|
-
pruning: new Uint8Array(
|
|
206
|
+
moveTable: readInt32(readCheckedEntry(node_path_1.default.join(assetDirectory, moveTable.file), moveTable, "int32")),
|
|
207
|
+
pruning: new Uint8Array(readCheckedEntry(node_path_1.default.join(assetDirectory, pruningTable.file), pruningTable, "byte"))
|
|
85
208
|
};
|
|
86
209
|
}
|
|
87
210
|
function loadEdge3(assetDirectory) {
|
|
88
|
-
const
|
|
89
|
-
const
|
|
90
|
-
|
|
211
|
+
const manifestPath = node_path_1.default.join(assetDirectory, "edge3-manifest.json");
|
|
212
|
+
const manifest = readManifest(manifestPath);
|
|
213
|
+
const distances = requireEntry(manifest, manifestPath, "distances");
|
|
214
|
+
// `range` here already counts packed nibble BYTES (classCount / 2), unlike
|
|
215
|
+
// the other manifests — derive the length from classCount instead.
|
|
216
|
+
const classCount = manifest.classCount;
|
|
217
|
+
const length = typeof classCount === "number" ? Math.ceil(classCount / 2) : expectedBytes(distances, "byte");
|
|
218
|
+
// Keep the nibble-packed distances as-is (15.5MB instead of 31MB): every
|
|
219
|
+
// reader (phase3 getPrun, solve.ts, edge3DistanceLookup) handles both
|
|
220
|
+
// formats, and the smaller working set halves edge-table DRAM traffic.
|
|
221
|
+
const buffer = readChecked(node_path_1.default.join(assetDirectory, distances.file), distances.checksum, length);
|
|
222
|
+
const table = new Uint8Array(buffer);
|
|
223
|
+
return { distances: table, distanceOfEdge: (0, edge3_js_1.edge3DistanceLookup)(table) };
|
|
224
|
+
}
|
|
225
|
+
function loadHc(assetDirectory, manifest) {
|
|
226
|
+
const manifestPath = node_path_1.default.join(assetDirectory, "hc-manifest.json");
|
|
227
|
+
const distances = requireEntry(manifest, manifestPath, "distances");
|
|
228
|
+
const packed = new Uint8Array(readCheckedEntry(node_path_1.default.join(assetDirectory, distances.file), distances, "nibble"));
|
|
229
|
+
const dense = (0, hc_pdb_js_1.buildHcDense)(packed);
|
|
230
|
+
if (manifest.moveTable !== undefined) {
|
|
231
|
+
const moveTable = requireEntry(manifest, manifestPath, "moveTable");
|
|
232
|
+
const moveBuffer = readCheckedEntry(node_path_1.default.join(assetDirectory, moveTable.file), moveTable, "int32");
|
|
233
|
+
dense.moves = new Int32Array(moveBuffer.buffer.slice(moveBuffer.byteOffset, moveBuffer.byteOffset + moveBuffer.byteLength));
|
|
234
|
+
}
|
|
235
|
+
return dense;
|
|
236
|
+
}
|
|
237
|
+
function loadC3Pair(assetDirectory, hcManifest) {
|
|
238
|
+
const filePath = node_path_1.default.join(assetDirectory, "c3_pair_dist.bin");
|
|
239
|
+
const manifestPath = node_path_1.default.join(assetDirectory, "hc-manifest.json");
|
|
240
|
+
if (!hcManifest || hcManifest.c3PairDist === undefined) {
|
|
241
|
+
// Optional table: absent = not deployed (fine). Present but unsealed =
|
|
242
|
+
// the historical silent-corruption hole — refuse to load it unchecked.
|
|
243
|
+
if ((0, node_fs_1.existsSync)(filePath)) {
|
|
244
|
+
throw new Error(`${filePath} exists but ${manifestPath} has no "c3PairDist" checksum entry — refusing to load it ` +
|
|
245
|
+
"unchecked. Regenerate the manifest: pnpm cube4:hc:tables.");
|
|
246
|
+
}
|
|
247
|
+
return undefined;
|
|
248
|
+
}
|
|
249
|
+
const checked = requireEntry(hcManifest, manifestPath, "c3PairDist");
|
|
250
|
+
if (typeof checked.bytes === "number" && checked.bytes !== C3_PAIR_DIST_BYTES) {
|
|
251
|
+
throw new Error(`Invalid table manifest ${manifestPath}: c3PairDist.bytes is ${checked.bytes}, expected ` +
|
|
252
|
+
`${C3_PAIR_DIST_BYTES} — run pnpm cube4:hc:tables to regenerate it.`);
|
|
253
|
+
}
|
|
254
|
+
return new Uint8Array(readChecked(node_path_1.default.join(assetDirectory, checked.file), checked.checksum, C3_PAIR_DIST_BYTES));
|
|
91
255
|
}
|
|
92
256
|
/** Load all cube4 reduction tables (throws if the generated assets are missing). */
|
|
93
257
|
function loadCube4ReductionTables() {
|
|
@@ -95,10 +259,21 @@ function loadCube4ReductionTables() {
|
|
|
95
259
|
if (!(0, node_fs_1.existsSync)(node_path_1.default.join(assetDirectory, "center1-manifest.json"))) {
|
|
96
260
|
throw new Error("Cube4 reduction tables not generated — run pnpm cube4:center1:tables etc.");
|
|
97
261
|
}
|
|
98
|
-
|
|
262
|
+
const tables = {
|
|
99
263
|
center1: loadCenter1(assetDirectory),
|
|
100
264
|
center2: loadCenter2(assetDirectory),
|
|
101
265
|
center3: loadCenter3(assetDirectory),
|
|
102
266
|
edge3: loadEdge3(assetDirectory)
|
|
103
267
|
};
|
|
268
|
+
// Shared manifest for the two optional PDB tables; absent = not deployed.
|
|
269
|
+
const hcManifestPath = node_path_1.default.join(assetDirectory, "hc-manifest.json");
|
|
270
|
+
const hcManifest = (0, node_fs_1.existsSync)(hcManifestPath) ? readManifest(hcManifestPath) : undefined;
|
|
271
|
+
if (hcManifest) {
|
|
272
|
+
tables.hc = loadHc(assetDirectory, hcManifest);
|
|
273
|
+
}
|
|
274
|
+
const c3Pair = loadC3Pair(assetDirectory, hcManifest);
|
|
275
|
+
if (c3Pair)
|
|
276
|
+
tables.c3PairDist = c3Pair;
|
|
277
|
+
assertCube4ReductionTableBudget(tables);
|
|
278
|
+
return tables;
|
|
104
279
|
}
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Phase-2 wing-transversal pruning (two-pair PDB).
|
|
4
|
+
*
|
|
5
|
+
* A phase-2 goal is only usable when the wings are "transversal": every
|
|
6
|
+
* pair has exactly one wing in the second-half positions
|
|
7
|
+
* (`PAIRING_SECOND_HALVES`). Only ~0.1-0.25% of center goals pass, so the
|
|
8
|
+
* phase-2 search used to enumerate millions of center goals to find the
|
|
9
|
+
* transversal ones. This table bounds how many phase-2 moves two given pairs
|
|
10
|
+
* need to become transversal, so the search can prune branches that cannot
|
|
11
|
+
* end transversal within the remaining depth.
|
|
12
|
+
*
|
|
13
|
+
* State: the unordered positions of the two wings of pair X and of pair Y
|
|
14
|
+
* (276 × 276 index; overlapping positions are unreachable). The predicate
|
|
15
|
+
* only depends on positions, so ONE table serves every pair-of-pairs.
|
|
16
|
+
* Exact BFS distances under the given phase-2 move prefix (the search set),
|
|
17
|
+
* hence admissible; generated at runtime (76,176 bytes, a few ms).
|
|
18
|
+
*/
|
|
19
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
20
|
+
exports.PAIR_INDEX = exports.PAIR_STATES = void 0;
|
|
21
|
+
exports.getWingPositionMoves = getWingPositionMoves;
|
|
22
|
+
exports.getTransversalPairPdb = getTransversalPairPdb;
|
|
23
|
+
const center2_js_1 = require("./center2.js");
|
|
24
|
+
const pairing_coordinate_js_1 = require("./pairing-coordinate.js");
|
|
25
|
+
exports.PAIR_STATES = 276;
|
|
26
|
+
/** Unordered position pair (a != b) → 0..275, as a flat 24×24 table. */
|
|
27
|
+
exports.PAIR_INDEX = (() => {
|
|
28
|
+
const index = new Uint16Array(24 * 24).fill(0xffff);
|
|
29
|
+
let k = 0;
|
|
30
|
+
for (let a = 0; a < 24; a++) {
|
|
31
|
+
for (let b = a + 1; b < 24; b++) {
|
|
32
|
+
index[a * 24 + b] = k;
|
|
33
|
+
index[b * 24 + a] = k;
|
|
34
|
+
k++;
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
return index;
|
|
38
|
+
})();
|
|
39
|
+
const PAIR_A = new Uint8Array(exports.PAIR_STATES);
|
|
40
|
+
const PAIR_B = new Uint8Array(exports.PAIR_STATES);
|
|
41
|
+
for (let a = 0; a < 24; a++) {
|
|
42
|
+
for (let b = a + 1; b < 24; b++) {
|
|
43
|
+
const k = exports.PAIR_INDEX[a * 24 + b];
|
|
44
|
+
PAIR_A[k] = a;
|
|
45
|
+
PAIR_B[k] = b;
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
const IS_SECOND = new Uint8Array(24);
|
|
49
|
+
for (const p of pairing_coordinate_js_1.PAIRING_SECOND_HALVES)
|
|
50
|
+
IS_SECOND[p] = 1;
|
|
51
|
+
/**
|
|
52
|
+
* Wing position maps: `posMove[m * 24 + p]` = where the wing at position p
|
|
53
|
+
* goes under CENTER2_MOVES[m] (inverse of the `wp` gather maps).
|
|
54
|
+
*/
|
|
55
|
+
function getWingPositionMoves() {
|
|
56
|
+
if (cachedPosMove)
|
|
57
|
+
return cachedPosMove;
|
|
58
|
+
const gather = (0, center2_js_1.getWingMoveMaps)();
|
|
59
|
+
const posMove = new Uint8Array(center2_js_1.CENTER2_MOVES.length * 24);
|
|
60
|
+
for (let m = 0; m < center2_js_1.CENTER2_MOVES.length; m++) {
|
|
61
|
+
for (let i = 0; i < 24; i++)
|
|
62
|
+
posMove[m * 24 + gather[m * 24 + i]] = i;
|
|
63
|
+
}
|
|
64
|
+
cachedPosMove = posMove;
|
|
65
|
+
return posMove;
|
|
66
|
+
}
|
|
67
|
+
let cachedPosMove;
|
|
68
|
+
const cache = new Map();
|
|
69
|
+
/** Two-pair transversal distances under the first `moveCount` CENTER2_MOVES. */
|
|
70
|
+
function getTransversalPairPdb(moveCount) {
|
|
71
|
+
const hit = cache.get(moveCount);
|
|
72
|
+
if (hit)
|
|
73
|
+
return hit;
|
|
74
|
+
const posMove = getWingPositionMoves();
|
|
75
|
+
const n = exports.PAIR_STATES * exports.PAIR_STATES;
|
|
76
|
+
const dist = new Uint8Array(n).fill(0xff);
|
|
77
|
+
const queue = new Int32Array(n);
|
|
78
|
+
let tail = 0;
|
|
79
|
+
for (let x = 0; x < exports.PAIR_STATES; x++) {
|
|
80
|
+
if (IS_SECOND[PAIR_A[x]] === IS_SECOND[PAIR_B[x]])
|
|
81
|
+
continue;
|
|
82
|
+
for (let y = 0; y < exports.PAIR_STATES; y++) {
|
|
83
|
+
if (IS_SECOND[PAIR_A[y]] === IS_SECOND[PAIR_B[y]])
|
|
84
|
+
continue;
|
|
85
|
+
const ya = PAIR_A[y];
|
|
86
|
+
const yb = PAIR_B[y];
|
|
87
|
+
if (ya === PAIR_A[x] || ya === PAIR_B[x] || yb === PAIR_A[x] || yb === PAIR_B[x])
|
|
88
|
+
continue;
|
|
89
|
+
const s = x * exports.PAIR_STATES + y;
|
|
90
|
+
dist[s] = 0;
|
|
91
|
+
queue[tail++] = s;
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
for (let head = 0; head < tail; head++) {
|
|
95
|
+
const s = queue[head];
|
|
96
|
+
const x = (s / exports.PAIR_STATES) | 0;
|
|
97
|
+
const y = s - x * exports.PAIR_STATES;
|
|
98
|
+
const d = dist[s] + 1;
|
|
99
|
+
for (let m = 0; m < moveCount; m++) {
|
|
100
|
+
const row = m * 24;
|
|
101
|
+
const nx = exports.PAIR_INDEX[posMove[row + PAIR_A[x]] * 24 + posMove[row + PAIR_B[x]]];
|
|
102
|
+
const ny = exports.PAIR_INDEX[posMove[row + PAIR_A[y]] * 24 + posMove[row + PAIR_B[y]]];
|
|
103
|
+
const t = nx * exports.PAIR_STATES + ny;
|
|
104
|
+
if (dist[t] === 0xff) {
|
|
105
|
+
dist[t] = d;
|
|
106
|
+
queue[tail++] = t;
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
cache.set(moveCount, dist);
|
|
111
|
+
return dist;
|
|
112
|
+
}
|