Skip to content

Commit

Permalink
[vendor/twsearch] Vendor the Rust build and use for 2x2x2.
Browse files Browse the repository at this point in the history
  • Loading branch information
lgarron committed Oct 25, 2023
1 parent 316bf4d commit 1b4aeec
Show file tree
Hide file tree
Showing 8 changed files with 492 additions and 85 deletions.
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
*.ttf filter=lfs diff=lfs merge=lfs -text
*.wasm filter=lfs diff=lfs merge=lfs -text
src/cubing/vendor/mpl/twsearch-cpp/twsearch-*.js filter=lfs diff=lfs merge=lfs -text
src/cubing/vendor/mpl/twsearch/twsearch_wasm_bg-*.js filter=lfs diff=lfs merge=lfs -text
8 changes: 5 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -216,9 +216,11 @@ deploy-experiments: build-site-experiments
.PHONY: roll-vendored-twsearch
roll-vendored-twsearch:
test -d ../twsearch/ || exit
cd ../twsearch/ && make clean build/esm
rm -rf src/cubing/vendor/mpl/twsearch-cpp/*
cp -R ../twsearch/build/esm/* src/cubing/vendor/mpl/twsearch-cpp/
rm -rf ../twsearch/dist/wasm/
cd ../twsearch/ && make build-rust-wasm
mkdir -p ./src/cubing/vendor/mpl/twsearch
rm -rf ./src/cubing/vendor/mpl/twsearch/*
cp -R ../twsearch/dist/wasm/* ./src/cubing/vendor/mpl/twsearch/
${BUN_RUN} script/fix-vendored-twsearch.ts
.PHONY: update-create-cubing-app
update-create-cubing-app:
Expand Down
31 changes: 1 addition & 30 deletions script/fix-vendored-twsearch.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
import { readdir, readFile, writeFile } from "node:fs/promises";
import { join } from "node:path";

const DIR = new URL("../src/cubing/vendor/mpl/twsearch-cpp", import.meta.url)
const DIR = new URL("../src/cubing/vendor/mpl/twsearch", import.meta.url)
.pathname;
// TODO(https://github.com/cubing/cubing.js/issues/290)
const BIOME_JSON = new URL("../biome.json", import.meta.url).pathname;

let dynamicFileName = null;
for (const fileName of await readdir(DIR)) {
const filePath = join(DIR, fileName);
console.log("Fixing:", filePath);
Expand All @@ -21,34 +18,8 @@ for (const fileName of await readdir(DIR)) {
break;
}
default: {
if (dynamicFileName !== null) {
throw new Error("Too many files!");
}
dynamicFileName = fileName;
const lines = ["let _scriptDir;"];
lineLoop: for (const line of contents.split("\n")) {
for (const forbidden of [
"import.meta.url",
"require2",
"createRequire",
]) {
if (line.includes(forbidden)) {
continue lineLoop;
}
}
lines.push(line);
}
contents = lines.join("\n");
break;
}
}
await writeFile(filePath, contents);
}

console.log("Fixing:", BIOME_JSON);
let contents = await readFile(BIOME_JSON, "utf-8");
contents = contents.replaceAll(
/"src\/cubing\/vendor\/mpl\/twsearch-cpp\/twsearch-.*.js"/g,
`"src/cubing/vendor/mpl/twsearch-cpp/${dynamicFileName}"`,
);
await writeFile(BIOME_JSON, contents);
54 changes: 3 additions & 51 deletions src/cubing/search/inside/solve/puzzles/2x2x2.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Alg, Move } from "../../../../alg";
import { Alg } from "../../../../alg";
import type { KPuzzle } from "../../../../kpuzzle";
import { KPattern } from "../../../../kpuzzle";
import { cube2x2x2, puzzles } from "../../../../puzzles";
Expand All @@ -7,7 +7,7 @@ import { mustBeInsideWorker } from "../../inside-worker";
import type { SGSCachedData } from "../parseSGS";
import { TrembleSolver } from "../tremble";
import { searchDynamicSideEvents } from "./dynamic/sgs-side-events";
import { solveTwsearch, twsearchPromise } from "../twsearch";
import { solveTwsearch, wasmRandomScrambleForEvent } from "../twsearch";
import { experimentalNormalize2x2x2Orientation } from "../../../../puzzles/cubing-private";

let cachedTrembleSolver: Promise<TrembleSolver> | null = null;
Expand Down Expand Up @@ -70,41 +70,6 @@ export async function solve222HTMOptimal(
return normalizationAlg.concat(orientedResult);
}

async function hasHTMSolutionWithFewerMoves(
pattern: KPattern,
filterMin: number,
): Promise<boolean> {
try {
(await solve222HTMOptimal(pattern, filterMin - 1)).log();
return true;
} catch (e) {
if (e instanceof (await twsearchPromise).NoSolutionError) {
return false;
}
throw e;
}
}

function isCancelling(alg: Alg): boolean {
let lastFamily: undefined | string;
for (const node of alg.childAlgNodes()) {
const move = node.as(Move);
if (!move) {
throw new Error("Unexpected solution with a non-move node!");
}
const { family } = move;
if (
lastFamily &&
((lastFamily === "L" && family === "R") ||
(lastFamily === "R" && family === "L"))
) {
return true;
}
lastFamily = family;
}
return false;
}

// TODO: fix def consistency.
export async function solve222ForScramble(pattern: KPattern): Promise<Alg> {
mustBeInsideWorker();
Expand Down Expand Up @@ -162,18 +127,5 @@ export async function random222Pattern(): Promise<KPattern> {
}

export async function random222Scramble(): Promise<Alg> {
let pattern = await random222Pattern();
while (await hasHTMSolutionWithFewerMoves(pattern, 4)) {
console.info("Filtered out a 2x2x2 pattern!");
pattern = await random222Pattern();
}

let sol = await solve222ForScramble(pattern);
while (isCancelling(sol)) {
// Rely on `--randomstart` to find us a non-cancelling with ≈2/3 probability.
// TODO: Check that this works for 100% of patterns.
sol = await solve222ForScramble(pattern);
}

return sol;
return wasmRandomScrambleForEvent("222");
}
13 changes: 12 additions & 1 deletion src/cubing/search/inside/solve/twsearch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ import {
import { from } from "../../../vendor/mit/p-lazy/p-lazy";

export const twsearchPromise: Promise<
typeof import("../../../vendor/mpl/twsearch")
> = from(async () => import("../../../vendor/mpl/twsearch"));

export const twsearchCppPromise: Promise<
typeof import("../../../vendor/mpl/twsearch-cpp")
> = from(async () => import("../../../vendor/mpl/twsearch-cpp"));

Expand Down Expand Up @@ -38,7 +42,7 @@ export async function solveTwsearch(
serializeDefToTws,
solvePattern,
serializeScramblePattern,
} = await twsearchPromise;
} = await twsearchCppPromise;
const kpuzzle = new KPuzzle(def);
setArg("--startprunedepth 5"); // TODO
let moveSubsetString = ""; // TODO: pass the full set of moves, to avoid rotations not being treated as moves.
Expand Down Expand Up @@ -98,3 +102,10 @@ export async function solveTwsearch(
serializeScramblePattern("SearchState", patternData),
);
}

export async function wasmRandomScrambleForEvent(
eventID: string,
): Promise<Alg> {
const { wasmRandomScrambleForEvent } = await twsearchPromise;
return wasmRandomScrambleForEvent(eventID);
}
26 changes: 26 additions & 0 deletions src/cubing/vendor/mpl/twsearch/chunk-T3VXRQ2V.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
var __defProp = Object.defineProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __toBinary = /* @__PURE__ */ (() => {
var table = new Uint8Array(128);
for (var i = 0; i < 64; i++)
table[i < 26 ? i + 65 : i < 52 ? i + 71 : i < 62 ? i - 4 : i * 4 - 205] = i;
return (base64) => {
var n = base64.length, bytes = new Uint8Array((n - (base64[n - 1] == "=") - (base64[n - 2] == "=")) * 3 / 4 | 0);
for (var i2 = 0, j = 0; i2 < n; ) {
var c0 = table[base64.charCodeAt(i2++)], c1 = table[base64.charCodeAt(i2++)];
var c2 = table[base64.charCodeAt(i2++)], c3 = table[base64.charCodeAt(i2++)];
bytes[j++] = c0 << 2 | c1 >> 4;
bytes[j++] = c1 << 4 | c2 >> 2;
bytes[j++] = c2 << 6 | c3;
}
return bytes;
};
})();

export {
__export,
__toBinary
};
Loading

0 comments on commit 1b4aeec

Please sign in to comment.