🚀 optimize stuff I guess
This commit is contained in:
@@ -1,13 +1,16 @@
|
|||||||
import { Grid, isInsideLarge, getColor, isInside, Color } from "./grid";
|
import { Grid, isInsideLarge, getColor, isInside } from "./grid";
|
||||||
import { around4, Point, pointEquals } from "./point";
|
import { around4, Point, pointEquals } from "./point";
|
||||||
import { snakeWillSelfCollide, Snake } from "./snake";
|
import { snakeWillSelfCollide, Snake } from "./snake";
|
||||||
|
|
||||||
const computeSnakeKey = (snake: Snake) =>
|
const computeSnakeKey = (snake: Snake) => {
|
||||||
snake.map((p) => p.x + "." + p.y).join(",");
|
let key = "";
|
||||||
|
for (let i = 0; i < snake.length; i++) key += snake[i].x + "," + snake[i].y;
|
||||||
|
return key;
|
||||||
|
};
|
||||||
|
|
||||||
type I = {
|
type I = {
|
||||||
h: number;
|
// h: number;
|
||||||
f: number;
|
// f: number;
|
||||||
w: number;
|
w: number;
|
||||||
key: string;
|
key: string;
|
||||||
snake: Snake;
|
snake: Snake;
|
||||||
@@ -44,21 +47,13 @@ export const getAvailableRoutes = (
|
|||||||
{
|
{
|
||||||
key: computeSnakeKey(snake0),
|
key: computeSnakeKey(snake0),
|
||||||
snake: snake0,
|
snake: snake0,
|
||||||
|
|
||||||
// weight, step from origin
|
|
||||||
w: 0,
|
w: 0,
|
||||||
|
|
||||||
// heuristic
|
|
||||||
h: 0,
|
|
||||||
|
|
||||||
// fitness, smaller is better
|
|
||||||
f: 0,
|
|
||||||
parent: null,
|
parent: null,
|
||||||
},
|
},
|
||||||
];
|
];
|
||||||
const closeList: Record<string, I> = {};
|
const closeList: Record<string, I> = {};
|
||||||
|
|
||||||
const solutions: { color: Color; snakeN: Snake; directions: Point[] }[] = [];
|
const solutions: { snakeN: Snake; directions: Point[] }[] = [];
|
||||||
|
|
||||||
while (
|
while (
|
||||||
openList.length &&
|
openList.length &&
|
||||||
@@ -66,34 +61,28 @@ export const getAvailableRoutes = (
|
|||||||
openList[0].w <= maxWeight &&
|
openList[0].w <= maxWeight &&
|
||||||
solutions.length < maxSolutions
|
solutions.length < maxSolutions
|
||||||
) {
|
) {
|
||||||
openList.sort((a, b) => a.f - b.f);
|
openList.sort((a, b) => a.w - b.w);
|
||||||
const c = openList.shift()!;
|
const c = openList.shift()!;
|
||||||
|
|
||||||
closeList[c.key] = c;
|
closeList[c.key] = c;
|
||||||
|
|
||||||
// snakeSteps.push(copySnake(c.snake));
|
|
||||||
|
|
||||||
const [head] = c.snake;
|
const [head] = c.snake;
|
||||||
const color =
|
const color =
|
||||||
isInside(grid, head.x, head.y) && getColor(grid, head.x, head.y);
|
isInside(grid, head.x, head.y) && getColor(grid, head.x, head.y);
|
||||||
|
|
||||||
if (color) {
|
if (color) {
|
||||||
const solution = {
|
|
||||||
color,
|
|
||||||
snakeN: c.snake,
|
|
||||||
directions: unwrap(c.parent, c.snake[0]),
|
|
||||||
};
|
|
||||||
|
|
||||||
const s0 = solutions.find((s) =>
|
const s0 = solutions.find((s) =>
|
||||||
snakeEquals(s.snakeN, solution.snakeN, maxLengthEquality + 1)
|
snakeEquals(s.snakeN, c.snake, maxLengthEquality + 1)
|
||||||
);
|
);
|
||||||
|
|
||||||
if (!s0 || solution.directions.length < s0.directions.length)
|
const directions = unwrap(c.parent, c.snake[0]);
|
||||||
solutions.push(solution);
|
|
||||||
|
if (!s0 || directions.length < s0.directions.length)
|
||||||
|
solutions.push({ snakeN: c.snake, directions });
|
||||||
} else {
|
} else {
|
||||||
for (const direction of around4) {
|
for (let i = 0; i < around4.length; i++) {
|
||||||
const x = head.x + direction.x;
|
const x = head.x + around4[i].x;
|
||||||
const y = head.y + direction.y;
|
const y = head.y + around4[i].y;
|
||||||
|
|
||||||
if (
|
if (
|
||||||
isInsideLarge(grid, 1, x, y) &&
|
isInsideLarge(grid, 1, x, y) &&
|
||||||
@@ -105,16 +94,8 @@ export const getAvailableRoutes = (
|
|||||||
const key = computeSnakeKey(snake);
|
const key = computeSnakeKey(snake);
|
||||||
|
|
||||||
if (!closeList[key] && !openList.some((s) => s.key === key)) {
|
if (!closeList[key] && !openList.some((s) => s.key === key)) {
|
||||||
const h =
|
|
||||||
Math.abs(snake[0].x - snake0[0].x) +
|
|
||||||
Math.abs(snake[0].y - snake0[0].y);
|
|
||||||
|
|
||||||
const w = 1 + c.w;
|
const w = 1 + c.w;
|
||||||
|
openList.push({ key, snake, w, parent: c });
|
||||||
const f = w;
|
|
||||||
// const f = h * 0.6 - w;
|
|
||||||
|
|
||||||
openList.push({ key, snake, f, w, h, parent: c });
|
|
||||||
} else {
|
} else {
|
||||||
// console.log(key, closeList);
|
// console.log(key, closeList);
|
||||||
// debugger;
|
// debugger;
|
||||||
|
|||||||
Reference in New Issue
Block a user