-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path10-2.ts
43 lines (34 loc) · 832 Bytes
/
10-2.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
export default function main(input: string): string {
const grid = input.split('\n');
let count = 0;
for (let i = 0; i < grid.length; i++) {
for (let j = 0; j < grid[0].length; j++) {
if (grid[i][j] === '0') {
traverse(i, j);
}
}
}
function traverse(i: number, j: number) {
const ninePos: string[] = [];
dfs(i, j);
function dfs(i: number, j: number) {
if (grid[i][j] === '9') {
ninePos.push(`${i} ${j}`);
return;
}
const next = +grid[i][j] + 1;
for (const dir of [
[0, 1],
[1, 0],
[0, -1],
[-1, 0],
]) {
if (grid[i + dir[0]]?.[j + dir[1]] === next.toString()) {
dfs(i + dir[0], j + dir[1]);
}
}
}
count += ninePos.length;
}
return count.toString();
}