-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathb.test.ts
134 lines (101 loc) · 3.51 KB
/
b.test.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
import { expect, test } from "bun:test";
function toKey([r, c]: [number, number]): string {
return `${r},${c}`;
}
function fromKey(k: string): [number, number] {
return k.split(",").map(Number).slice(0, 2) as [number, number];
}
function solution(input: string) {
const grid = input.split("\n");
const start: [number, number] = [0, grid[0].indexOf(".")];
const end: [number, number] = [grid.length - 1, grid[grid.length - 1].indexOf(".")];
const forks = new Set([toKey(start), toKey(end)]);
for (let r = 0; r < grid.length; r++) {
for (let c = 0; c < grid[r].length; c++) {
const ch = grid[r][c];
if (ch === "#") continue;
let neighbours = 0;
for (const [dr, dc] of [
[r, c + 1],
[r, c - 1],
[r + 1, c],
[r - 1, c],
]) {
if (dr < 0 || dr >= grid.length || dc < 0 || dc >= grid[0].length) {
continue;
}
if (grid[dr][dc] !== "#") {
neighbours += 1;
}
}
if (neighbours >= 3) {
forks.add(toKey([r, c]));
}
}
}
const graph: Record<string, Record<string, number>> = {};
for (const key of forks) {
graph[key] = {};
}
for (const key of forks) {
const [r, c] = fromKey(key);
const stack: [number, number, number][] = [[r, c, 0]];
const seen = new Set([key]);
while (stack.length > 0) {
const stackItem = stack.pop();
if (!stackItem) throw new Error("Unexpected false");
const [r, c, n] = stackItem;
if (n !== 0 && forks.has(toKey([r, c]))) {
graph[key][toKey([r, c])] = n;
continue;
}
for (const [dr, dc] of [
[0, 1],
[0, -1],
[1, 0],
[-1, 0],
]) {
const nr = r + dr;
const nc = c + dc;
if (nr < 0 || nr >= grid.length || nc < 0 || nc >= grid[0].length) {
continue;
}
const nextPointString = toKey([nr, nc]);
if (grid[nr][nc] !== "#" && !seen.has(nextPointString)) {
stack.push([nr, nc, n + 1]);
seen.add(nextPointString);
}
}
}
}
const seen = new Set();
function dfs([r, c]: [number, number]) {
if (r === start[0] && c === start[1]) return 0;
const fromNodeString = toKey([r, c]);
let max = Number.NEGATIVE_INFINITY;
seen.add(fromNodeString);
for (const nodeString in graph[fromNodeString]) {
if (seen.has(nodeString)) continue;
const [nr, nc] = fromKey(nodeString);
const n = graph[fromNodeString][nodeString];
max = Math.max(max, dfs([nr, nc]) + n);
}
seen.delete(fromNodeString);
return max;
}
return dfs(end);
}
test("example", async () => {
const file = Bun.file(`${import.meta.dir}/example.txt`);
const input = await file.text();
const actual = solution(input);
const expected = 154;
expect(actual).toBe(expected);
});
test("puzzle input", async () => {
const file = Bun.file(`${import.meta.dir}/input.txt`);
const input = await file.text();
const actual = solution(input);
const expected = 6646;
expect(actual).toBe(expected);
});