Advent of Code 2017 Day 5

Advent of Code 2017 Day 5 is a fun little diversion. Basically, we have the world's simplest assembly code: each line has a line number, and a jump command. Every time a jump is taken, its value increases by one. We have to solve the problem of figuring out how many steps will be taken before a jump puts us off the list.

I won't go over how I parsed the input in detail: I used a C++ Map to map line numbers to jump commands pretty simply.

Then it's just a simple algo:

    line_no = 1;
    int nxt_line_no = 1;
    while (line_no < line_max && line_no > 0)
    {
        nxt_line_no += code[line_no];
        ++steps;
        //code[line_no] >= 3 ? code[line_no]-- : code[line_no]++;
        code[line_no]++;
        line_no = nxt_line_no;
    }

    std::cout << steps << std::endl;
    return 0;

While we're on the list and our location is valid, set the target destination to follow the current jump command, increment the steps taken, increment the jump we took, then update the line number to our new target. Boom.

The commented out line comes into play in day 2, and is the only difference: if the jump was 3 or more, it gets decremented instead of incremented. Simple difference, one line of change between the parts.

What does this remind me of?

This reminds me of writing a sim of a very simple computer and its assembly language in an assignment for NIU: The Simplesim. It had a few opcodes, few registers, and like 8 bytes of memory but it got the job done. Was a fun diversion.

Updates

I'll probably continue to do updates on advent of code 2017 progress, as I'd like to finish it within the next few weeks, but I'm going to focus more on other programming tips, paradigms, and draw from my own projects to make this blog more useful to others while still maintaining its purpose of documenting my learning. Thanks as always for tuning in!

0
Subscribe to my newsletter

Read articles from Jake Fitzenreider directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Jake Fitzenreider
Jake Fitzenreider

I am a recent grad of Northern Illinois University working on my own projects and now on the hunt for a job!