Skip to content

Commit 9fff6eb

Browse files
committed
[ts] 2024:04: Unify day04 parts
1 parent ede7948 commit 9fff6eb

File tree

2 files changed

+29
-85
lines changed

2 files changed

+29
-85
lines changed

2024/day04/part2.ts 2024/day04/day04.ts

+29-16
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@ const inputLines = (await Bun.file("input").text()).trim().split(EOL);
66
type Grid = Array<Array<string>>;
77
type Vector = [number, number]; // [x, y]
88

9-
const SEARCH_STRING = "MAS";
10-
const VALID_DIRECTIONS: Array<Vector> = [
11-
[-1, -1], [1, -1], [-1, 1], [1, 1] // Diagonals
12-
];
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]]
1311

14-
const HINT_SYMBOL = SEARCH_STRING.split("")[1];
12+
function VALID_DIRECTIONS(part2: boolean): Array<Vector> {
13+
return part2 ? DIAGONAL_DIRECTIONS : STRAIGHT_DIRECTIONS.concat(DIAGONAL_DIRECTIONS);
14+
}
1515

1616
function vecAdd(one: Vector, two: Vector): Vector {
1717
return [one[0] + two[0], one[1] + two[1]];
@@ -32,40 +32,53 @@ function isInBounds(point: Vector, grid: Grid) {
3232
)
3333
}
3434

35-
function locateHints(grid: Grid): Array<Vector> {
35+
function locateHints(grid: Grid, hintSymbol: String): Array<Vector> {
3636
const hints: Array<Vector> = [];
3737
for (let y: number = 0; y < grid.length; y++) {
3838
const line = grid[y];
3939
for (let x: number = 0; x < line.length; x++) {
40-
if (line[x] == HINT_SYMBOL) hints.push([x, y]);
40+
if (line[x] == hintSymbol) hints.push([x, y]);
4141
}
4242
}
4343
return hints;
4444
}
4545

46-
function investigateHint(hint: Vector): boolean {
46+
function investigateHint(hint: Vector, searchString: String, part2: boolean = false): number {
4747
let hitCount: number = 0;
48-
for (let searchDirection of VALID_DIRECTIONS) {
48+
for (let searchDirection of VALID_DIRECTIONS(part2)) {
4949
const directionString: Array<String> = [];
5050

51-
let searchLocation = vecAdd(hint, vecNeg(searchDirection));
51+
let searchLocation = part2 ? vecAdd(hint, vecNeg(searchDirection)) : hint;
5252
let searchStringIndex = 0;
5353
while (
5454
isInBounds(searchLocation, inputGrid)
55-
&& searchStringIndex < SEARCH_STRING.length
55+
&& searchStringIndex < searchString.length
5656
) {
5757
// TODO: Maybe fail the direction on first wrong letter?
5858

5959
directionString.push(getFromGrid(searchLocation, inputGrid));
6060
searchLocation = vecAdd(searchLocation, searchDirection);
6161
searchStringIndex += 1;
6262
}
63-
if (directionString.join("") == SEARCH_STRING) hitCount += 1;
63+
if (directionString.join("") == searchString) hitCount += 1;
6464
}
65-
return hitCount == 2;
65+
if (part2) return Number(hitCount == 2);
66+
else return hitCount;
6667
}
6768

6869
const inputGrid: Grid = inputLines.map((inputLine: string) => inputLine.split(""));
69-
const hints: Array<Vector> = locateHints(inputGrid);
70-
const totalHitCount = hints.map(investigateHint).reduce((acc, didHit) => didHit ? acc + 1 : acc, 0);
71-
console.log("Part 2:", totalHitCount);
70+
71+
function part1() {
72+
const hints: Array<Vector> = locateHints(inputGrid, "X");
73+
const hitCounts: Array<number>= hints.map((hint) => investigateHint(hint, "XMAS"));
74+
return hitCounts.reduce((acc, hitCount) => acc + hitCount);
75+
}
76+
77+
function part2() {
78+
const hints: Array<Vector> = locateHints(inputGrid, "A");
79+
const hitCounts: Array<number> = hints.map((hint) => investigateHint(hint, "MAS", true));
80+
return hitCounts.reduce((acc, hitCount) => acc + hitCount);
81+
}
82+
83+
console.log("Part 1:", part1());
84+
console.log("Part 2:", part2());

2024/day04/part1.ts

-69
This file was deleted.

0 commit comments

Comments
 (0)