-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday_16.mjs
129 lines (101 loc) · 4.19 KB
/
day_16.mjs
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
import * as fs from 'node:fs';
class Valve {
constructor(data) {
const [valveData, neighborData] = data.split('; ');
this.name = valveData.split(' ')[1];
this.flowRate = parseInt(valveData.split('=')[1]);
const tunnels = neighborData.split(' ').map(n => n.substring(0, 2)).slice(4);
this.edges = tunnels.reduce((acc, cur) => ({ ...acc, [cur]: { weight: 1 } }), {});
}
get neighbors() {
return Object.entries(this.edges).filter(([_, edge]) => edge.weight === 1);
};
}
const valves = fs
.readFileSync('input.txt', 'utf-8')
.trim()
.split('\n')
.map(info => new Valve(info));
const addNodeByName = ([name, edge]) => { edge.node = valves.find(v => v.name === name); };
const identifyAllEdgeNodes = valve => { Object.entries(valve.edges).forEach(addNodeByName); };
const findShortestPath = (nameA, nameB) => {
const startNode = valves.find(valve => valve.name === nameA);
const queue = [{ node: startNode, length: 0 }];
const visited = new Set([startNode.name]);
while (queue.length > 0) {
const { node, length } = queue.shift();
for (const [neighbor, { weight, node: neighborNode }] of node.neighbors) {
if (neighbor === nameB) return length + weight;
if (!visited.has(neighbor)) {
visited.add(neighbor);
queue.push({ node: neighborNode, length: length + weight });
}
}
}
return Number.MAX_VALUE;
};
const allValveNames = valves.map(valve => valve.name);
const addShortestPathToAllMissingNodes = () => {
valves.forEach(valve => {
allValveNames
.filter(name => name !== valve.name && !Object.keys(valve.edges).includes(name))
.forEach(neighbourName => {
valve.edges[neighbourName] = { weight: findShortestPath(valve.name, neighbourName) };
addNodeByName([neighbourName, valve.edges[neighbourName]]);
});
});
};
const zeroFlowRooms = allValveNames.filter(name => !valves.find(valve => valve.name === name).flowRate);
const deletePathsToZeroFlowRooms = () => {
valves.forEach(valve => zeroFlowRooms.forEach(target => {
delete valve.edges[target];
}));
};
const findHighestReleasePotential = (graph, startNode) => {
const numberOfValvesWithPositiveFlow = valves.length - zeroFlowRooms.length;
const queue = [{ node: startNode, minutesRemaining: 30, opened: {} }];
const quickestPathTo = {};
let highestReleasePotential = 0;
while (queue.length > 0) {
let { node: currentNode, minutesRemaining, opened } = queue.shift();
const logValue = () => {
const possibleRelease = Object
.values(opened)
.reduce((acc, { minutesRemaining, flowRate }) => acc + flowRate * minutesRemaining, 0);
highestReleasePotential = Math.max(highestReleasePotential, possibleRelease);
};
if (minutesRemaining === 0) {
logValue();
continue;
}
const hash = `${currentNode.name}-[${Object.entries(opened).toSorted().reduce((acc, [key, value]) => acc + `${key}:${value.minutesRemaining};`, '')}]`;
if (quickestPathTo[hash] >= minutesRemaining) continue;
quickestPathTo[hash] = minutesRemaining;
const openedValve = {};
if (currentNode.flowRate) {
minutesRemaining -= 1;
openedValve[currentNode.name] = { minutesRemaining, flowRate: currentNode.flowRate };
}
const namesOfOpenedValves = Object.keys(opened);
if (namesOfOpenedValves.length + Object.keys(openedValve).length === numberOfValvesWithPositiveFlow) {
opened = { ...opened, ...openedValve };
logValue();
continue;
}
Object.values(currentNode.edges).forEach(({ weight, node }) => {
const potentialRemainingMinutes = minutesRemaining - weight;
if (!namesOfOpenedValves.includes(node.name) && potentialRemainingMinutes >= 0) {
queue.push({
node, minutesRemaining: potentialRemainingMinutes, opened: { ...opened, ...openedValve },
});
}
});
}
return highestReleasePotential;
};
const startNode = valves.find(valve => valve.name === 'AA');
valves.forEach(identifyAllEdgeNodes);
addShortestPathToAllMissingNodes();
deletePathsToZeroFlowRooms();
/* Unfinished: Currently giving the correct result for test data, but not real data... */
console.log(`Part 1: ${findHighestReleasePotential(valves, startNode)}`);