Skip to content

Commit

Permalink
chore: removed coord implementation in favor of Vec2
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexAegis committed Jan 14, 2024
1 parent aec6e36 commit 578eee0
Show file tree
Hide file tree
Showing 8 changed files with 46 additions and 96 deletions.
5 changes: 0 additions & 5 deletions solutions/typescript/2018/10/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,6 @@
"p2": "RUN=1 NODE_NO_WARNINGS=1 ts-node-esm src/p2.ts"
},
"exports": {
"./boundary.interface": {
"types": "./src/boundary.interface.ts",
"import": "./dist/boundary.interface.js",
"default": "./dist/boundary.interface.js"
},
"./interpreter.function": {
"types": "./src/interpreter.function.ts",
"import": "./dist/interpreter.function.js",
Expand Down
6 changes: 0 additions & 6 deletions solutions/typescript/2018/10/src/boundary.interface.ts

This file was deleted.

6 changes: 3 additions & 3 deletions solutions/typescript/2018/10/src/interpreter.function.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { split } from '@alexaegis/advent-of-code-lib';
import { Vector } from './model/vector.class.js';
import { MotionVector } from './model/motion-vector.class.js';

export const interpreter = (input: string): Vector[] =>
split(input).map((line) => Vector.parse(line));
export const interpreter = (input: string): MotionVector[] =>
split(input).map((line) => MotionVector.parse(line));
27 changes: 0 additions & 27 deletions solutions/typescript/2018/10/src/model/coord.class.ts

This file was deleted.

23 changes: 23 additions & 0 deletions solutions/typescript/2018/10/src/model/motion-vector.class.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { Vec2, type Vec2String } from '@alexaegis/advent-of-code-lib';
import { type } from 'arktype';

export class MotionVector {
static inputType = type(['string', 'string', 'string', 'string', 'string']);
public constructor(
public position: Vec2,
public velocity: Vec2,
) {}

static parse(input: string): MotionVector {
const split = MotionVector.inputType.assert(input.split(/[<>]/));
return new MotionVector(new Vec2(split[1] as Vec2String), new Vec2(split[3] as Vec2String));
}

public move(): Vec2 {
return this.position.addMut(this.velocity);
}

public toString(): string {
return `<${this.position.toString()}> , <${this.velocity.toString()}>`;
}
}
22 changes: 0 additions & 22 deletions solutions/typescript/2018/10/src/model/vector.class.ts

This file was deleted.

47 changes: 16 additions & 31 deletions solutions/typescript/2018/10/src/p1.ts
Original file line number Diff line number Diff line change
@@ -1,43 +1,26 @@
import { task } from '@alexaegis/advent-of-code-lib';
import { max, min } from '@alexaegis/advent-of-code-lib/math';
import { BoundingBox, Vec2, task } from '@alexaegis/advent-of-code-lib';
import packageJson from '../package.json';
import type { Boundary } from './boundary.interface.js';
import { interpreter } from './interpreter.function.js';
import { Coord } from './model/coord.class.js';
import type { Vector } from './model/vector.class.js';

export const normalize = (input: Vector[]): Vector[] => {
const { minX, minY } = boundary(input);
const norm = new Coord(minX, minY);
import type { MotionVector } from './model/motion-vector.class.js';

export const normalize = (input: MotionVector[]): MotionVector[] => {
const aabb = BoundingBox.fromVectors(input.map((i) => i.position));
return input.map((vector) => {
vector.position.sub(norm);
vector.position.subMut(aabb.size);
return vector;
});
};

export const area = (b: Boundary): number => (b.maxX - b.minX) * (b.maxY - b.minY); // did not terminate correctly
export const verticalArea = (b: Boundary): number => b.maxY - b.minY;

export const boundary = (input: Vector[]): Boundary => {
return {
maxX: input.map((vector) => vector.position.x).reduce(max),
minX: input.map((vector) => vector.position.x).reduce(min),
maxY: input.map((vector) => vector.position.y).reduce(max),
minY: input.map((vector) => vector.position.y).reduce(min),
};
};

export const print = (input: Vector[]): string => {
const { maxX, minX, maxY, minY } = boundary(input);
console.log(`maxX: ${maxX}, minX: ${minX}, maxY: ${maxY}, minY: ${minY}`);

export const print = (input: MotionVector[]): string => {
const aabb = BoundingBox.fromVectors(input.map((i) => i.position));
const stars = new Set(input.map((vector) => vector.position.toString()));

let pic = '';
for (let y = minY; y <= maxY; y++) {
for (let y = aabb.top; y <= aabb.bottom; y++) {
let row = '';
for (let x = minX; x <= maxX; x++) {
row = row + stars.has(new Coord(x, y).toString()) ? '#' : '.';
for (let x = aabb.left; x <= aabb.right; x++) {
row = row + stars.has(new Vec2(x, y).toString()) ? '#' : '.';
}
pic = pic + row + '\n';
}
Expand All @@ -46,13 +29,15 @@ export const print = (input: Vector[]): string => {

export const p1 = (input: string): number => {
const vectors = interpreter(input);
let minArea: number = area(boundary(vectors));
const aabb = BoundingBox.fromVectors(vectors.map((i) => i.position));
let minArea: number = aabb.area();
let i = 0;
for (;;) {
for (const vector of vectors) {
vector.position.add(vector.velocity);
vector.position.addMut(vector.velocity);
}
const currArea = verticalArea(boundary(vectors));
const aabb = BoundingBox.fromVectors(vectors.map((i) => i.position));
const currArea = aabb.vertical.length;
if (minArea > currArea) {
minArea = currArea;
} else break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ import type { Vec2Like } from './vec2.class.types.js';
export type BoundingBoxCorner = 'topLeft' | 'topRight' | 'bottomLeft' | 'bottomRight';

/**
* TODO: Rename to Area
*
* It consists of two closed intervals from left to right and bottom to top
*
* It uses a screen coordinate system where 0,0 is at the top left, and Y
Expand Down Expand Up @@ -176,6 +174,10 @@ export class BoundingBox {
}
}

area(): number {
return this.horizontal.length * this.vertical.length;
}

forEach(callback: (x: number, y: number) => void, resolution = 1): void {
if (this.isFinite()) {
for (let y = this.top; y <= this.bottom; y += resolution) {
Expand Down

0 comments on commit 578eee0

Please sign in to comment.