-
Notifications
You must be signed in to change notification settings - Fork 0
/
day16.ts
160 lines (136 loc) · 4 KB
/
day16.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
import _ from "lodash";
import fs from "fs";
const grid: string[] = _.reject(
fs.readFileSync("inputs/day16.txt", "utf-8").split("\n"),
_.isEmpty
);
enum Direction {
North = 0,
East = 1,
South = 2,
West = 3,
}
enum Cell {
Empty = ".",
MirrorLeft = "/",
MirrorRight = "\\",
SplitterHorizontal = "-",
SplitterVertical = "|",
}
const height = grid.length;
const width = grid[0].length;
const adjacent: [number, number, number][] = [
[-1, 0, Direction.North],
[0, 1, Direction.East],
[1, 0, Direction.South],
[0, -1, Direction.West],
];
const visited: boolean[][][] = _.times(height, () =>
_.times(width, () => [false, false, false, false])
);
const countVisited = (): number =>
_.sum(
visited.map((row) =>
_.sum(row.map((column) => column.some((d) => d === true)))
)
);
const clearVisited = (): void => {
for (let row = 0; row < height; row++) {
for (let column = 0; column < width; column++) {
for (let direction = 0; direction < 4; direction++) {
visited[row][column][direction] = false;
}
}
}
};
const dfs = (row: number, column: number, direction: Direction): void => {
if (row < 0 || row >= height || column < 0 || column >= width) {
return;
}
if (visited[row][column][direction]) {
return;
}
visited[row][column][direction] = true;
switch (grid[row][column]) {
case Cell.Empty: {
const [dr, dc, _] = adjacent.find(([_dr, _dc, d]) => d === direction)!;
dfs(row + dr, column + dc, direction);
break;
}
case Cell.MirrorLeft: {
const mapping: Record<Direction, Direction> = {
[Direction.North]: Direction.East,
[Direction.East]: Direction.North,
[Direction.South]: Direction.West,
[Direction.West]: Direction.South,
};
const [dr, dc, newD] = adjacent.find(
([_dr, _dc, d]) => d === mapping[direction]
)!;
dfs(row + dr, column + dc, newD);
break;
}
case Cell.MirrorRight: {
const mapping: Record<Direction, Direction> = {
[Direction.North]: Direction.West,
[Direction.East]: Direction.South,
[Direction.South]: Direction.East,
[Direction.West]: Direction.North,
};
const [dr, dc, newD] = adjacent.find(
([_dr, _dc, d]) => d === mapping[direction]
)!;
dfs(row + dr, column + dc, newD);
break;
}
case Cell.SplitterHorizontal: {
if (direction === Direction.North || direction === Direction.South) {
for (const newD of [Direction.East, Direction.West]) {
const [dr, dc, _] = adjacent.find(([_dr, _dc, d]) => d === newD)!;
dfs(row + dr, column + dc, newD);
}
} else {
const [dr, dc, _] = adjacent.find(([_dr, _dc, d]) => d === direction)!;
dfs(row + dr, column + dc, direction);
}
break;
}
case Cell.SplitterVertical: {
if (direction === Direction.East || direction === Direction.West) {
for (const newD of [Direction.North, Direction.South]) {
const [dr, dc, _] = adjacent.find(([_dr, _dc, d]) => d === newD)!;
dfs(row + dr, column + dc, newD);
}
} else {
const [dr, dc, _] = adjacent.find(([_dr, _dc, d]) => d === direction)!;
dfs(row + dr, column + dc, direction);
}
break;
}
default: {
throw new Error(`Unknown cell type: ${grid[row][column]}`);
}
}
};
dfs(0, 0, Direction.East);
// Part 1
console.log(countVisited());
let best = 0;
for (let entryRow = 0; entryRow < height; entryRow++) {
clearVisited();
dfs(entryRow, 0, Direction.East);
best = Math.max(best, countVisited());
clearVisited();
dfs(entryRow, width - 1, Direction.West);
best = Math.max(best, countVisited());
}
for (let entryColumn = 0; entryColumn < width; entryColumn++) {
clearVisited();
dfs(0, entryColumn, Direction.South);
best = Math.max(best, countVisited());
clearVisited();
dfs(height - 1, entryColumn, Direction.North);
best = Math.max(best, countVisited());
}
// Part 2
console.log(best);