-
Notifications
You must be signed in to change notification settings - Fork 0
/
day7-part1.js
46 lines (36 loc) · 998 Bytes
/
day7-part1.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
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 = [0, 1, 2, 3, 4];
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};
}
for (let i = 0; i < 5; ++i) {
amplifiers[i].computer.enqueueInput(output);
output = amplifiers[i].iterator.next().value;
}
maxOutput = Math.max(maxOutput, output);
}
console.log(maxOutput);
});