# AdventOfCode-js-2019 Advent of Code 2019 solutions done in NodeJS ## Explanation A big theme of this year's challenge is an Intcode Computer the user is tasked with designing. This computer works on programs from a tape, or stack, of integers. Some of these integers on the stack are instructions, or opcodes. Opcodes are 2-digit integers (assumed 0 prefixed) that represent actions for the computer to perform on the stack. All integers on the stack are represented as decimal numbers. ## Parameter Modes The Intcode Computer being built has two Parameter Modes: Position Mode, and Immediate Mode. Each opcode can self-describe the Parameter Mode to use for each of its parameters independent of each other. ### Position Mode In Position Mode, the computer's default setting, parameters passed to functions are considered to be addresses where the actual value to operate on can be found. Thus, in Position Mode, an input of `42` tells the computer to look at memory address 42 on the stack. ### Immediate Mode In Immediate Mode, the parameter is taken "as is", and operated on accordingly. For example, `42` in Immediate Mode is treated as `42`. ### Relative Mode Relative mode acts similarly to Position Mode, where the final value is retrieved from another location in memory. It's different, though, in that a "Relative Base Offset" is added to that memory address to get the true address. The Relative Base Offset value is tracked by the computer's memory stack. ## Opcode Dictionary * [01 - ADD](#01---add) * [02 - MULTIPLY](#02---multiply) * [03 - INPUT](#03---input) * [04 - OUTPUT](#04---output) * [05 - JUMP IF TRUE](#05---jump-if-true) * [06 - JUMP IF FALSE](#06---jump-if-false) * [07 - LESS THAN](#07---less-than) * [08 - EQUALS](#08---equals) * [09 - MODIFY RELATIVE BASE](#09---modify-relative-base) * [99 - HALT](#99---halt) ### 01 - ADD Add two numbers, and outputs to the memory address in the third parameter. Opcode accepts 2 Postion Mode markers. | Forms | Description | | ----- | ----------- | | 1 | Add two values in Position Mode | | 01 | Add two values in Position Mode | | 101 | Add two values. First in Immediate Mode, second in Position Mode | | 001 | Add two values in Position Mode| | 1001 | Add two values. First in Position Mode, second in Immediate Mode | | 1101 | Add two values in Position Mode | | Parameters | Description | | ---------------- | ----------- | | Operand 1 | First number, or address of the number, to add | | Operand 2 | Second number, or address of the number, to add | | Output position | The memory address to store the result in | ### 02 - MULTIPLY Multiply two numbers, and outputs to the memory address in the third parameter. Opcode accepts 2 Postion Mode markers. | Forms | Description | | ----- | ----------- | | 2 | Multiply two values in Position Mode | | 02 | Multiply two values in Position Mode | | 102 | Multiply two values. First in Immediate Mode, second in Position Mode | | 002 | Multiply two values in Position Mode| | 1002 | Multiply two values. First in Position Mode, second in Immediate Mode | | 1102 | Multiply two values in Position Mode | | Parameters | Description | | ---------------- | ----------- | | Operand 1 | First number, or address of the number, to multiply | | Operand 2 | Second number, or address of the number, to multiply | | Output position | The memory address to store the result in | ### 03 - INPUT Takes input from either the runtime input array, or from the console, depending on the Input Mode Map. The Input Mode Map is an array of value, either `INPUT_FROM_CONSOLE` or `INPUT_FROM_RUNTIME_STACK`, provided to the computer before running. Each time an INPUT opcode is encounter, an Input Mode is shifted off of this array. Once the array is empty, any further INPUT opcodes encounter will assume `INPUT_FROM_RUNTIME_STACK` unless the `inputFromConsole` option is true. If the input is set to come from the runtime input array, and the array is empty, the computer will _pause_ execution until either the `Run` function for the `Input` functions are called on the computer. The output position may be in either Immediate Mode or Relative Mode. | Forms | Description | | ----- | ----------- | | 3 | Receive input | | 03 | Receive input | | 103 | Receive input in Immediate Mode | | 203 | Receive input in Relative Mode | | Parameters | Description | | ---------------- | ----------- | | Output position | The memory address to store the result in | ### 04 - OUTPUT Outputs either a value from a memory address, or a literal value. This output is initially sent to an `outputValues` array. If the computer's `outputToConsole` option is true, then the output is instead displayed in the console. If the computer has an `outputComputer` set, then the output is instead sent to the output computer through an `Input` call to that computer. | Forms | Description | | ----- | ----------- | | 4 | Output a value in Position Mode | | 04 | Output a value in Position Mode | | 104 | Output a value in Immediate Mode | | Parameters | Description | | ---------------- | ----------- | | Operand 1 | The number, or memory address of the number, to output | ### 05 - JUMP IF TRUE ### 06 - JUMP IF FALSE ### 07 - LESS THAN ### 08 - EQUALS ### 09 - MODIFY RELATIVE BASE Takes one parameter as input, and adds that number to the stack's Relative Base Offset. This value may be either a positive or negative integer. | Parameters | Description | | ---------------- | ----------- | | Operand 1 | The number, or memory address of the number, to add to the Relative Base Offset | ### 99 - HALT A simple instruction with no parameters. Stops the computer from running any more instructions.