DEV Community

Cover image for Set and Forget
Robert Mion
Robert Mion

Posted on

Set and Forget

Advent of Code 2019 Day 17

Try the simulator with your puzzle input!

Simulator of the vacuuming robot

Task: Solve for X where...

Part 1

X = the sum of the alignment parameters for the scaffold intersections
Enter fullscreen mode Exit fullscreen mode

Part 2

X = the amount of dust reported as collected after the robot visits every part of the scaffold at least once
Enter fullscreen mode Exit fullscreen mode

No example input given

Instead, example output is given:

  • ASCII codes 35, 46, and 10 as #, . and \n

It represents:

  • Scaffolding as #s
  • Emptiness as .s
  • Displayed in lines, separated by \ns

Part 1

  1. Turning ASCII codes into characters
  2. Testing the program's first few output codes using a new Intcode computer
  3. Seeing the whole picture
  4. Using the whole picture to generate the correct answer

Turning ASCII codes into characters

I had to look this up.

Thankfully, it's easy in JavaScript:

String.fromCharCode(35) // '#'
String.fromCharCode(46) // '.'
String.fromCharCode(10) // '\n'
Enter fullscreen mode Exit fullscreen mode

Testing the program's first few output codes using a new Intcode computer simulator

  • I already configured my last Intcode computer simulator to run until a new value is added to output
  • I just have to update it such that it renders another character in a string...instead of a newly discovered wall, empty space or oxygen location

One new simulator...coming right up!

After some setup and holding down ENTER for a couple minutes to generate the first several hundred output codes, I saw what I was looking for:
The start of some scaffolding

Seeing the whole picture

After tweaking my code to process the entire program and halting, I saw the full scaffolding:
All of the scaffolding

Using the whole picture to generate the correct answer

I've solved puzzles like this a few times before algorithmically.

This time, I wanted to do it visually.

Here's how I did that:
Animation showing how I solved Part 1

Part 2

  1. Visualizing the path to calculate and determine the ASCII codes for each movement function
  2. Attempting to feed each code as input to the Intcode program
  3. Troubleshooting, fully annotated
  4. Proof that I succeeded in moving the robot
  5. Could I update my simulator to render the continuous feed?

Visualizing the path to calculate and determine the ASCII codes for each movement function

This presented another required use of my design tool to draw the map.

Here's how I did that:
Painting the robot's desired path

Attempting to feed each code as input to the Intcode program

  • I'm under the impression that I need to feed each ASCII code, one after the other, as soon as a new value is output via opcode 4
  • I foresee it not being quite that easy, but hopefully I'm either correct or close enough that I can troubleshoot and ultimately generate the single large non-ASCII value

Time to update my Intcode computer and program, run it, and see what I get!

Troubleshooting, fully annotated

  • I initially set up my instructions as a flat list of character codes representing my movement commands and functions
  • After debugging my computer enough to see some initial output, I realized it expected separated lists

Proof of a failing program

  • This meant I had to convert my flat list into a nested array
  • And update my code to track two locations: 1) which list to reference, 2) which index to reference

After fiddling with code until I was seeing the correct list and locations print, I turned my attention to the X shown in the image above, representing a misplaced robot.

Somehow, my converted instructions must be wrong, because the robot should not be falling off!

A ha! An error in my character code conversion!

I mistakenly thought 10 was one number!

'10'.charCodeAt()
// 49
Enter fullscreen mode Exit fullscreen mode
  • charCodeAt defaults to position 0, which is 1 in 10

Therefore, what I thought in my instructions was:

R,6,L,10
Enter fullscreen mode Exit fullscreen mode

Was actually being converted to:

R,6,L,1
Enter fullscreen mode Exit fullscreen mode

I needed to insert the character code for 0 (48) - and 2 (50), elsewhere that the instruction was 12 - at the appropriate locations in my nested arrays where I already had a 49 (1).

Refresh page. Press button to run code.

Voila!

Proof that I succeeded in moving the robot

Successes:

  1. The final rendered state of the grid shows a robot in the ending scaffold location
  2. The symbol at the bottom is proof that the last value in my output is something other than a 1- or 2-digit number

The final rendered state of my program

Indeed, it was the expected large integer mentioned in the puzzle instructions.

Could I update my simulator to render the continuous feed?

  • Right now, the page prints all states of the scene, one below the other
  • I really want each scene to replace the prior state of the scene in millisecond increments

Yes. Yes I can!

It took some wonky magic number math and trial and error.

But I got my simulator to render the continuous video feed as a new set of ASCII characters on the page.

Try the simulator with your puzzle input!
Simulator of the vacuuming robot

I did it!!

  • I solved both parts!
  • I made a few GIFs to visualize my manual puzzle-solving algorithms!
  • I made another simulator that renders each step taken by the controlled robot!
  • I've now solved 8/8 Intcode computer puzzles!
  • I've now built 8 Intcode computer simulators!

Top comments (0)