🚀 bootstrap workspace

This commit is contained in:
platane
2020-07-16 01:19:21 +02:00
commit e9654e7e66
14 changed files with 7679 additions and 0 deletions

View File

@@ -0,0 +1,24 @@
import { Grid, Color } from "./grid";
const rand = (a: number, b: number) => Math.floor(Math.random() * (b - a)) + a;
export const generateGrid = (
width: number,
height: number,
options: { colors: Color[]; emptyP: number } = {
colors: [1, 2, 3],
emptyP: 2,
}
): Grid => {
const g = {
width,
height,
data: Array.from({ length: width * height }, () => {
const x = rand(-options.emptyP, options.colors.length);
return x < 0 ? null : options.colors[x];
}),
};
return g;
};

25
packages/compute/grid.ts Normal file
View File

@@ -0,0 +1,25 @@
export type Color = number;
export type Grid = {
width: number;
height: number;
data: (Color | null)[];
};
export const getIndex = (grid: Grid, x: number, y: number) =>
x * grid.height + y;
export const isInside = (grid: Grid, x: number, y: number) =>
x >= 0 && y >= 0 && x < grid.width && y < grid.height;
export const getColor = (grid: Grid, x: number, y: number) =>
grid.data[getIndex(grid, x, y)];
export const setColor = (
grid: Grid,
x: number,
y: number,
color: Color | null
) => {
grid.data[getIndex(grid, x, y)] = color;
};

13
packages/compute/index.ts Normal file
View File

@@ -0,0 +1,13 @@
import { Grid, Color } from "./grid";
type Point = { x: number; y: number };
export const computeBestRun = (
pattern: Color[],
grid: Grid,
originalPosition: Point
) => {
console.log(pattern, grid, originalPosition);
return [];
};

View File

@@ -0,0 +1,4 @@
{
"name": "@snk/compute",
"version": "1.0.0"
}