-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday10.ts
54 lines (46 loc) · 1.12 KB
/
day10.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
import { input } from './input';
const myGrid = input.split('\n').map((r) => r.split('').map(Number));
function part1(grid: number[][], cntPeaks: boolean = true) {
const dirs: Array<[number, number]> = [
[0, 1],
[1, 0],
[-1, 0],
[0, -1],
];
function walk(x: number, y: number, peaks: Set<string>) {
const c = grid[x]?.[y];
if (c == null) {
// OOB
return 0;
}
if (c === 9) {
if (cntPeaks && peaks.has(`${x},${y}`)) {
return 0;
}
peaks.add(`${x},${y}`);
return 1;
}
let cnt = 0;
for (let [dx, dy] of dirs) {
const nextChar = grid[x + dx]?.[y + dy];
if (nextChar === c + 1) {
cnt += walk(x + dx, y + dy, peaks);
}
}
return cnt;
}
let score = 0;
for (let i = 0; i < grid.length; i++) {
const row = grid[i];
for (let j = 0; j < row.length; j++) {
const c = row[j];
if (c === 0) {
const current = walk(i, j, new Set<string>());
score += current;
}
}
}
return score;
}
console.log(`unique peaks ${part1(myGrid)}`);
console.log(`trails ${part1(myGrid, false)}`);