Skip to main content

C'est la Z

A Teacher looks at Advent of Code 2016 #1

I recently posted about Advent of Code - a series of programming problems relseased one a day. While they vary in terms of level of difficulty, a number of them make nice problems for introductory to mid level programming classes.

I thought I'd share some of my thoughts on a few of them starting with the first problem from this years competition.

Take a minute to read it over.

At first glance, it might seem to a young programmer that this problem requires a two dimensional array - all about (x,y) coordinates but then there's a problem - there are no limits on coordinates and we can't make an unlimited size array.

After thinking a bit, hopefully the programmer realizes that all they need to do is keep track of the how the (x,y) location changes over time. In the solution below, we start at (0,0) and count the steps as we update two variables x and y.

When we finish processing the moves, we have our current location in (x,y) and we have the number of steps taken to get there.

The solution below hsa a couple of niceties that a beginning programmer might not know or use (and I'm not arguing that what's written is superior in any way, it's just what I ended up writing).

I make use of tuple destructuring:

which assigns x to the first item in the tuple and y the second. I used that a number of times

I also use a list I call dirs to hold dx and dy values for the four direcitons:

This made it easier to to update the location based on the 4 directions. I could also have just used if statements.

Here's all the code:

Overall, a nice little problem for beginning and intermediate students.

comments powered by Disqus