Browse Source

Add functionality for INPUT opcode

ApisNecros 1 year ago
parent
commit
df317bf776
3 changed files with 64 additions and 1 deletions
  1. 30 0
      IntComp/Computer.js
  2. 31 1
      package-lock.json
  3. 3 0
      package.json

+ 30 - 0
IntComp/Computer.js

@@ -1,3 +1,5 @@
+const prompt = require("prompt-sync")({ sigint: true });
+
 const Stack = require("./Stack");
 const ComputerParameterMode = require("./ComputerParameterMode");
 const { DeepClone } = require("./common");
@@ -14,6 +16,7 @@ module.exports = class Computer {
         this.OPCODES = {
             ADD: 1,
             MULTIPLY: 2,
+            INPUT: 3,
             OUTPUT: 4,
             HALT: 99,
         };
@@ -58,6 +61,10 @@ module.exports = class Computer {
                 this.Operation_Multiply(operandLeft, operandRight, position);
                 break;
             }
+            case this.OPCODES.INPUT: {
+                this.Operation_Input();
+                break;
+            }
             case this.OPCODES.OUTPUT: {
                 const outputPosition = this.stack.Next().Get();
 
@@ -132,6 +139,29 @@ module.exports = class Computer {
         this.stack.Put(outputPosition, newValue);
     }
 
+    /**
+     * Execute the Input opcode
+     *
+     * Prompts the user to input a value from the command line
+     *
+     * @returns {void}
+     */
+    Operation_Input() {
+        const outputPosition = this.stack.Next().Get(ComputerParameterMode.IMMEDIATE_MODE);
+
+        console.log(`DEBUG: outputPosition: ${outputPosition}`);
+
+        let userInput;
+
+        do {
+            userInput = Number(prompt("Please enter a number: "));
+        } while (Number.isNaN(userInput));
+
+        console.log(`DEBUG: userInput: ${userInput}`);
+
+        this.stack.Put(outputPosition, userInput);
+    }
+
     /**
      * Execute the OUTPUT opcode
      *

+ 31 - 1
package-lock.json

@@ -1,9 +1,12 @@
 {
-    "name": "AoC",
+    "name": "2019",
     "lockfileVersion": 3,
     "requires": true,
     "packages": {
         "": {
+            "dependencies": {
+                "prompt-sync": "^4.2.0"
+            },
             "devDependencies": {
                 "@nicquid/eslint-config-nqt": "^1.0.2"
             }
@@ -1930,6 +1933,33 @@
                 "node": ">= 0.8.0"
             }
         },
+        "node_modules/prompt-sync": {
+            "version": "4.2.0",
+            "resolved": "https://registry.npmjs.org/prompt-sync/-/prompt-sync-4.2.0.tgz",
+            "integrity": "sha512-BuEzzc5zptP5LsgV5MZETjDaKSWfchl5U9Luiu8SKp7iZWD5tZalOxvNcZRwv+d2phNFr8xlbxmFNcRKfJOzJw==",
+            "dependencies": {
+                "strip-ansi": "^5.0.0"
+            }
+        },
+        "node_modules/prompt-sync/node_modules/ansi-regex": {
+            "version": "4.1.1",
+            "resolved": "https://registry.npmjs.org/ansi-regex/-/ansi-regex-4.1.1.tgz",
+            "integrity": "sha512-ILlv4k/3f6vfQ4OoP2AGvirOktlQ98ZEL1k9FaQjxa3L1abBgbuTDAdPOpvbGncC0BTVQrl+OM8xZGK6tWXt7g==",
+            "engines": {
+                "node": ">=6"
+            }
+        },
+        "node_modules/prompt-sync/node_modules/strip-ansi": {
+            "version": "5.2.0",
+            "resolved": "https://registry.npmjs.org/strip-ansi/-/strip-ansi-5.2.0.tgz",
+            "integrity": "sha512-DuRs1gKbBqsMKIZlrffwlug8MHkcnpjs5VPmL1PAh+mA30U0DTotfDZ0d2UUsXpPmPmMMJ6W773MaA3J+lbiWA==",
+            "dependencies": {
+                "ansi-regex": "^4.1.0"
+            },
+            "engines": {
+                "node": ">=6"
+            }
+        },
         "node_modules/punycode": {
             "version": "2.3.1",
             "resolved": "https://registry.npmjs.org/punycode/-/punycode-2.3.1.tgz",

+ 3 - 0
package.json

@@ -2,5 +2,8 @@
     "type": "commonjs",
     "devDependencies": {
         "@nicquid/eslint-config-nqt": "^1.0.2"
+    },
+    "dependencies": {
+        "prompt-sync": "^4.2.0"
     }
 }