Computer.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  1. const prompt = require("prompt-sync")({ sigint: true });
  2. const util = require("util");
  3. const Stack = require("./Stack");
  4. const ComputerParameterMode = require("./ComputerParameterMode");
  5. const InputModes = require("./InputModes");
  6. const { DeepClone } = require("./common");
  7. module.exports = class Computer {
  8. /**
  9. * An Intcode Computer for the Advent of Code 2019 challenge
  10. *
  11. * @author Apis Necros
  12. *
  13. * @param {number[]} stack The initial memory to load into the computer
  14. * @param {Object} options Options that can be enabled within the computer
  15. * @param {boolean} options.followPointer When true, the memory will be dumped every call to Execute with the current instruction highlighted
  16. * @param {number} options.tickRate The number of milliseconds between calls to Execute. Initializes to 0.
  17. * @param {boolean} options.inputFromConsole When true, the computer will prompt for input on the console. If false, it will check for an linked computer and, if one exists, will wait for input from that computer.
  18. * @param {boolean} options.outputToConsole When true, the computer will print the output of opcode 4 to the console. If false, it will check for an linked computer and, if one exists, pass the output to that computer.
  19. * @param {number[]} options.inputModeMap Map calls to the INPUT opcode to user input or the input array
  20. */
  21. constructor(stack, options = {}) {
  22. this._initialMemory = DeepClone(stack);
  23. this.stack = new Stack(stack);
  24. this.OPCODES = {
  25. ADD: 1,
  26. MULTIPLY: 2,
  27. INPUT: 3,
  28. OUTPUT: 4,
  29. JUMP_IF_TRUE: 5,
  30. JUMP_IF_FALSE: 6,
  31. LESS_THAN: 7,
  32. EQUALS: 8,
  33. HALT: 99,
  34. };
  35. this.EQUALITY = {
  36. EQUALS: 0,
  37. LESS_THAN: 1,
  38. };
  39. this.parameterMode = ComputerParameterMode.POSITION_MODE;
  40. /**
  41. * Whether the Execute loop should skip moving the pointer after running the opcode
  42. *
  43. * Some opcodes, such as JUMP_IF_TRUE set the stack pointer, and as such shouldn't have
  44. * the Execute function move it after the opcode finishes executing.
  45. */
  46. this.skipNext = false;
  47. this.options = {
  48. followPointer: options.followPointer ?? false,
  49. tickRate: options.tickRate ?? 0,
  50. inputFromConsole: options.inputFromConsole ?? false,
  51. outputToConsole: options.outputToConsole ?? false,
  52. inputModeMap: options.inputModeMap ?? [],
  53. };
  54. /**
  55. * A function to pass the value from OUTPUT opcodes to
  56. *
  57. * If the outputToConsole option is false, and a function is provided here,
  58. * any values from an OUTPUT opcode will be passed here.
  59. * @type {?Computer}
  60. */
  61. this.outputComputer = null;
  62. /**
  63. * An input value provided at runtime
  64. *
  65. * This value will be passed to the first INPUT opcode made by the computer,
  66. * and will then be cleared. If the program being ran tries to make another
  67. * call to INPUT, and `options.inputFromConsole` is false, then an error
  68. * will be thrown.
  69. * @type {number}
  70. */
  71. this.runtimeInput = null;
  72. /**
  73. * An array of outputs from the computer
  74. *
  75. * Outputs from the computer that weren't output to the console, or to a
  76. * callback function.
  77. *
  78. * Ideally, when not in outputToConsole mode, the computer should only ever have
  79. * one output, but I'm not certain that will be the case. In that case, this array
  80. * will have the outputs in chronological order.
  81. * @type {number[]}
  82. */
  83. this.outputValues = [];
  84. }
  85. /**
  86. * Run the computer
  87. *
  88. * Runs opcodes on the stack until either the a HALT command is
  89. * encountered, or an error is thrown.
  90. * @returns {void}
  91. */
  92. async Run() {
  93. while (this.Execute(this.stack.Get(ComputerParameterMode.IMMEDIATE_MODE)) === true) {
  94. if (this.options.tickRate) {
  95. // Sleep
  96. // eslint-disable-next-line no-await-in-loop, no-promise-executor-return, arrow-parens
  97. await new Promise(resolve => setTimeout(resolve, this.options.tickRate));
  98. }
  99. }
  100. }
  101. /**
  102. * Run the computer with an initial input value
  103. *
  104. * Runs opcodes on the stack until either the a HALT command is
  105. * encountered, or an error is thrown. Stores the input to be used
  106. * by the first INPUT opcode encountered.
  107. * @param {number} input The input value to initialize the comptuer with.
  108. * @returns {void}
  109. */
  110. async RunWithInput(input) {
  111. this.runtimeInput = input;
  112. while (this.Execute(this.stack.Get(ComputerParameterMode.IMMEDIATE_MODE)) === true) {
  113. if (this.options.tickRate) {
  114. // Sleep
  115. // eslint-disable-next-line no-await-in-loop, no-promise-executor-return, arrow-parens
  116. await new Promise(resolve => setTimeout(resolve, this.options.tickRate));
  117. }
  118. }
  119. }
  120. /**
  121. * Execute a call using the provided opcode
  122. *
  123. * @param {number} rawOpcode A opcode to execute
  124. * @returns {boolean} False if the opcode was HALT, otherwise true
  125. */
  126. Execute(rawOpcode) {
  127. let status = true;
  128. this.skipNext = false;
  129. if (this.options.followPointer) {
  130. this.DumpMemory(true);
  131. }
  132. const opcode = rawOpcode % 100;
  133. // console.log(`DEBUG: opcode: ${opcode}`);
  134. switch (opcode) {
  135. case this.OPCODES.ADD: {
  136. this.Operation_Add(rawOpcode);
  137. break;
  138. }
  139. case this.OPCODES.MULTIPLY: {
  140. this.Operation_Multiply(rawOpcode);
  141. break;
  142. }
  143. case this.OPCODES.INPUT: {
  144. this.Operation_Input();
  145. break;
  146. }
  147. case this.OPCODES.OUTPUT: {
  148. this.Operation_Output(rawOpcode);
  149. break;
  150. }
  151. case this.OPCODES.JUMP_IF_TRUE: {
  152. this.Operation_JumpIf(rawOpcode, true);
  153. break;
  154. }
  155. case this.OPCODES.JUMP_IF_FALSE: {
  156. this.Operation_JumpIf(rawOpcode, false);
  157. break;
  158. }
  159. case this.OPCODES.LESS_THAN: {
  160. this.Operation_Equality(rawOpcode, this.EQUALITY.LESS_THAN);
  161. break;
  162. }
  163. case this.OPCODES.EQUALS: {
  164. this.Operation_Equality(rawOpcode, this.EQUALITY.EQUALS);
  165. break;
  166. }
  167. case this.OPCODES.HALT:
  168. status = false;
  169. break;
  170. default:
  171. throw Error(`Opcode ${opcode} not found\nMemdump: ${JSON.stringify(this.stack.Dump())}\nPointer: ${this.stack.pointer}`);
  172. }
  173. if (!this.skipNext) {
  174. this.stack.Next();
  175. }
  176. return status;
  177. }
  178. /**
  179. * Parse operands based on the current parameter mode
  180. *
  181. * When the int computer is in Immediate Mode, the values are returned
  182. * as-is. When in Position Mode, the operands are used as memory
  183. * addresses, and the values at those addresses are returned instead.
  184. *
  185. * @returns {number[]} The parsed list of operands
  186. */
  187. _ParseOperands(...operands) {
  188. if (this.parameterMode == ComputerParameterMode.IMMEDIATE_MODE) { return operands; }
  189. return operands.map((operand) => this.stack.Get(operand));
  190. }
  191. /**
  192. * Execute the Add opcode
  193. *
  194. * Adds two numbers and stores the result at the provided position
  195. * on the stack.
  196. *
  197. * Parses the operand Parameter Mode out of the opcode used to make
  198. * this call.
  199. *
  200. * @param {number} rawOpcode The opcode in memory used to make this call
  201. * @returns {void}
  202. */
  203. Operation_Add(rawOpcode) {
  204. const operandLeftMode = ComputerParameterMode.ParseParameterMode(rawOpcode, 1);
  205. const operandRightMode = ComputerParameterMode.ParseParameterMode(rawOpcode, 2);
  206. const operandLeft = this.stack.Next().Get(operandLeftMode);
  207. const operandRight = this.stack.Next().Get(operandRightMode);
  208. const outputPosition = this.stack.Next().Get(ComputerParameterMode.IMMEDIATE_MODE);
  209. const newValue = operandLeft + operandRight;
  210. this.stack.Put(outputPosition, newValue);
  211. }
  212. /**
  213. * Execute the Multiply opcode
  214. *
  215. * Multiplies two numbers and stores the result at the provided
  216. * position on the stack.
  217. *
  218. * @param {number} rawOpcode The opcode in memory used to make this call
  219. * @returns {void}
  220. */
  221. Operation_Multiply(rawOpcode) {
  222. const operandLeftMode = ComputerParameterMode.ParseParameterMode(rawOpcode, 1);
  223. const operandRightMode = ComputerParameterMode.ParseParameterMode(rawOpcode, 2);
  224. const operandLeft = this.stack.Next().Get(operandLeftMode);
  225. const operandRight = this.stack.Next().Get(operandRightMode);
  226. const outputPosition = this.stack.Next().Get(ComputerParameterMode.IMMEDIATE_MODE);
  227. const newValue = operandLeft * operandRight;
  228. this.stack.Put(outputPosition, newValue);
  229. }
  230. /**
  231. * Execute the Input opcode
  232. *
  233. * Checks to see if the computer's `runtimeInput` is set. If so, uses that
  234. * value as the input, and stores that at a specified address, and then
  235. * empties the `runtimeInput` value. If `runtimeInput` is empty, and if
  236. * the computer is set to accept input from the console, prompts the
  237. * user for input.
  238. *
  239. * @returns {void}
  240. */
  241. Operation_Input() {
  242. const outputPosition = this.stack.Next().Get(ComputerParameterMode.IMMEDIATE_MODE);
  243. /** A variable to store the input in before putting it on the stack */
  244. let userInput;
  245. /** The input mode to use to get the value */
  246. let inputMode = this.options.inputModeMap.shift();
  247. // If no input mode was found, attempt to set on using the runtime options
  248. if (inputMode === undefined) {
  249. if (this.options.inputFromConsole) { inputMode = InputModes.INPUT_FROM_CONSOLE; }
  250. else { inputMode = InputModes.INPUT_FROM_RUNTIME_STACK; }
  251. }
  252. // Get the input based on the input mode
  253. switch (inputMode) {
  254. case InputModes.INPUT_FROM_RUNTIME_STACK: {
  255. userInput = this.runtimeInput;
  256. this.runtimeInput = null;
  257. break;
  258. }
  259. case InputModes.INPUT_FROM_CONSOLE: {
  260. do {
  261. userInput = Number(prompt("Please enter a number: "));
  262. } while (Number.isNaN(userInput));
  263. break;
  264. }
  265. default:
  266. throw new Error("No input found");
  267. }
  268. // Put the input value onto the stack
  269. this.stack.Put(outputPosition, userInput);
  270. }
  271. /**
  272. * Execute the OUTPUT opcode
  273. *
  274. * @param {number} rawOpcode The opcode in memory used to make this call
  275. * @returns {void}
  276. */
  277. Operation_Output(rawOpcode) {
  278. const currAddress = this.options.outputToConsole ? this.stack.pointer : undefined;
  279. const outputPositionMode = ComputerParameterMode.ParseParameterMode(rawOpcode, 1);
  280. const output = this.stack.Next().Get(outputPositionMode);
  281. if (this.options.outputToConsole) {
  282. console.log(`OUTPUT FROM ADDRESS ${currAddress}: ${output}`);
  283. }
  284. else if (this.outputComputer !== null) {
  285. this.outputComputer.RunWithInput(output);
  286. }
  287. else {
  288. this.outputValues.push(output);
  289. }
  290. }
  291. /**
  292. * Execute the Jump_If_True and Jump_If_False opcodes
  293. *
  294. * Jumps to a given address in memory if the value at next address is memory matches
  295. * the given true/false condition.
  296. *
  297. * @param {number} rawOpcode The opcode in memory used to make this call
  298. * @param {boolean} testCondition The value the memory value should be compared against
  299. * @returns {void}
  300. */
  301. Operation_JumpIf(rawOpcode, testCondition) {
  302. const paramMode = ComputerParameterMode.ParseParameterMode(rawOpcode, 1);
  303. const jumpAddressMode = ComputerParameterMode.ParseParameterMode(rawOpcode, 2);
  304. const param = this.stack.Next().Get(paramMode);
  305. const jumpAddress = this.stack.Next().Get(jumpAddressMode);
  306. const performJump = !!param == testCondition;
  307. if (performJump) {
  308. this.skipNext = true;
  309. this.stack.SetPointerAddress(jumpAddress);
  310. }
  311. }
  312. /**
  313. * Execute the various equality checking opcodes
  314. *
  315. * @param {number} rawOpcode The opcode in memory used to make this call
  316. * @param {number} testCondition The type of equality check to perform as defined in the computer's constructor
  317. * @returns {void}
  318. */
  319. Operation_Equality(rawOpcode, testCondition) {
  320. const operandLeftMode = ComputerParameterMode.ParseParameterMode(rawOpcode, 1);
  321. const operandRightMode = ComputerParameterMode.ParseParameterMode(rawOpcode, 2);
  322. const operandLeft = this.stack.Next().Get(operandLeftMode);
  323. const operandRight = this.stack.Next().Get(operandRightMode);
  324. const outputPosition = this.stack.Next().Get(ComputerParameterMode.IMMEDIATE_MODE);
  325. let testPassed = false;
  326. switch (testCondition) {
  327. case this.EQUALITY.EQUALS:
  328. testPassed = operandLeft == operandRight;
  329. break;
  330. case this.EQUALITY.LESS_THAN:
  331. testPassed = operandLeft < operandRight;
  332. break;
  333. default:
  334. break;
  335. }
  336. this.stack.Put(outputPosition, Number(testPassed));
  337. }
  338. /**
  339. * Outputs the computer's stack to the console
  340. *
  341. * @param {boolean} [highlightPointer=false] Should the memory address of the current pointer be highlighted
  342. * @returns {void}
  343. */
  344. DumpMemory(highlightPointer = false) {
  345. let memory = this.stack.Dump();
  346. if (highlightPointer) {
  347. memory = memory.map((instruction, idx) => (idx == this.stack.pointer ? `{${instruction}}` : instruction));
  348. }
  349. console.log(util.inspect(memory, { breakLength: Infinity, colors: true, compact: true }));
  350. }
  351. /**
  352. * Get a value from the unhandled OUTPUT values in FIFO order
  353. *
  354. * @returns {number} An unhandled value from an output call
  355. */
  356. FetchOutputValue() {
  357. return this.outputValues.shift();
  358. }
  359. /**
  360. * Resets the computer's memory to the value it was created with
  361. *
  362. * @returns {void}
  363. */
  364. Reset() {
  365. this.stack = new Stack(this._initialMemory);
  366. this.outputValues = null;
  367. this.outputComputer = null;
  368. }
  369. /**
  370. * Sets the computer's memory to a new stack
  371. *
  372. * Note: This resets the computer's initial memory, so `Reset` will use this value
  373. *
  374. * @param {number[]} stack The new memory stack for the computer
  375. * @returns {void}
  376. */
  377. SetMemory(stack) {
  378. this._initialMemory = DeepClone(stack);
  379. this.stack = new Stack(stack);
  380. }
  381. };