diff --git a/gir.js b/gir.js index 5662a83..18c783d 100644 --- a/gir.js +++ b/gir.js @@ -705,8 +705,8 @@ function runVM(state, maxCycles = null) { memory.set(index, 0); } let old = memory.get(index); - memory.set(index, old + - multiplier * change); + let value = old + multiplier * change; + memory.set(index, value & 0xff); } ip++; @@ -972,10 +972,12 @@ function ircbotRun(program, input, maxCycles = 400000) { // Find min and max of the existant array indinces, since // there is no good way to easily get them and we need them // for the output - let min = Array.from(result.state.memory.keys()).reduce( - (x, y) => Math.min(x, y)); - let max = Array.from(result.state.memory.keys()).reduce( - (x, y) => Math.max(x, y)); + // Default to both being set to tapeHead, because that way + // even if there are no indices we can get values that + // the rest of the code can work with + let memoryIndices = Array.from(result.state.memory.keys()); + let min = memoryIndices.reduce(Math.min, tapeHead); + let max = memoryIndices.reduce(Math.max, tapeHead); // Get 15 cells of context on each side of tape head // Exception is if max or min comes up before that, in which