Computer.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. const Stack = require("./Stack");
  2. const ComputerParameterMode = require('./ComputerParameterMode');
  3. /**
  4. * An Intcode Computer for the Advent of Code 2019 challenge
  5. *
  6. * @author Apis Necros
  7. */
  8. module.exports = class Computer {
  9. constructor(stack) {
  10. this.stack = new Stack(stack);
  11. this.OPCODES = {
  12. ADD: 1,
  13. MULTIPLY: 2,
  14. OUTPUT: 4,
  15. HALT: 99,
  16. };
  17. this.paramMode = ComputerParameterMode.POSITION_MODE;
  18. }
  19. /**
  20. * Run the computer
  21. *
  22. * Runs opcodes on the stack until either the a HALT command is
  23. * encountered, or an error is thrown.
  24. * @returns {void}
  25. */
  26. Run() {
  27. // eslint-disable-next-line no-empty
  28. while (this.Execute(this.stack.Get()) === true) { }
  29. }
  30. /**
  31. * Execute a call using the provided opcode
  32. *
  33. * @param {number} opcode A opcode to execute
  34. * @returns {boolean} False if the opcode was HALT, otherwise true
  35. */
  36. Execute(opcode) {
  37. // console.log(`DEBUG: opcode: ${opcode}`)
  38. let status = true;
  39. switch (opcode) {
  40. case this.OPCODES.ADD: {
  41. const operandLeft = this.stack.Next().Get();
  42. const operandRight = this.stack.Next().Get();
  43. const position = this.stack.Next().Get();
  44. this.Operation_Add(operandLeft, operandRight, position);
  45. break;
  46. }
  47. case this.OPCODES.MULTIPLY: {
  48. const operandLeft = this.stack.Next().Get();
  49. const operandRight = this.stack.Next().Get();
  50. const position = this.stack.Next().Get();
  51. this.Operation_Multiply(operandLeft, operandRight, position);
  52. break;
  53. }
  54. case this.OPCODES.OUTPUT: {
  55. const outputPosition = this.stack.Next().Get();
  56. this.Operation_Output(outputPosition);
  57. break;
  58. }
  59. case this.OPCODES.HALT:
  60. status = false;
  61. break;
  62. default:
  63. throw Error(`Opcode ${opcode} not found`);
  64. }
  65. this.stack.Next();
  66. return status;
  67. }
  68. /**
  69. * Execute the Add opcode
  70. *
  71. * Adds two numbers and stores the result at the provided position
  72. * on the stack.
  73. *
  74. * @param {number} operandLeft The first operand
  75. * @param {number} operandRight The second operand
  76. * @param {number} outputPosition The position on the stack to place the result
  77. * @returns {void}
  78. */
  79. Operation_Add(operandLeft, operandRight, outputPosition) {
  80. const newValue = operandLeft + operandRight;
  81. this.stack.Put(outputPosition, newValue);
  82. }
  83. /**
  84. * Execute the Multiply opcode
  85. *
  86. * Multiplies two numbers and stores the result at the provided
  87. * position on the stack.
  88. *
  89. * @param {number} operandLeft The first operand
  90. * @param {number} operandRight The second operand
  91. * @param {number} outputPosition The position on the stack to place the result
  92. * @returns {void}
  93. */
  94. Operation_Multiply(operandLeft, operandRight, outputPosition) {
  95. const newValue = operandLeft * operandRight;
  96. this.stack.Put(outputPosition, newValue);
  97. }
  98. /**
  99. * Execute the OUTPUT opcode
  100. *
  101. * @param {number} outputPosition The memory address of the value to output
  102. */
  103. Operation_Output(outputPosition) {
  104. console.log(this.stack.Get(outputPosition));
  105. }
  106. /**
  107. * Outputs the computer's stack to the console
  108. * @returns {void}
  109. */
  110. DumpMemory() {
  111. console.log(this.stack.Dump());
  112. }
  113. };
  114. /**
  115. * Check whether the value at a specific spot in a number is non-zero
  116. *
  117. * Similar to the bitwise & operator, checks a "place" in a decimal number
  118. * to see if it has a non-zero value.
  119. *
  120. * @example
  121. * const test = 107;
  122. * console.log(DecimalPlaceIsNonZero(test, 1)) // true
  123. * console.log(DecimalPlaceIsNonZero(test, 2)) // false
  124. * console.log(DecimalPlaceIsNonZero(test, 3)) // true
  125. *
  126. * @param {number} input The number to check
  127. * @param {number} place The power of 10 to check against
  128. * @returns {boolean} Whether the value in that number's place is non-zero
  129. */
  130. function DecimalPlaceIsNonZero(input, place) {
  131. return !!Math.floor((input % 10**place) / 10**(place - 1));
  132. }