-
Notifications
You must be signed in to change notification settings - Fork 0
/
part2.ts
87 lines (76 loc) · 2.52 KB
/
part2.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
import * as fs from 'fs';
type Valve = {
id: string;
rate: number;
destinations: string[];
paths: Path[];
};
type Path = { id: string; distance: number };
const input = fs.readFileSync('input', 'utf8').split('\n');
const getDistance = (valves: Record<string, Valve>, from: string, to: string) => {
const queue = [{ id: from, steps: 0 }];
const visited = new Set(`${from},0`);
while (queue.length > 0) {
const current = queue.shift();
if (current.id === to) {
return current.steps;
}
const neighbors = valves[current.id].destinations
.map((id) => ({ id, steps: current.steps + 1 }))
.filter((x) => !visited.has(`${x.id},${x.steps}`));
neighbors.forEach((x) => visited.add(`${x.id},${x.steps}`));
queue.push(...neighbors);
}
};
const valves: Record<string, Valve> = input
.map((line) => {
const [, id, rate, destinations] = line.match(
/^Valve ([^\s]+) has flow rate=(\d+); tunnels? leads? to valves? (.+)$/,
);
return { id, rate: +rate, destinations: destinations.split(', ') };
})
.reduce((obj, valve) => ({ ...obj, [valve.id]: valve }), {});
Object.values(valves).forEach((valve) => {
valve.paths = Object.values(valves)
.filter(({ id, rate }) => id !== valve.id && rate > 0)
.map(({ id }) => ({ id, distance: getDistance(valves, valve.id, id) }));
});
const validValveIds = Object.keys(valves).filter((id) => valves[id].rate > 0);
const possiblePaths = validValveIds
.reduce((paths, id) => paths.concat(paths.map((set) => [...set, id])), [[]])
.slice(1);
const validPathPairs = [];
for (let i = 0; i < possiblePaths.length; i++) {
const j = possiblePaths.findIndex(
(path) =>
possiblePaths[i].length + path.length === validValveIds.length &&
new Set(possiblePaths[i].concat(path)).size === validValveIds.length,
);
if (j !== -1) {
validPathPairs.push([possiblePaths[i], possiblePaths[j]]);
possiblePaths.splice(j, 1);
}
}
const findBest = (
valves: Record<string, Valve>,
current: string,
open: Set<string>,
time: number,
) => {
if (time === 0) {
return 0;
}
const results = valves[current].paths
.filter(({ id, distance }) => !open.has(id) && time > distance)
.map(({ id, distance }) => {
const remaining = time - distance - 1;
const pressure = valves[id].rate * remaining;
return pressure + findBest(valves, id, new Set(open).add(id), remaining);
});
return Math.max(0, ...results);
};
const results = validPathPairs.map(
([human, elephant]) =>
findBest(valves, 'AA', new Set(human), 26) + findBest(valves, 'AA', new Set(elephant), 26),
);
console.log(Math.max(...results));