-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path16.js
64 lines (59 loc) · 1.61 KB
/
16.js
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
import { bfs } from './utils.js';
/**
* @param {string} d
*/
export const part1 = async d => {
const parseLineRegex = /Valve ([^ ]+) has flow rate=(\d+); tunnels? leads? to valves? (.*)/;
const data = d.split('\n')
.map(e => parseLineRegex.exec(e).slice(1))
.map(e => [e[0], parseInt(e[1], 10), e[2].split(', ')])
.sort((a, b) => b[1] - a[1]);
const valuesValue = new Map();
const valuesGrid = new Map();
const valuesCosts = new Map();
let currentPos = 'AA';
let targetPos = false; // We don't have a target to go to yet
let pressureReleased = 0;
let pressureReleasing = 0;
let minutesLeft = 30;
data.forEach(e => {
valuesGrid.set(e[0], e[2]);
if (e[1]) {
valuesValue.set(e[0], e[1]);
}
});
valuesGrid.forEach((_, start) => {
const costs = new Map();
valuesValue.forEach((_, end) => {
if (start == end) return;
costs.set(end, bfs(start, end, valuesGrid));
});
valuesCosts.set(start, costs);
});
while (minutesLeft > 0) {
minutesLeft--;
// Find next target
if (!targetPos) {
// Find the next value to open
const maxPressure = [currentPos, 0];
valuesValue.forEach((value, pos) => {
const pressureTotal = (minutesLeft - valuesCosts.get(currentPos).get(pos).length) * value;
if (pressureTotal > 0 && pressureTotal > maxPressure[1]) {
maxPressure[0] = pos;
maxPressure[1] = pressureTotal;
}
console.log(pos, pressureTotal, value, valuesCosts.get(currentPos).get(pos).length);
});
console.log(maxPressure);
break;
}
}
return data;
};
/**
* @param {string} d
*/
export const part2 = async d => {
const data = d.split('\n');
return data;
};