🔨 rename package compute -> solver
This commit is contained in:
48
packages/solver/__tests__/getBestRoute-fuzz.spec.ts
Normal file
48
packages/solver/__tests__/getBestRoute-fuzz.spec.ts
Normal file
@@ -0,0 +1,48 @@
|
||||
import { getBestRoute } from "../getBestRoute";
|
||||
import { snake3, snake4 } from "@snk/types/__fixtures__/snake";
|
||||
import {
|
||||
getHeadX,
|
||||
getHeadY,
|
||||
getSnakeLength,
|
||||
Snake,
|
||||
snakeWillSelfCollide,
|
||||
} from "@snk/types/snake";
|
||||
import { createFromSeed } from "@snk/types/__fixtures__/createFromSeed";
|
||||
|
||||
const n = 1000;
|
||||
|
||||
for (const { width, height, snake } of [
|
||||
{ width: 5, height: 5, snake: snake3 },
|
||||
{ width: 5, height: 5, snake: snake4 },
|
||||
])
|
||||
it(`should find solution for ${n} ${width}x${height} generated grids for ${getSnakeLength(
|
||||
snake
|
||||
)} length snake`, () => {
|
||||
const results = Array.from({ length: n }, (_, seed) => {
|
||||
const grid = createFromSeed(seed, width, height);
|
||||
|
||||
try {
|
||||
const chain = getBestRoute(grid, snake);
|
||||
|
||||
assertValidPath(chain);
|
||||
|
||||
return { seed };
|
||||
} catch (error) {
|
||||
return { seed, error };
|
||||
}
|
||||
});
|
||||
|
||||
expect(results.filter((x) => x.error)).toEqual([]);
|
||||
});
|
||||
|
||||
const assertValidPath = (chain: Snake[]) => {
|
||||
for (let i = 0; i < chain.length - 1; i++) {
|
||||
const dx = getHeadX(chain[i + 1]) - getHeadX(chain[i]);
|
||||
const dy = getHeadY(chain[i + 1]) - getHeadY(chain[i]);
|
||||
|
||||
if (!((Math.abs(dx) === 1 && dy == 0) || (Math.abs(dy) === 1 && dx == 0)))
|
||||
throw new Error(`unexpected direction ${dx},${dy}`);
|
||||
|
||||
if (snakeWillSelfCollide(chain[i], dx, dy)) throw new Error(`self collide`);
|
||||
}
|
||||
};
|
||||
26
packages/solver/__tests__/getBestRoute.spec.ts
Normal file
26
packages/solver/__tests__/getBestRoute.spec.ts
Normal file
@@ -0,0 +1,26 @@
|
||||
import { getBestRoute } from "../getBestRoute";
|
||||
import { Color, createEmptyGrid, setColor } from "@snk/types/grid";
|
||||
import { createSnakeFromCells, snakeToCells } from "@snk/types/snake";
|
||||
import * as grids from "@snk/types/__fixtures__/grid";
|
||||
import { snake3 } from "@snk/types/__fixtures__/snake";
|
||||
|
||||
it("should find best route", () => {
|
||||
const snk0 = [
|
||||
{ x: 0, y: 0 },
|
||||
{ x: 1, y: 0 },
|
||||
];
|
||||
|
||||
const grid = createEmptyGrid(5, 5);
|
||||
setColor(grid, 3, 3, 1 as Color);
|
||||
|
||||
const chain = getBestRoute(grid, createSnakeFromCells(snk0))!;
|
||||
|
||||
expect(snakeToCells(chain[1])[1]).toEqual({ x: 0, y: 0 });
|
||||
|
||||
expect(snakeToCells(chain[chain.length - 1])[0]).toEqual({ x: 3, y: 3 });
|
||||
});
|
||||
|
||||
for (const [gridName, grid] of Object.entries(grids))
|
||||
it(`should find a solution for ${gridName}`, () => {
|
||||
getBestRoute(grid, snake3);
|
||||
});
|
||||
12
packages/solver/__tests__/getPathTo.spec.ts
Normal file
12
packages/solver/__tests__/getPathTo.spec.ts
Normal file
@@ -0,0 +1,12 @@
|
||||
import { createEmptyGrid } from "@snk/types/grid";
|
||||
import { getHeadX, getHeadY } from "@snk/types/snake";
|
||||
import { snake3 } from "@snk/types/__fixtures__/snake";
|
||||
import { getPathTo } from "../getPathTo";
|
||||
|
||||
it("should find it's way in vaccum", () => {
|
||||
const grid = createEmptyGrid(5, 0);
|
||||
|
||||
const path = getPathTo(grid, snake3, 5, -1)!;
|
||||
|
||||
expect([getHeadX(path[0]), getHeadY(path[0])]).toEqual([5, -1]);
|
||||
});
|
||||
19
packages/solver/__tests__/getPathToPose.spec.ts
Normal file
19
packages/solver/__tests__/getPathToPose.spec.ts
Normal file
@@ -0,0 +1,19 @@
|
||||
import { createSnakeFromCells } from "@snk/types/snake";
|
||||
import { getPathToPose } from "../getPathToPose";
|
||||
|
||||
it("should fing path to pose", () => {
|
||||
const snake0 = createSnakeFromCells([
|
||||
{ x: 0, y: 0 },
|
||||
{ x: 1, y: 0 },
|
||||
{ x: 2, y: 0 },
|
||||
]);
|
||||
const target = createSnakeFromCells([
|
||||
{ x: 1, y: 0 },
|
||||
{ x: 2, y: 0 },
|
||||
{ x: 3, y: 0 },
|
||||
]);
|
||||
|
||||
const path = getPathToPose(snake0, target);
|
||||
|
||||
expect(path).toBeDefined();
|
||||
});
|
||||
86
packages/solver/__tests__/sortPush.spec.ts
Normal file
86
packages/solver/__tests__/sortPush.spec.ts
Normal file
@@ -0,0 +1,86 @@
|
||||
import { sortPush } from "../utils/sortPush";
|
||||
|
||||
const sortFn = (a: number, b: number) => a - b;
|
||||
|
||||
it("should sort push length=0", () => {
|
||||
const a: any[] = [];
|
||||
const x = -1;
|
||||
const res = [...a, x].sort(sortFn);
|
||||
|
||||
sortPush(a, x, sortFn);
|
||||
|
||||
expect(a).toEqual(res);
|
||||
});
|
||||
|
||||
it("should sort push under", () => {
|
||||
const a = [1, 2, 3, 4, 5];
|
||||
const x = -1;
|
||||
const res = [...a, x].sort(sortFn);
|
||||
|
||||
sortPush(a, x, sortFn);
|
||||
|
||||
expect(a).toEqual(res);
|
||||
});
|
||||
|
||||
it("should sort push 0", () => {
|
||||
const a = [1, 2, 3, 4, 5];
|
||||
const x = 1;
|
||||
const res = [...a, x].sort(sortFn);
|
||||
|
||||
sortPush(a, x, sortFn);
|
||||
|
||||
expect(a).toEqual(res);
|
||||
});
|
||||
|
||||
it("should sort push end", () => {
|
||||
const a = [1, 2, 3, 4, 5];
|
||||
const x = 5;
|
||||
const res = [...a, x].sort(sortFn);
|
||||
|
||||
sortPush(a, x, sortFn);
|
||||
|
||||
expect(a).toEqual(res);
|
||||
});
|
||||
|
||||
it("should sort push over", () => {
|
||||
const a = [1, 2, 3, 4, 5];
|
||||
const x = 10;
|
||||
const res = [...a, x].sort(sortFn);
|
||||
|
||||
sortPush(a, x, sortFn);
|
||||
|
||||
expect(a).toEqual(res);
|
||||
});
|
||||
|
||||
it("should sort push inside", () => {
|
||||
const a = [1, 2, 3, 4, 5];
|
||||
const x = 1.5;
|
||||
const res = [...a, x].sort(sortFn);
|
||||
|
||||
sortPush(a, x, sortFn);
|
||||
|
||||
expect(a).toEqual(res);
|
||||
});
|
||||
|
||||
describe("benchmark", () => {
|
||||
const n = 200;
|
||||
|
||||
const samples = Array.from({ length: 5000 }, () => [
|
||||
Math.random(),
|
||||
Array.from({ length: n }, () => Math.random()),
|
||||
]);
|
||||
const s0 = samples.map(([x, arr]: any) => [x, arr.slice()]);
|
||||
const s1 = samples.map(([x, arr]: any) => [x, arr.slice()]);
|
||||
|
||||
it("push + sort", () => {
|
||||
for (const [x, arr] of s0) {
|
||||
arr.push(x);
|
||||
arr.sort(sortFn);
|
||||
}
|
||||
});
|
||||
it("sortPush", () => {
|
||||
for (const [x, arr] of s1) {
|
||||
sortPush(arr, x, sortFn);
|
||||
}
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user