Happy December, readers! Christmas has come and gone and you’re probably over it already. I, on the other hand, am determined to keep making progress on the Advent of Code challenge, which has served as a happy distraction and learning opportunity this month. When I haven’t been teaching, applying for jobs, getting prodded by doctors (long story), and trying to land my first contract gig, I’ve been helping Santa and his team deliver the goods.
In the process, I learned about (and/or got more practice using):
- object-oriented programming (OOP)
- string manipulation & regular expression matching
- MD5 hash conversion
- bitwise logic operators
- arrays and hashes
- functional programming
- algorithmic efficiency (Big O notation)
- and more!
Over time, I built out my repo to include testing (for easier code refactoring) and input files (for cleaner code), as well as a README detailing my approach to each problem. I know it sounds silly–and did not impress my career counselor–but this was truly a great professional learning exercise, and I’m enjoying the opportunity to see how other people solved the problems and how to optimize my own solutions. In some cases, my approach works, but takes a long time to run. In other solutions, my approach works in theory but takes too long to return a solution. I’m still tinkering and will read up on the problems I didn’t solve before I put it away until next year.
One of my favorite exercises was Day 14, Reindeer Games, in which a set of reindeer are racing, and the objective to to find how far the winning reindeer has travelled after a given number of seconds. Each reindeer travels at a set speed (x kms/second) for y seconds, and then needs to rest for z seconds. To solve this challenge I took an OOP approach and considered “what are the Nouns involved here?”
- We have Reindeer, each flying on a track, organized by a race. Reindeer have a name, a distance they travel every second, a fly-time and a rest-time.
- Each reindeer flies on a Track, which belongs to one reindeer. The track knows whether its reindeer is flying or resting at any given point, how long it has been in that state, and what index it’s at on the track (how far it’s gone). The track knows how to advance a reindeer once per second depending on the reindeer’s state.
- Organizing all this we have a third class, Race, which registers the reindeer as racers and has a “run_race” method that advances each reindeer for a given number of seconds. At the end of the race, it checks each reindeer’s track position to see who has travelled the farthest, and returns a distance.
This is my favorite type of programming to do, as I enjoy thinking through the “who knows about / controls what” in a given problem set. My solution isn’t necessarily the most efficient (though I have no real reason to believe that it’s inefficient, besides having to carve out an array with ~5,000 entries for each reindeer to mark the “track” on which they travel), but it’s straightforward enough that a non-programmer should be able to look at it and basically understand what’s going on.
Well, see for yourself: