Skip to content

Commit ade857f

Browse files
committed
lib: opcode processor gets callback function for missing input
1 parent 50f62a2 commit ade857f

File tree

1 file changed

+11
-13
lines changed

1 file changed

+11
-13
lines changed

lib.js

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -55,22 +55,14 @@ adventofcode.init();
5555
*
5656
* @param {string} input program source code
5757
* @param {int[]} user_input
58-
* @param {int} keep program in memory for another run
58+
* @param {function} callback function in case of missing input
5959
* @returns {string} return value
6060
*/
61-
adventofcode.opcode_process = function(input, user_input = [], keep = -1) {
61+
adventofcode.opcode_process = function(input, user_input = [], callback = function(){}) {
6262
let program = input.split(",").map(x => parseInt(x));
6363
let pointer = 0;
6464
let output = [];
6565

66-
if (keep >= 0 ) {
67-
if (typeof this.opcode_memory == 'undefined')
68-
this.opcode_reset();
69-
70-
if (typeof this.opcode_memory[keep] != 'undefined')
71-
program = this.opcode_memory[keep];
72-
}
73-
7466
this.opcode_relative_base = 0;
7567

7668
while (program[pointer] !== 99) {
@@ -94,6 +86,15 @@ adventofcode.opcode_process = function(input, user_input = [], keep = -1) {
9486
break;
9587

9688
case 3:
89+
if (user_input.length === 0) {
90+
if (typeof callback === 'function') {
91+
user_input.push(callback(output));
92+
output = [];
93+
}
94+
else
95+
return output.join(',');
96+
}
97+
9798
program[this.opcode_get_pointer(program, pointer+1, op_parts[2])] = user_input.shift();
9899
pointer += 2;
99100
break;
@@ -134,9 +135,6 @@ adventofcode.opcode_process = function(input, user_input = [], keep = -1) {
134135
}
135136
}
136137

137-
if (keep >= 0)
138-
this.opcode_memory[keep] = program;
139-
140138
return output.join(',');
141139
};
142140

0 commit comments

Comments
 (0)