-
Notifications
You must be signed in to change notification settings - Fork 0
/
day23-part1.js
47 lines (36 loc) · 1010 Bytes
/
day23-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
47
const readline = require('readline');
const Computer = require('../common/intcode');
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
rl.once('line', line => {
const computerCount = 50;
const natAddress = 255;
const intCode = line.split(',').map(n => Number(n));
const inputFunction = () => -1;
const computers = [];
const outputBuffers = [];
for (let i = 0; i < computerCount; ++i) {
const computer = new Computer(intCode, inputFunction);
computer.inputQueue.push(i);
computers[i] = computer;
outputBuffers[i] = [];
}
poll: while (true) {
for (let i = 0; i < computerCount; ++i) {
const output = computers[i].runOneCycle();
if (output !== undefined) {
outputBuffers[i].push(output);
}
while (outputBuffers[i].length >= 3) {
const [address, x, y] = outputBuffers[i].splice(0, 3);
if (address === natAddress) {
console.log(y);
break poll;
}
computers[address].inputQueue.push(x, y);
}
}
}
});