Skip to content

Commit 00b86d2

Browse files
committed
First attempt, doesn't seem to work
1 parent a01e187 commit 00b86d2

File tree

3 files changed

+99
-13
lines changed

3 files changed

+99
-13
lines changed

2019/23/intcode-computer-optimized.js

Lines changed: 32 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,28 +14,46 @@ const IMMEDIATE_MODE = '1';
1414
const RELATIVE_MODE = '2';
1515

1616
class Computer {
17+
static ADD = ADD;
18+
static MUL = MUL;
19+
static INP = INP;
20+
static OUT = OUT;
21+
static JIT = JIT;
22+
static JIF = JIF;
23+
static LTH = LTH;
24+
static EQU = EQU;
25+
static ARB = ARB;
26+
static STP = STP;
27+
28+
static POSITION_MODE = POSITION_MODE;
29+
static IMMEDIATE_MODE = IMMEDIATE_MODE;
30+
static RELATIVE_MODE = RELATIVE_MODE;
31+
1732
constructor({
1833
memory,
1934
inputs = [],
20-
replenish_input = undefined,
21-
pause_on_output = true,
22-
id = 0,
35+
// Called with computer as it's only arg
36+
defaultInput,
37+
pause_on = { [OUT]: true },
38+
address,
2339
clone_memory = false,
2440
}) {
25-
// For debugging
26-
this.id = String.fromCharCode('A'.charCodeAt(0) + id);
41+
// Must match initial input
42+
this.address = address;
43+
if (this.address !== inputs[0]) {
44+
throw new Error(`Invalid address: ${address}, inputs: ${JSON.stringify(inputs)}`);
45+
}
2746

2847
this.original_memory = clone_memory && memory.slice(0);
2948
this.memory = memory.slice(0);
3049
this.pointer = 0;
3150
this.relative_base = 0;
32-
this.pause_on_output = pause_on_output;
51+
this.pause_on = pause_on;
3352

3453
this.inputs = Array.isArray(inputs) ? inputs.slice(0) : [inputs];
35-
this.replenish_input = replenish_input;
3654
this.outputs = [];
3755

38-
this.parseOpTime = 0;
56+
this.defaultInput = defaultInput;
3957

4058
this.OPS = {
4159
[ADD]: {
@@ -63,10 +81,11 @@ class Computer {
6381
realName: 'INP',
6482
params: 1,
6583
fn: (a) => {
84+
if (this.defaultInput && this.inputs.length === 0) {
85+
let default_input_value = this.defaultInput(this);
86+
this.inputs.push(default_input_value);
87+
}
6688
this.memory[a] = this.inputs.shift();
67-
if (this.replenish_input !== undefined) {
68-
this.inputs.push(this.replenish_input);
69-
}
7089
},
7190
write: true,
7291
},
@@ -177,10 +196,10 @@ class Computer {
177196
this.runOp(op);
178197

179198
/**
180-
* In circuits, computer execution should be paused on outout so that value can be passed to the next computer.
199+
* In circuits, computer execution should be paused on output so that value can be passed to the next computer.
181200
* Additionally, execution should immediately stopped if we have halted.
182201
*/
183-
if ((this.pause_on_output && op.name === OUT) || this.halted) {
202+
if (this.pause_on[op.name] || this.halted) {
184203
break;
185204
}
186205

2019/23/network.js

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
const { Computer } = require('./intcode-computer-optimized');
2+
3+
class Network {
4+
constructor(program, nics_count = 50) {
5+
this.original_program = [...program];
6+
this.packet_queue = Array(nics_count).fill([]);
7+
8+
this.nics = Array(nics_count)
9+
.fill()
10+
.map((_, i) => {
11+
return new Computer({
12+
memory: program,
13+
inputs: [i],
14+
address: i,
15+
defaultInput: (computer) => {
16+
if (this.packet_queue[computer.address].length === 0) {
17+
return -1;
18+
} else {
19+
// Take first packet
20+
let packet = this.packet_queue[computer.address][0];
21+
if (packet.length === 2) {
22+
// First shift `x` value and keep partial packet in queue
23+
let x = packet.shift();
24+
return x;
25+
} else {
26+
// Then take the `y` value and remove the packet from the queue
27+
let [y] = this.packet_queue[computer.address].shift();
28+
return y;
29+
}
30+
}
31+
},
32+
pause_on: {
33+
[Computer.OUT]: true,
34+
[Computer.INP]: true,
35+
},
36+
});
37+
});
38+
}
39+
40+
run(break_on_address = 255) {
41+
while (true) {
42+
for (let nic of this.nics) {
43+
let outputs = nic.run();
44+
if (outputs.length === 3) {
45+
let address = outputs.shift();
46+
let x = outputs.shift();
47+
let y = outputs.shift();
48+
49+
if (address === break_on_address) {
50+
return { x, y };
51+
}
52+
this.packet_queue[address].push([x, y]);
53+
}
54+
}
55+
}
56+
57+
}
58+
}
59+
60+
module.exports = {
61+
Network,
62+
};

2019/23/part-one.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,6 @@
11
const { input } = require('./input');
2+
const { Network } = require('./network');
3+
4+
let network = new Network(input);
5+
let net_packet = network.run();
6+
console.log(net_packet);

0 commit comments

Comments
 (0)