February 23rd, 2016

Whovian robo-spider

R Advent: Days 1-5

Colin posted about Advent of Code (adventofcode.com) the other day, and the first couple problems are enticing, so I got sucked into doing the whole thing. Well, last night I finished!

It was an enjoyable challenge, and I thought it would be fun to post my solutions here and discuss them a little bit. I did everything in R because that's what I want to get better at, and I tried to write my code as R-tastically as possible. That means two things, mostly: first, aiming for vectorized/functional code over looping/procedural code, and second, using community-developed libraries rather than reinventing the wheel whenever possible. I spend a lot of time googling "how do you do X in R?" anyway (partly because I'm still learning R idioms and partly because inconsistent naming is one of R's biggest weaknesses), and I figure that in the real world, the goal is never to prove that you can do it, but to solve the problem. So if there's a library out there that was written by an expert and will do what you want, use it.

One of the things I like about R is how concise you can be. In other languages, I often feel like I'm spending most of my time and effort keeping a bunch of arrays and counters and indices in sync as I shepherd all the data through the analysis procedure. Whereas in R, it feels more like I put all my effort into doing origami to fold my data up into the correct configuration, and then I can write three lines of code to make magic happen. I'm including how many lines of code (LOC) each solution took; A/B means part 1 took A lines and part 2 took B extra.

Days 1-5 Collapse )