🚀 refactor algorithm

This commit is contained in:
platane
2020-10-29 23:27:08 +01:00
parent 1c6814c2fa
commit d81ecec836
27 changed files with 274 additions and 777 deletions

View File

@@ -1,30 +1,16 @@
import { copyGrid } from "@snk/types/grid";
import { pruneLayer } from "./pruneLayer";
import { cleanLayer } from "./cleanLayer-monobranch";
import { getSnakeLength, Snake } from "@snk/types/snake";
import { cleanIntermediateLayer } from "./cleanIntermediateLayer";
import type { Color, Grid } from "@snk/types/grid";
import { Color, copyGrid } from "@snk/types/grid";
import type { Grid } from "@snk/types/grid";
import { cleanColoredLayer } from "./cleanColoredLayer";
import type { Snake } from "@snk/types/snake";
export const getBestRoute = (grid0: Grid, snake0: Snake) => {
const grid = copyGrid(grid0);
const colors = extractColors(grid0);
const snakeN = getSnakeLength(snake0);
const chain: Snake[] = [snake0];
for (const color of colors) {
const gridN = copyGrid(grid);
for (const color of extractColors(grid))
chain.unshift(...cleanColoredLayer(grid, chain[0], color));
// clear the free colors
const chunk = pruneLayer(grid, color, snakeN);
chain.unshift(...cleanLayer(gridN, chain[0], chunk));
// clear the remaining colors, allowing to eat color+1
const nextColor = (color + 1) as Color;
chain.unshift(...cleanIntermediateLayer(grid, nextColor, chain[0]));
}
return chain.reverse().slice(1);
return chain.reverse();
};
const extractColors = (grid: Grid): Color[] => {