Skip to content

Commit

Permalink
chore: use built in vec and boundingbox impl
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexAegis committed Jan 14, 2024
1 parent f6f0bce commit 3f97678
Show file tree
Hide file tree
Showing 8 changed files with 49 additions and 151 deletions.
11 changes: 5 additions & 6 deletions solutions/typescript/2018/06/src/interpret.function.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import { split } from '@alexaegis/advent-of-code-lib';
import { Coord } from './model/coord.class.js';
import { Vec2, split } from '@alexaegis/advent-of-code-lib';

export const interpret = (input: string): Coord[] => {
const points: Coord[] = [];
export const interpret = (input: string): Vec2[] => {
const points: Vec2[] = [];
for (const line of split(input)) {
if (line) {
const lineSplit = line.split(', ');
points.push(new Coord(Number(lineSplit[1]), Number(lineSplit[0])));
const [y, x] = line.splitIntoStringPair(', ');
points.push(new Vec2(Number.parseInt(x, 10), Number.parseInt(y, 10)));
}
}
return points;
Expand Down
3 changes: 0 additions & 3 deletions solutions/typescript/2018/06/src/model/args.interface.ts

This file was deleted.

21 changes: 0 additions & 21 deletions solutions/typescript/2018/06/src/model/coord.class.spec.ts

This file was deleted.

24 changes: 0 additions & 24 deletions solutions/typescript/2018/06/src/model/coord.class.ts

This file was deleted.

81 changes: 25 additions & 56 deletions solutions/typescript/2018/06/src/p1.ts
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
3 changes: 1 addition & 2 deletions solutions/typescript/2018/06/src/p2.spec.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { loadTaskResources } from '@alexaegis/advent-of-code-lib';
import { describe, expect, it } from 'vitest';
import packageJson from '../package.json';
import type { Args } from './model/args.interface.js';
import { p2 } from './p2.js';
import { p2, type Args } from './p2.js';

describe('2018 - Day 6 - Part Two', () => {
it('should solve the input', async () => {
Expand Down
51 changes: 15 additions & 36 deletions solutions/typescript/2018/06/src/p2.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { task } from '@alexaegis/advent-of-code-lib';
import { BoundingBox, task } from '@alexaegis/advent-of-code-lib';
import packageJson from '../package.json';
import { interpret } from './interpret.function.js';
import type { Args } from './model/args.interface.js';
import { Coord } from './model/coord.class.js';

export interface Args {
limit: number;
}

/**
*
Expand All @@ -11,44 +13,21 @@ import { Coord } from './model/coord.class.js';
*/
export const p2 = (input: string, args: Args | undefined): 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);

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;
}
}
let area = 0;

if (boundaryTop && boundaryRight && boundaryBottom && boundaryLeft) {
const boundaryStart: Coord = new Coord(boundaryLeft.x, boundaryTop.y);
const boundaryEnd: Coord = new Coord(boundaryRight.x, boundaryBottom.y + 1);
let area = 0;
for (let x = boundaryStart.x; x < boundaryEnd.x; x++) {
for (let y = boundaryStart.y; y < boundaryEnd.y; y++) {
if (
points.map((a) => a.manhattan(x, y)).reduce((acc, next) => (acc += next)) <
(args ? args.limit : 0)
) {
area++;
}
for (const x of aabb.horizontal.iter()) {
for (const y of aabb.vertical.iter()) {
if (
points.map((a) => a.manhattan(x, y)).reduce((acc, next) => (acc += next)) <
(args ? args.limit : 0)
) {
area++;
}
}
return area;
} else {
return undefined;
}
return area;
};

await task(p2, packageJson.aoc); // 42998 ~46ms
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ export class Interval implements IntervalLike, IntervalQualifier {

reduce<A>(reducer: (accumulator: A, next: number) => A, initialValue: A): A {
let accumulator = initialValue;
for (const item of this.walk()) {
for (const item of this.iter()) {
accumulator = reducer(accumulator, item);
}
return accumulator;
Expand Down Expand Up @@ -506,14 +506,14 @@ export class Interval implements IntervalLike, IntervalQualifier {
return Interval.isAboveLow(this, n);
}

*walk(): Generator<number> {
*iter(): Generator<number> {
for (let i = this.lowest(); this.contains(i); i++) {
yield i;
}
}

collectValues(): number[] {
return [...this.walk()];
return [...this.iter()];
}

/**
Expand Down

0 comments on commit 3f97678

Please sign in to comment.