I Heard You Like Registers
Tags: adventofcode, algorithms, computerscience, programming
Published: 2022-08-04 16:19:28

## [Advent of Code 2017 Day 8](https://adventofcode.com/2017/day/8)

## [Try the simulator using your puzzle input!](https://aociheardyoulikeregisters.rmion.repl.co/)
![Simulation of the accumulating object](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/yukoqmehb10f5m7cdtp1.gif)

## Part 1
1. One giant `reduce()`...?
2. With two `switch` statements

### One giant `reduce()`...?
Per the instructions, I need to:
> compute the result of a series of unusual register instructions

And once finished, find:
> the largest value in any register after completing the instructions

I interpret all that as an algorithm that looks like this:
```
Return the largest number from
  The list of values associated with keys in a dictionary
    That has been accumulated from a list of instructions
```

In JavaScript, it looks like this:
```js
return Math.max(
  ...Object.values(
    input.reduce((accumulator, current) => {
      // Process each instruction
      // Make sure keys exist in the accumulating object
      // And that they start with the value 0
      // Update and return the accumulating object
    }, {})
  )
)
```

### With two `switch` statements
The first `switch` will handle: `>`, `<`, `>=`, `<=`, `==`, `!=`

The second will handle: `inc`, `dec`

The blueprint for the input pattern is:
```
xhe       dec        -173   if vhu       >           98
registerA updates-by amount if registerB compares-to number
```
- The register to modify
- Whether to increase or decrease that register's value
- The amount by which to increase or decrease it
- And a condition, which always features a register on the left-hand side and a number on the right-hand side

`switch` one hinges on `compares-to`:
```
Set a flag as false
Depending on what compares-to is:
  Update flag to whether the value stored in registerB as compared to number is true or false
```

`switch` two only runs if `flag` is true, and hinges on `updates-by`:
```
Depending on what updates-by is:
  Increment or decrement the value stored in registerA by amount
```

Testing my algorithm:
- It worked on the example input!
- It worked on my puzzle input!

## Part 2
### `reduce()` two things now!
- In Part 1, my `reduce()` accumulated an object only
- Now, it needs to accumulate a 2-element array: the first element is the same object from Part 1, the second element is a number that will store the maximum value

After updating my object as per the instruction, I now need to:
```
Determine the maximum value among each of the stored values in the object, stored as max
If max is greater than the value currently stored in the second element of the accumulating array
  Update the second element to equal max
```

In JavaScript, it looks like this:
```js
return input.reduce((accumulator, current) => {
      // Process each instruction
      // Make sure keys exist in the accumulating object
      // And that they start with the value 0
      // Update and return the accumulating object
      // Update the max value if a new one exists
    }, [{},0])
  )
)
```

By the time the program finishes, `max` should store `the highest value held in any register during this process`.

Testing my algorithm:
- It worked on the example input!
- It worked on my puzzle input!

## I did it!!
- I solved both parts!
- With one `reduce()`, a few `switch` statements, and `Math`!
- I [built a simulator](https://aociheardyoulikeregisters.rmion.repl.co/) to watch the object accumulate!