Advent of Code 2019

The story of my slow descent into madness

View on GitHub
2 December 2019

Day 2: 1202 Program Alarm

by Simon Berger


Links:

I really enjoy writing “emulators” or even just small language interpreters to solve puzzles, however, I’m a tad wary that this Intcode program is going to come back again soon. To be clear, I wouldn’t mind if it came back, but last year there was a similar puzzle on day 19 which had a let’s say problematic second part. The code used a very inefficient implementation and had to be translated to find out what it did (See my solution). I honestly quite enjoyed that puzzle, but somehow it irked me that the problem couldn’t be solved using programming only and with today’s puzzle I think we might be seeing something similar again this year.

Enough with last year’s problems, so far we’re good. I was honestly positively surprised today, I remember having some problems regarding mutable and normal borrows last year. I was certain that the following wasn’t going to work, but to my surprise, it did.

fn add(p: &mut Program, a: OpCode, b: OpCode, s: OpCode) {
    let index = p[s];
    p[index] = p[p[a]] + p[p[b]];
}
fn mul(p: &mut Program, a: OpCode, b: OpCode, s: OpCode) {
    let index = p[s];
    p[index] = p[p[a]] * p[p[b]];
}

In hindsight it seems obvious that this wouldn’t cause a problem and I think I just confused it with the following situation:

let mut p: Program;

do_something(&mut p, p[1], p[2]);

This wouldn’t compile because &mut p is a mutable borrow which means p can’t be borrowed again, which is what p[1] and p[2] are trying to do. But anyway, all in all, this was a really nice introduction and I’m positive future puzzles are going to build on it.

In the first part of the puzzle the noun and verb are hard-coded to 12 and 1 respectively. I wonder whether those constants are the same for everyone. If not, I would have to adjust my input system for these kinds of parameters.

Also, I rewrote some of the input logic which meant I had to adjust parts of day 1. Specifically I added utils for comma separated values (as opposed to lines) and turned the “usize_lines”/”isize_lines” method into a more general “parse_lines” method.

I also decided to add some unit tests (also for day 1), but the second part doesn’t provide any examples and I’m neither going to include my input, nor am I going to come up with examples. So the unit tests only really test the “Intcode interpreter” using the few examples provided.

GitHub pages

I also decided to setup this little “blog”. It’s using GitHub pages with the awfully named Hacker theme. I think it looks great, but god does that name ever make me cringe.

tags: intcode
Overview
Previous day Next day