-
Notifications
You must be signed in to change notification settings - Fork 0
/
a.test.ts
193 lines (146 loc) · 5.55 KB
/
a.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
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
import { expect, test } from "bun:test";
function countIslands(graph: Record<string, string[]>) {
const visited = new Set<string>();
const islands: Set<string>[] = [];
function dfs(nodes: string[]) {
const queue = nodes.slice();
while (queue.length > 0) {
const currentNode = queue.shift();
if (!currentNode) throw new Error("Unexpected");
if (visited.has(currentNode)) continue;
visited.add(currentNode);
islands[islands.length - 1].add(currentNode);
queue.push(...graph[currentNode]);
}
}
for (const node in graph) {
if (!visited.has(node)) {
islands.push(new Set([node]));
dfs(graph[node]);
}
}
return islands;
}
function solution(input: string) {
const instr = input.split("\n").map((line) => {
const [name, connectionsString] = line.split(": ");
const connections = connectionsString.split(" ");
return [name, connections] as const;
});
const graph = instr.reduce<Record<string, string[]>>((acc, [node, neighbours]) => {
if (!acc[node]) {
acc[node] = neighbours.slice();
} else {
acc[node].push(...neighbours);
}
for (const neighbour of neighbours) {
if (acc[neighbour]) {
acc[neighbour].push(node);
} else {
acc[neighbour] = [node];
}
}
return acc;
}, {});
const paths: Record<string, Record<string, string[]>> = {};
let stopCount = 12;
for (const nodeA in graph) {
paths[nodeA] = {};
for (const nodeB in graph) {
if (nodeA === nodeB) continue;
function bfs(startNode: string, endNode: string) {
const queue = [startNode];
const visited = new Set([startNode]);
const previousNodes: Record<string, string> = {};
while (queue.length > 0) {
const currentNode = queue.shift();
if (!currentNode) throw new Error("Unexpected");
if (currentNode === endNode) {
const path = [endNode];
let cur = endNode;
while (cur !== startNode) {
cur = previousNodes[cur];
path.unshift(cur);
}
return path;
}
for (const neighbor of graph[currentNode]) {
if (!visited.has(neighbor)) {
visited.add(neighbor);
queue.push(neighbor);
previousNodes[neighbor] = currentNode;
}
}
}
throw new Error("No path found");
}
if (paths[nodeB]?.[nodeA]) {
paths[nodeA][nodeB] = paths[nodeB][nodeA];
continue;
}
paths[nodeA][nodeB] = bfs(nodeA, nodeB);
}
if (stopCount-- < 0) {
break;
}
}
const count: Record<string, Record<string, number>> = {};
for (const nodeA in paths) {
for (const nodeB in paths[nodeA]) {
const path = paths[nodeA][nodeB];
for (let i = 0; i < path.length - 1; i++) {
const edgeStart = path[i];
const edgeEnd = path[i + 1];
if (!count[edgeStart]) {
count[edgeStart] = {};
}
if (!count[edgeStart][edgeEnd]) {
count[edgeStart][edgeEnd] = 0;
}
count[edgeStart][edgeEnd] += 1;
}
}
}
const flatCount: [number, string, string][] = [];
for (const nodeA in count) {
for (const nodeB in count[nodeA]) {
flatCount.push([count[nodeA][nodeB], nodeA, nodeB]);
}
}
const sorted = flatCount.sort((a, b) => b[0] - a[0]);
for (let i = 0; i < sorted.length; i++) {
for (let j = i + 1; j < sorted.length; j++) {
for (let k = j + 1; k < sorted.length; k++) {
const [_, ...edge1] = sorted[i];
const [__, ...edge2] = sorted[j];
const [___, ...edge3] = sorted[k];
const g = { ...graph };
g[edge1[0]] = g[edge1[0]].filter((node) => node !== edge1[1]);
g[edge1[1]] = g[edge1[1]].filter((node) => node !== edge1[0]);
g[edge2[0]] = g[edge2[0]].filter((node) => node !== edge2[1]);
g[edge2[1]] = g[edge2[1]].filter((node) => node !== edge2[0]);
g[edge3[0]] = g[edge3[0]].filter((node) => node !== edge3[1]);
g[edge3[1]] = g[edge3[1]].filter((node) => node !== edge3[0]);
const islands = countIslands(g);
if (islands.length === 2) {
return islands.reduce((acc, nodes) => acc * nodes.size, 1);
}
}
}
}
throw new Error("Unexpected");
}
test("example", async () => {
const file = Bun.file(`${import.meta.dir}/example.txt`);
const input = await file.text();
const actual = solution(input);
const expected = 54;
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 = 507626;
expect(actual).toBe(expected);
});