diff options
author | Elizabeth Hunt <elizabeth.hunt@simponic.xyz> | 2023-12-02 22:51:23 -0700 |
---|---|---|
committer | Elizabeth Hunt <elizabeth.hunt@simponic.xyz> | 2023-12-02 22:51:23 -0700 |
commit | 33c0bad9e3724fb939dc5fb069e14613d86fa4be (patch) | |
tree | 545cc810633cd5fe31bbbccc7d6c86d42a1ec54a | |
parent | c97078c1dc2d245d829294ff700b2e2cbcbbf3fe (diff) | |
download | aoc-33c0bad9e3724fb939dc5fb069e14613d86fa4be.tar.gz aoc-33c0bad9e3724fb939dc5fb069e14613d86fa4be.zip |
some extra utils
-rw-r--r-- | utils/index.ts | 1 | ||||
-rw-r--r-- | utils/point2d.ts | 22 | ||||
-rw-r--r-- | utils/str.ts | 1 |
3 files changed, 24 insertions, 0 deletions
diff --git a/utils/index.ts b/utils/index.ts index b2e38c1..bb34979 100644 --- a/utils/index.ts +++ b/utils/index.ts @@ -1,2 +1,3 @@ export * from "./jsonds"; export * from "./point2d"; +export * from "./str"; diff --git a/utils/point2d.ts b/utils/point2d.ts index 3d07569..45bee2e 100644 --- a/utils/point2d.ts +++ b/utils/point2d.ts @@ -7,3 +7,25 @@ export const l2Norm = (p1: Vec2, p2: Vec2) => Math.sqrt(Math.pow(p2.x - p1.x, 2) + Math.pow(p2.y - p1.y, 2)); export const isDiagAdj = (p1: Vec2, p2: Vec2) => l2Norm(p1, p2) <= Math.sqrt(2); + +export const neighbors = (p1: Vec2, max: Vec2) => { + const ns: Vec2[] = []; + const { x, y } = p1; + for (let dy = -1; dy <= 1; dy++) { + for (let dx = -1; dx <= 1; dx++) { + const [newX, newY] = [dx + x, dy + y]; + if ( + !(dy === dx && dy === 0) && + newX >= 0 && + newX < max.x && + newY < max.y && + newY >= 0 + ) { + ns.push({ x: newX, y: newY }); + } + } + } + return ns; +}; + +export const; diff --git a/utils/str.ts b/utils/str.ts new file mode 100644 index 0000000..cbbc269 --- /dev/null +++ b/utils/str.ts @@ -0,0 +1 @@ +export const isNumeric = (symbol: string) => symbol <= "9" && symbol >= "0"; |