-
-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: use built in vec and boundingbox impl
- Loading branch information
Showing
8 changed files
with
49 additions
and
151 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
21 changes: 0 additions & 21 deletions
21
solutions/typescript/2018/06/src/model/coord.class.spec.ts
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,69 +1,38 @@ | ||
import { task } from '@alexaegis/advent-of-code-lib'; | ||
import { BoundingBox, Vec2, task } from '@alexaegis/advent-of-code-lib'; | ||
import packageJson from '../package.json'; | ||
import { interpret } from './interpret.function.js'; | ||
import { Coord } from './model/coord.class.js'; | ||
|
||
export const p1 = (input: string): number | undefined => { | ||
const points = interpret(input); | ||
let boundaryTop: Coord | undefined; | ||
let boundaryRight: Coord | undefined; | ||
let boundaryBottom: Coord | undefined; | ||
let boundaryLeft: Coord | undefined; | ||
const aabb = BoundingBox.fromVectors(points); | ||
aabb.pad(0, 1); | ||
const bucket = new Map<string, Vec2[]>(); | ||
|
||
for (const point of points) { | ||
if (boundaryTop === undefined || boundaryTop.y >= point.y) { | ||
boundaryTop = point; | ||
} | ||
if (boundaryRight === undefined || boundaryRight.x <= point.x) { | ||
boundaryRight = point; | ||
} | ||
if (boundaryBottom === undefined || boundaryBottom.y <= point.y) { | ||
boundaryBottom = point; | ||
} | ||
if (boundaryLeft === undefined || boundaryLeft.x >= point.x) { | ||
boundaryLeft = point; | ||
} | ||
} | ||
|
||
if (boundaryTop && boundaryRight && boundaryBottom && boundaryLeft) { | ||
const boundaryStart: Coord = new Coord(boundaryLeft.x, boundaryTop.y); | ||
const boundaryEnd: Coord = new Coord(boundaryRight.x, boundaryBottom.y + 1); | ||
const bucket = new Map<string, Coord[]>(); | ||
for (const point of points) { | ||
bucket.set(point.toString(), []); | ||
} | ||
for (let x = boundaryStart.x; x < boundaryEnd.x; x++) { | ||
for (let y = boundaryStart.y; y < boundaryEnd.y; y++) { | ||
const ordered: Coord[] = points.sort( | ||
(a, b) => a.manhattan(x, y) - b.manhattan(x, y), | ||
); | ||
if (ordered[0]?.manhattan(x, y) !== ordered[1]?.manhattan(x, y)) { | ||
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion | ||
const b = bucket.get(ordered[0]!.toString()); | ||
if (b) { | ||
b.push(new Coord(x, y)); | ||
} | ||
} | ||
for (const x of aabb.horizontal.iter()) { | ||
for (const y of aabb.vertical.iter()) { | ||
const ordered: Vec2[] = points.sort((a, b) => a.manhattan(x, y) - b.manhattan(x, y)); | ||
if (ordered[0]?.manhattan(x, y) !== ordered[1]?.manhattan(x, y)) { | ||
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion | ||
const b = bucket.getOrAdd(ordered[0]!.toString(), () => []); | ||
b.push(new Vec2(x, y)); | ||
} | ||
} | ||
const bound: number[] = []; | ||
for (const territory of bucket.values()) { | ||
if ( | ||
!territory.some( | ||
(point) => | ||
point.x <= boundaryStart.x || | ||
point.y <= boundaryStart.y || | ||
point.x >= boundaryEnd.x || | ||
point.y >= boundaryEnd.y - 1, // magic boundary bandaid | ||
) | ||
) { | ||
bound.push(territory.length); | ||
} | ||
} | ||
const bound: number[] = []; | ||
for (const territory of bucket.values()) { | ||
if ( | ||
!territory.some( | ||
(point) => | ||
point.x <= aabb.left || | ||
point.y <= aabb.top || | ||
point.x >= aabb.right || | ||
point.y >= aabb.bottom - 1, // magic boundary bandaid | ||
) | ||
) { | ||
bound.push(territory.length); | ||
} | ||
return bound.reduce((acc, next) => (next > acc ? next : acc), 0); | ||
} else { | ||
return undefined; | ||
} | ||
return bound.reduce((acc, next) => (next > acc ? next : acc), 0); | ||
}; | ||
|
||
await task(p1, packageJson.aoc); // 3006 ~230ms |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters