-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path16-1.ts
44 lines (35 loc) · 1.21 KB
/
16-1.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
export default function main(input: string): string {
const grid = input.split('\n').map((l) => l.split(''));
// key = position id (pos[0] * grid.length + pos[1])
// value = array of directions
const visited = new Map<number, [number, number][]>();
recurse([0, 0], [0, 1]);
return visited.size.toString();
function recurse(pos: [number, number], dir: [number, number]) {
const char = grid[pos[0]]?.[pos[1]];
if (char === undefined) return;
const posId = pos[0] * grid.length + pos[1];
if (visited.has(posId)) {
if (visited.get(posId)!.some((d) => d[0] === dir[0] && d[1] === dir[1])) {
// already been here
return;
} else {
visited.get(posId)!.push([...dir]);
}
} else {
visited.set(posId, [[...dir]]);
}
if (char === '\\') {
[dir[0], dir[1]] = [dir[1], dir[0]];
} else if (char === '/') {
[dir[0], dir[1]] = [-dir[1], -dir[0]];
} else if (char === '|' && dir[0] === 0) {
recurse([pos[0] - 1, pos[1]], [-1, 0]);
dir = [1, 0];
} else if (char === '-' && dir[1] === 0) {
recurse([pos[0], pos[1] - 1], [0, -1]);
dir = [0, 1];
}
recurse([pos[0] + dir[0], pos[1] + dir[1]], [...dir]);
}
}