Skip to content

Commit a6354ca

Browse files
committed
AoC2025: day 7 part 1
1 parent 0227e23 commit a6354ca

4 files changed

Lines changed: 106 additions & 1 deletion

File tree

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import {part1, part2} from "./day07";
2+
3+
describe('2025 Day 7', () => {
4+
test('Part 1', async () => {
5+
expect(await part1('testInput1')).toEqual(21);
6+
expect(await part1('input')).toEqual(1598);
7+
});
8+
9+
test('Part 2', async () => {
10+
// expect(await part2('testInput1')).toEqual(31);
11+
// expect(await part2('input')).toEqual(29379307);
12+
});
13+
});

2024-2025/src/2025/day07/day07.ts

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import path from "node:path";
2+
import {readInputLineByLine} from "@utils/io";
3+
import {Cardinal, Coord, Grid, move, readLinesToGrid} from "@utils/grid";
4+
5+
export async function part1(inputFile: string) {
6+
return await day7(inputFile, findBeanSplits);
7+
}
8+
9+
export async function part2(inputFile: string) {
10+
return await day7(inputFile);
11+
}
12+
13+
async function day7(inputFile: string, calcFn?: (grid: Grid, initialPos: Coord) => number) {
14+
const inputPath = path.join(__dirname, inputFile);
15+
const lines = await readInputLineByLine(inputPath);
16+
const { grid, initialPos } = readLinesToGrid(lines, 'S');
17+
return calcFn?.(grid, initialPos!);
18+
}
19+
20+
function findBeanSplits(grid: Grid, initialPos: Coord): number {
21+
const visited = new Set<string>();
22+
const queue: Coord[] = [move(initialPos, Cardinal.SOUTH)];
23+
let splits = 0;
24+
25+
while (queue.length > 0) {
26+
let current = queue.shift()!;
27+
28+
while (true) {
29+
const key = current.serialize();
30+
const cellValue = grid.get(key);
31+
32+
if (cellValue === undefined || visited.has(key)) break;
33+
34+
visited.add(key);
35+
36+
if (cellValue === '^') {
37+
splits++;
38+
const left = move(current, Cardinal.WEST);
39+
const right = move(current, Cardinal.EAST);
40+
if (grid.has(left.serialize()))
41+
queue.push(left);
42+
if (grid.has(right.serialize()))
43+
queue.push(right);
44+
break;
45+
}
46+
47+
current = move(current, Cardinal.SOUTH);
48+
}
49+
}
50+
51+
return splits;
52+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
.......S.......
2+
...............
3+
.......^.......
4+
...............
5+
......^.^......
6+
...............
7+
.....^.^.^.....
8+
...............
9+
....^.^...^....
10+
...............
11+
...^.^...^.^...
12+
...............
13+
..^...^.....^..
14+
...............
15+
.^.^.^.^.^...^.
16+
...............

2024-2025/src/utils/grid.ts

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,30 @@ export interface Coord {
2626

2727
export type Grid = Map<string, string>;
2828

29+
export function getMaxX(grid: Grid): number {
30+
let max = Number.NEGATIVE_INFINITY;
31+
for (const key of grid.keys()) {
32+
const { x } = Coord.deserialize(key);
33+
if (x > max) max = x;
34+
}
35+
if (max === Number.NEGATIVE_INFINITY) {
36+
throw new Error('Grid is empty');
37+
}
38+
return max;
39+
}
40+
41+
export function getMaxY(grid: Grid): number {
42+
let max = Number.NEGATIVE_INFINITY;
43+
for (const key of grid.keys()) {
44+
const { y } = Coord.deserialize(key);
45+
if (y > max) max = y;
46+
}
47+
if (max === Number.NEGATIVE_INFINITY) {
48+
throw new Error('Grid is empty');
49+
}
50+
return max;
51+
}
52+
2953
export function readLinesToGrid(lines: string[], initialPosId?: string, endPosId?: string) {
3054
const grid: Grid = new Map();
3155
let initialPos: Coord | undefined = undefined;
@@ -192,4 +216,4 @@ export function findCoordsWithinManhattanDistance(source: Coord, distance: numbe
192216
}
193217

194218
return coordinates;
195-
}
219+
}

0 commit comments

Comments
 (0)