DEV Community

Cover image for I Heard You Like Registers
Robert Mion
Robert Mion

Posted on

I Heard You Like Registers

Advent of Code 2017 Day 8

Try the simulator using your puzzle input!

Simulation of the accumulating object

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
Enter fullscreen mode Exit fullscreen mode

In JavaScript, it looks like this:

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
    }, {})
  )
)
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode
  • 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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

In JavaScript, it looks like this:

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])
  )
)
Enter fullscreen mode Exit fullscreen mode

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 to watch the object accumulate!

Top comments (0)