Computer.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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().GetUsingStackValue();
  42. const operandRight = this.stack.Next().GetUsingStackValue();
  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().GetUsingStackValue();
  49. const operandRight = this.stack.Next().GetUsingStackValue();
  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. }
  58. case this.OPCODES.HALT:
  59. status = false;
  60. break;
  61. default:
  62. throw Error(`Opcode ${opcode} not found`);
  63. }
  64. this.stack.Next();
  65. return status;
  66. }
  67. /**
  68. * Execute the Add opcode
  69. *
  70. * Adds two numbers and stores the result at the provided position
  71. * on the stack.
  72. *
  73. * @param {number} operandLeft The first operand
  74. * @param {number} operandRight The second operand
  75. * @param {number} outputPosition The position on the stack to place the result
  76. * @returns {void}
  77. */
  78. Operation_Add(operandLeft, operandRight, outputPosition) {
  79. const newValue = operandLeft + operandRight;
  80. this.stack.Put(outputPosition, newValue);
  81. }
  82. /**
  83. * Execute the Multiply opcode
  84. *
  85. * Multiplies two numbers and stores the result at the provided
  86. * position on the stack.
  87. *
  88. * @param {number} operandLeft The first operand
  89. * @param {number} operandRight The second operand
  90. * @param {number} outputPosition The position on the stack to place the result
  91. * @returns {void}
  92. */
  93. Operation_Multiply(operandLeft, operandRight, outputPosition) {
  94. const newValue = operandLeft * operandRight;
  95. this.stack.Put(outputPosition, newValue);
  96. }
  97. /**
  98. * Execute the OUTPUT opcode
  99. *
  100. * @param {number} outputPosition The memory address of the value to output
  101. */
  102. Operation_Output(outputPosition) {
  103. console.log(this.stack.Get(outputPosition));
  104. }
  105. /**
  106. * Outputs the computer's stack to the console
  107. * @returns {void}
  108. */
  109. DumpMemory() {
  110. console.log(this.stack.Dump());
  111. }
  112. };