-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday23.ts
260 lines (223 loc) · 6.29 KB
/
day23.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
function log(u: unknown) {
console.log(JSON.stringify(u, undefined, 2));
}
type Coords = [x: number, y: number];
function printGrid(output: string[][]) {
log({
o: output.map((r, i) => `${('000' + i).substring(-2)} ${r.join('')}`),
});
}
const test_input = `#.#####################
#.......#########...###
#######.#########.#.###
###.....#.>.>.###.#.###
###v#####.#v#.###.#.###
###.>...#.#.#.....#...#
###v###.#.#.#########.#
###...#.#.#.......#...#
#####.#.#.#######.#.###
#.....#.#.#.......#...#
#.#####.#.#.#########v#
#.#...#...#...###...>.#
#.#.#v#######v###.###v#
#...#.>.#...>.>.#.###.#
#####v#.#.###v#.#.###.#
#.....#...#...#.#.#...#
#.#########.###.#.#.###
#...###...#...#...#.###
###.###.#.###v#####v###
#...#...#.#.>.>.#.>.###
#.###.###.#.###.#.#v###
#.....###...###...#...#
#####################.#`
.split('\n')
.map((r) => r.trim());
const puzzle_input = `<get from aoc`.split('\n').map((r) => r.trim());
const directions: Coords[] = [
[1, 0],
[0, 1],
[-1, 0],
[0, -1],
];
const slides = 'v>^<'; // order matches order of directions
type QueueItem = { c: Coords; dir: Coords; sum: number; visited: string[] };
function getKey(c: Coords): string {
return c[0] + '_' + c[1];
}
function addCoords(a: Coords, b: Coords): Coords {
return [a[0] + b[0], a[1] + b[1]];
}
function reverseCoords(a: Coords): Coords {
return [a[0] * -1, a[1] * -1];
}
function doWork(input: readonly string[], dry: boolean) {
log(`size ${input.length} x ${input[0].length}`);
const output = input.map((r) => r.split(''));
const findNext = (start: Coords, dir: Coords): Coords[] => {
const result: Coords[] = [];
for (let i = 0; i < 4; i++) {
const d: Coords = directions[i];
// backwards
if (dir[0] !== dir[1] && dir[0] === -1 * d[0] && dir[1] === -1 * d[1]) {
continue;
}
const c: Coords = addCoords(start, d);
const tile = input[c[0]]?.[c[1]];
if (
tile === '.' ||
tile === slides[i] ||
(dry && slides.includes(tile))
) {
result.push(d);
}
}
return result;
};
const start: Coords = [0, input[0].indexOf('.')];
const startKey = getKey(start);
const end: Coords = [input.length - 1, input[input.length - 1].indexOf('.')];
const endKey = getKey(end);
const buildGraph = () => {
const nodes: Map<string, [key: string, cnt: number][]> = new Map();
// q stores intersection (or start) and valid next directions
const q: [Coords, Coords][] = [];
const validDirs = findNext(start, [0, 0]);
for (const n of validDirs) {
q.push([start, n]);
}
while (q.length > 0) {
const c = q.pop();
if (c == null) {
continue;
}
const visited: Set<string> = new Set();
const [node, dir] = c;
output[node[0]][node[1]] = 'O';
let sum = 0;
visited.add(getKey(node));
let current = node;
let currentDir = dir;
let nd = [dir];
while (nd.length === 1) {
const next = addCoords(current, nd[0]);
if (visited.has(getKey(next))) {
break;
}
sum++;
current = next;
currentDir = nd[0];
nd = findNext(current, nd[0]);
}
const knownNode = nodes.has(getKey(current));
const dest = nodes.get(getKey(node)) ?? [];
dest.push([getKey(current), sum]);
if (!knownNode) {
q.push([current, reverseCoords(currentDir)]);
}
for (let ndi of nd) {
const next = addCoords(current, ndi);
if (visited.has(getKey(next))) {
continue;
}
if (!knownNode) {
q.push([current, ndi]);
}
}
nodes.set(getKey(node), dest);
}
return nodes;
};
const findPath: (
graph: Map<string, [key: string, cnt: number][]>,
start: string,
sum: number,
visited: string[],
) => number = (
graph: Map<string, [key: string, cnt: number][]>,
start: string,
sum: number,
visited: string[],
) => {
// not super fast, but acceptable enough
if (start === endKey) {
return sum;
}
const destinations = graph.get(start);
if (
destinations == null ||
destinations.length == 0 ||
visited.includes(start)
) {
return 0;
}
visited.push(start);
return destinations
.map((d) => findPath(graph, d[0], sum + d[1], [...visited]))
.reduce((a, c) => Math.max(a, c), 0);
};
let max: number = 0;
if (dry) {
// part 2
const graphNodes = buildGraph();
for (const [k, v] of graphNodes) {
log(`${k} >> ${v.map((i) => JSON.stringify(i)).join(',')}`);
}
max = findPath(graphNodes, startKey, 0, []);
// printGrid(output);
}
if (!dry) {
const q: Array<QueueItem> = [
{ c: start, dir: [0, 0], sum: 0, visited: [getKey(start)] },
];
let cnt = 0;
while (q.length > 0) {
if (cnt % 1000 == 0) log(`cnt ${cnt}`);
cnt++;
const current = q.pop();
if (current == null) {
throw new Error('null in q');
}
let { c: start, dir, sum, visited: v } = current;
const visited = new Set(v);
let nextDirs = findNext(start, dir);
let loc = start;
while (nextDirs.length > 0) {
if (nextDirs.length === 0) {
break;
}
if (nextDirs.length === 1) {
const d = [addCoords(loc, nextDirs[0]), nextDirs[0]];
if (visited.has(getKey(d[0]))) {
break;
}
sum++;
if (d[0][0] === end[0] && d[0][1] === end[1]) {
max = Math.max(max, sum);
break;
}
visited.add(getKey(d[0]));
// output[d[0][0]][d[0][1]] = 'O';
loc = d[0];
nextDirs = findNext(d[0], d[1]);
continue;
}
if (nextDirs.length > 1) {
const vt = Array.from(visited.values());
for (const d of nextDirs) {
const next = addCoords(loc, d);
if (!visited.has(getKey(next))) {
vt.push(getKey(next));
q.push({ c: next, dir: d, sum: sum + 1, visited: vt });
}
}
break;
}
}
}
}
log(`longest ${max}`);
}
// doWork(test_input, false);
// doWork(test_input, true);
// doWork(puzzle_input, false); // part1
doWork(puzzle_input, true); // part2