-
Notifications
You must be signed in to change notification settings - Fork 0
/
day7-part2.js
52 lines (42 loc) · 1.08 KB
/
day7-part2.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
const readline = require('readline');
const Computer = require('../common/intcode');
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
rl.once('line', line => {
const intCode = line.split(',').map(n => Number(n));
let maxOutput = -Infinity;
for (let perm = 0; perm < 120; ++perm) {
const phases = [];
{
let n = perm;
const phaseList = [5, 6, 7, 8, 9];
for (let i = 5; i > 0; --i) {
const phase = phaseList.splice(n % i, 1)[0];
phases.push(phase);
n = Math.floor(n / i);
}
}
let output = 0;
let amplifiers = [];
for (let i = 0; i < 5; ++i) {
const computer = new Computer(intCode);
const iterator = computer.run();
computer.enqueueInput(phases[i]);
amplifiers[i] = {computer, iterator};
}
feedbackLoop: while (true) {
for (let i = 0; i < 5; ++i) {
amplifiers[i].computer.enqueueInput(output);
let next = amplifiers[i].iterator.next();
if (next.done) {
break feedbackLoop;
}
output = next.value;
}
}
maxOutput = Math.max(maxOutput, output);
}
console.log(maxOutput);
});