DEV Community

Cover image for Sensor Boost
Robert Mion
Robert Mion

Posted on

Sensor Boost

Advent of Code 2019 Day 9

Try the simulator with your puzzle input!

Simulator of Intcode computer running example input

Task: Solve for X where...

Part 1

X = the BOOST keycode
Enter fullscreen mode Exit fullscreen mode

Part 2

X = the coordinates of the distress signal
Enter fullscreen mode Exit fullscreen mode

Several example inputs

109,1,204,-1,1001,100,1,100,1008,100,16,101,1006,101,0,99

1102,34915192,34915192,7,4,7,99,0

104,1125899906842624,99
Enter fullscreen mode Exit fullscreen mode

Each one represents:

  • An Intcode program that runs on a Intcode computer
  • Where each integer represents either an opcode, a parameter mode, or a parameter within a larger instruction

Part 1

  1. Intcode computer: Round 4!
  2. Using test mode to troubleshoot my computer

Intcode computer: Round 4!

Previous articles in this series - for Days 2, 5 and 7 - summarize the rules learned up to those points.

Up to now, my Intcode computer understands the following rules:

  • Opcodes 1,2,3,4,5,6,7,8,99
  • Parameters modes 0,1

This puzzle introduces:

  • One new bit of state to manage: relative base, which starts at 0
  • One new opcode: 9 - this modifies the relative base
  • One new parameter mode: 2 - this looks-up the proper value in memory in relation to the relative base
  • Values stored in memory will be orders of magnitude larger than previously encountered
  • Memory addresses referenced throughout the program that exist beyond the initial maximum addresses allocated - the program must be updated to accommodate and look-up or write to these locations without error mid-execution of the program

Using test mode to troubleshoot my computer

As noted in the instructions:

The BOOST program will ask for a single input; run it in test mode by providing it the value 1. It will perform a series of checks on each opcode, output any opcodes (and the associated parameter modes) that seem to be functioning incorrectly, and finally output a BOOST keycode.
Once your Intcode computer is fully functional, the BOOST program should report no malfunctioning opcodes when run in test mode; it should only output a single value, the BOOST keycode.

After adding code to accommodate the new rules, I ran my program with input 1.

I saw this output:

[203, 0]
Enter fullscreen mode Exit fullscreen mode

I knew this meant there was a bug in my Intcode computer's logic.

As added help, I was logging out each opcode-parameter combination encountered during the program up to the point where it halts.

Here's what I saw near that output:

209
209
203
Enter fullscreen mode Exit fullscreen mode

Prior to the first 209, no logged integers contained a 2 in any parameter position.

What was this trying to tell me?

  • I likely wasn't handling relative mode correctly in my instruction functions for opcodes 9 and 3

When inspecting those functions' code, I noticed something I overlooked from Day 5's rules:

Parameters that an instruction writes to will never be in immediate mode.

I over-accounted for this by hard-coding position mode a.k.a. 0 in functions that write to a value.

As a fix, I adjusted all instances of that to always refer to the parameter mode used just-in-time.

I re-ran the program.

My output only included one value:

  • A 10-digit integer

It was the correct answer!

Part 2

The easiest Part 2 ever:

Run the program with input 2
Wait a little while
Return the single output value, a coordinate
Enter fullscreen mode Exit fullscreen mode
  • Check
  • Check
  • Check

It was the correct answer again!

I did it!!

As celebrated at the beginning of Part 2's instructions:

I now have a complete Intcode computer.

Also:

  • I solved both parts!
  • I've now solved eight parts of four puzzles wherein I successfully built an Intcode computer
  • I've built four simulators that re-enact certain features of an Intcode computer
  • I've completed 4/5 puzzles referenced in Day 25 - fingers crossed that I'm able to solve Day 17's puzzle, too!

Latest comments (0)