Skip to content

Commit 3e60e7e

Browse files
committed
[ts] 2024:04: Improve day04 vector handling
1 parent 9fff6eb commit 3e60e7e

File tree

1 file changed

+21
-18
lines changed

1 file changed

+21
-18
lines changed

2024/day04/day04.ts

+21-18
Original file line numberDiff line numberDiff line change
@@ -4,31 +4,34 @@ import { EOL } from "node:os";
44
const inputLines = (await Bun.file("input").text()).trim().split(EOL);
55

66
type Grid = Array<Array<string>>;
7-
type Vector = [number, number]; // [x, y]
87

9-
const STRAIGHT_DIRECTIONS: Array<Vector> = [[-1, 0], [1, 0], [0, -1], [0, 1]]
10-
const DIAGONAL_DIRECTIONS: Array<Vector> = [[-1, -1], [1, -1], [-1, 1], [1, 1]]
8+
class Vector {
9+
x: number;
10+
y: number;
1111

12-
function VALID_DIRECTIONS(part2: boolean): Array<Vector> {
13-
return part2 ? DIAGONAL_DIRECTIONS : STRAIGHT_DIRECTIONS.concat(DIAGONAL_DIRECTIONS);
14-
}
12+
constructor(x: number, y: number) {
13+
this.x = x;
14+
this.y = y;
15+
}
1516

16-
function vecAdd(one: Vector, two: Vector): Vector {
17-
return [one[0] + two[0], one[1] + two[1]];
17+
add(other: Vector): Vector { return new Vector(this.x + other.x, this.y + other.y); }
18+
neg(): Vector { return new Vector(this.x * -1, this.y * -1); }
1819
}
20+
const vec = (x: number, y: number) => new Vector(x, y);
1921

20-
function vecNeg(vec: Vector): Vector {
21-
return [vec[0] * -1, vec[1] * -1];
22-
}
22+
const STRAIGHT_DIRECTIONS: Array<Vector> = [vec(-1, 0), vec(1, 0), vec(0, -1), vec(0, 1)];
23+
const DIAGONAL_DIRECTIONS: Array<Vector> = [vec(-1, -1), vec(1, -1), vec(-1, 1), vec(1, 1)];
2324

24-
function getFromGrid(point: Vector, grid: Grid): String {
25-
return grid[point[1]][point[0]];
25+
function VALID_DIRECTIONS(part2: boolean): Array<Vector> {
26+
return part2 ? DIAGONAL_DIRECTIONS : STRAIGHT_DIRECTIONS.concat(DIAGONAL_DIRECTIONS);
2627
}
2728

29+
const getFromGrid = (point: Vector, grid: Grid): String => grid[point.y][point.x];
30+
2831
function isInBounds(point: Vector, grid: Grid) {
2932
return (
30-
point[1] >= 0 && point[1] < grid.length
31-
&& point[0] >= 0 && point[0] < grid[0].length
33+
point.y >= 0 && point.y < grid.length
34+
&& point.x >= 0 && point.x < grid[0].length
3235
)
3336
}
3437

@@ -37,7 +40,7 @@ function locateHints(grid: Grid, hintSymbol: String): Array<Vector> {
3740
for (let y: number = 0; y < grid.length; y++) {
3841
const line = grid[y];
3942
for (let x: number = 0; x < line.length; x++) {
40-
if (line[x] == hintSymbol) hints.push([x, y]);
43+
if (line[x] == hintSymbol) hints.push(new Vector(x, y));
4144
}
4245
}
4346
return hints;
@@ -48,7 +51,7 @@ function investigateHint(hint: Vector, searchString: String, part2: boolean = fa
4851
for (let searchDirection of VALID_DIRECTIONS(part2)) {
4952
const directionString: Array<String> = [];
5053

51-
let searchLocation = part2 ? vecAdd(hint, vecNeg(searchDirection)) : hint;
54+
let searchLocation = part2 ? hint.add(searchDirection.neg()) : hint;
5255
let searchStringIndex = 0;
5356
while (
5457
isInBounds(searchLocation, inputGrid)
@@ -57,7 +60,7 @@ function investigateHint(hint: Vector, searchString: String, part2: boolean = fa
5760
// TODO: Maybe fail the direction on first wrong letter?
5861

5962
directionString.push(getFromGrid(searchLocation, inputGrid));
60-
searchLocation = vecAdd(searchLocation, searchDirection);
63+
searchLocation = searchLocation.add(searchDirection);
6164
searchStringIndex += 1;
6265
}
6366
if (directionString.join("") == searchString) hitCount += 1;

0 commit comments

Comments
 (0)