@@ -55,22 +55,14 @@ adventofcode.init();
55
55
*
56
56
* @param {string } input program source code
57
57
* @param {int[] } user_input
58
- * @param {int } keep program in memory for another run
58
+ * @param {function } callback function in case of missing input
59
59
* @returns {string } return value
60
60
*/
61
- adventofcode . opcode_process = function ( input , user_input = [ ] , keep = - 1 ) {
61
+ adventofcode . opcode_process = function ( input , user_input = [ ] , callback = function ( ) { } ) {
62
62
let program = input . split ( "," ) . map ( x => parseInt ( x ) ) ;
63
63
let pointer = 0 ;
64
64
let output = [ ] ;
65
65
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
-
74
66
this . opcode_relative_base = 0 ;
75
67
76
68
while ( program [ pointer ] !== 99 ) {
@@ -94,6 +86,15 @@ adventofcode.opcode_process = function(input, user_input = [], keep = -1) {
94
86
break ;
95
87
96
88
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
+
97
98
program [ this . opcode_get_pointer ( program , pointer + 1 , op_parts [ 2 ] ) ] = user_input . shift ( ) ;
98
99
pointer += 2 ;
99
100
break ;
@@ -134,9 +135,6 @@ adventofcode.opcode_process = function(input, user_input = [], keep = -1) {
134
135
}
135
136
}
136
137
137
- if ( keep >= 0 )
138
- this . opcode_memory [ keep ] = program ;
139
-
140
138
return output . join ( ',' ) ;
141
139
} ;
142
140
0 commit comments