-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathpart-one.ts
55 lines (48 loc) · 1.68 KB
/
part-one.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import { input } from './input';
import { InfiniteGrid, type GridId } from './infinite-grid';
const grid = new InfiniteGrid({ load: input, parseAs: Number });
const hikingTrails = grid.findAll(0);
function findTrailheads(id: GridId, grid: InfiniteGrid<number>) {
const findTrailheadsDFS = (
currentPath: Array<GridId>,
allPaths: Array<Array<GridId>>
): Array<Array<GridId>> => {
const lastId = currentPath.at(-1)!;
const x = InfiniteGrid.toXCoord(lastId);
const y = InfiniteGrid.toYCoord(lastId);
const value = grid.get(x, y);
if (value === 9) {
// At the end of the trail, save it
allPaths.push(currentPath);
} else {
for (let neighbor of grid.neighbors(x, y).values()) {
if (neighbor.value === value + 1) {
// Valid next step, recurse down this new path from that neighbor
findTrailheadsDFS([...currentPath, neighbor.id], allPaths);
}
}
}
return allPaths;
};
// Start the walk from the `id`
const trailheads = findTrailheadsDFS([id], []);
return trailheads;
}
let trailheadSum = 0;
for (let { id } of hikingTrails) {
const trailheads = findTrailheads(id, grid);
/**
* > A trailhead's score is the number of 9-height positions reachable
* > from that trailhead via a hiking trail.
*
* `trailheads` are all the full valid paths to the end of the trail.
* However, for part one, we only care about how many _endings_ the trail
* has, now how many ways there are to get to that end.
*
* So, pop off the ending coord (the `9` height cell) and count the
* number of unique peaks we have to add to our sum.
*/
const uniquePeaks = new Set(trailheads.map((trail) => trail.at(-1)));
trailheadSum += uniquePeaks.size;
}
console.log(trailheadSum);