1-
21const add = ( { posIn1, posIn2, posOut, data } ) => {
3- data [ posOut ] = data [ posIn1 ] + data [ posIn2 ]
2+ console . debug ( `Adding ${ data [ Number ( posIn1 ) ] } to ${ data [ Number ( posIn2 ) ] } ` )
3+ data [ posOut ] = data [ Number ( posIn1 ) ] + data [ Number ( posIn2 ) ]
44 return true
55}
66
77const multiply = ( { posIn1, posIn2, posOut, data } ) => {
8- data [ posOut ] = data [ posIn1 ] * data [ posIn2 ]
8+ console . debug ( `Multiplying ${ data [ Number ( posIn1 ) ] } with ${ data [ Number ( posIn2 ) ] } ` )
9+ data [ posOut ] = data [ Number ( posIn1 ) ] * data [ Number ( posIn2 ) ]
910 return true
1011}
1112
@@ -15,12 +16,14 @@ const terminate = ({ position }) => {
1516}
1617
1718const step = ( { position, data } ) => {
19+ console . debug ( `Step: ${ position } ` )
1820 const opCodesMap = {
1921 1 : add ,
2022 2 : multiply ,
2123 99 : terminate
2224 }
2325 const segment = data . slice ( position , position + 4 )
26+
2427 // Run the correct opcode for the specified step
2528 return opCodesMap [ data [ position ] ] ( {
2629 posIn1 : segment [ 1 ] ,
@@ -34,6 +37,14 @@ const step = ({ position, data }) => {
3437const runProgram = ( { data } ) => {
3538 let position = 0
3639 let running = true
40+
41+ // Convert to BigInts because operations will exceed 53bit integers
42+ // TODO: Standard chokes on this ES2020 feature. Remove eslint-disable
43+ // once fixed.
44+ // See https://github.com/standard/standard/issues/1436
45+ // eslint-disable-next-line no-undef
46+ data . forEach ( ( key , idx ) => { data [ idx ] = BigInt ( key ) } )
47+
3748 while ( running === true && position <= data . length ) {
3849 running = step ( { position, data } )
3950 position += 4
0 commit comments