-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday23_p2.js
113 lines (86 loc) · 2.59 KB
/
day23_p2.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
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
var intcode = require("./intcode-day23");
var inputData = require('./day23.json');
let computers = [];
let outputData = [];
let destination = [];
let natComputer = nat();
function outputCallback(output, computer) {
if(!isSet(outputData[computer]) && !isSet(destination[computer])) {
destination[computer] = output;
}
else if(!isSet(outputData[computer]) && isSet(destination[computer])) {
if(computer === 255) {
console.log("Output to computer 255: ", output);
}
outputData[computer] = output;
}
else {
if(destination[computer] === 255) {
console.log("Sending val to nat: ", output);
natComputer.addInput({ x: outputData[computer], y: output})
}
else {
console.log("Sending X=", outputData[computer], ", Y=", output, " to computer: ", destination[computer], " from computer: ", computer);
computers[destination[computer]].addInput({ x: outputData[computer], y: output});
}
destination[computer] = null;
outputData[computer] = null;
}
}
let finalResultNat = 0;
function nat() {
let lastInput = null;
let lastYValSent = null;
let done = false;
function addInput(input) {
lastInput = input;
}
setInterval(() => {
if(computers.every(t => t.idle()) && lastInput) {
if(done) {
console.log("Final Result: ", finalResultNat);
return;
}
if(lastInput.y === lastYValSent) {
finalResultNat = lastInput;
console.log("Final Result: ", finalResultNat);
done = true;
}
else {
lastYValSent = lastInput.y;
}
computers[0].addInput(lastInput);
//count++;
}
}, 500);
return {
addInput: addInput
};
}
function isSet(theVal) {
if(typeof theVal === 'undefined' || theVal === null) {
return false;
}
return true;
}
for(let i = 0; i < 50; i++) {
let program = intcode.runProgram([...inputData], o => outputCallback(o, i), i, true);
computers.push(program);
}
for(let i = 0; i < 50; i++) {
computers[i].start();
computers[i].addInput(i);
}
for(let i = 0; i < 50; i++) {
computers[i].addInput(i);
}
program.programPromise
.then(t => {
console.log("Tile count: ", tileCount);
});
setupGame();
bindHandlers();
program.start();
// process.stdin.setRawMode(true);
// keypress(process.stdin);
// process.stdin.resume();