Browse Source

Add ability to reset the computer's memory

ApisNecros 1 year ago
parent
commit
5332a0ac01
1 changed files with 11 additions and 0 deletions
  1. 11 0
      IntComp/Computer.js

+ 11 - 0
IntComp/Computer.js

@@ -1,5 +1,6 @@
 const Stack = require("./Stack");
 const ComputerParameterMode = require("./ComputerParameterMode");
+const { DeepClone } = require("./common");
 
 /**
  * An Intcode Computer for the Advent of Code 2019 challenge
@@ -8,6 +9,7 @@ const ComputerParameterMode = require("./ComputerParameterMode");
  */
 module.exports = class Computer {
     constructor(stack) {
+        this._initialMemory = DeepClone(stack);
         this.stack = new Stack(stack);
         this.OPCODES = {
             ADD: 1,
@@ -141,6 +143,15 @@ module.exports = class Computer {
     DumpMemory() {
         console.log(this.stack.Dump());
     }
+
+    /**
+     * Resets the computer's memory to the value it was created with
+     *
+     * @returns {void}
+     */
+    Reset() {
+        this.stack = new Stack(this._initialMemory);
+    }
 };
 
 /**