Advent of Code in Ruby, Perl and Elm
Jan 24, 2016

Eric Wastl's Advent of Code is a programming challenge with a Christmas theme originally published as 2 related problems per day from 1st to 25th of December, 2015. Each problem requires writing a computer program to solve it.

Submission of a correct answer is rewarded by a star and shortly after the last problem was published over 1000 people had collected all 50 available (see the stats page). I managed (using Ruby) all except the 2nd part of day 19 which turned out to have a unexpectedly simple solution (see analysis published by askalski).

I had become aware of the challenge from a tweet by James Edward Gray II, a well known member of the Ruby community, who later published all his solutions. I wondered what I could learn from comparing James's Ruby solutions to mine and from trying to port them to Perl 5 and Elm, two other languages I'm interested in.

List Utils

Many of the problems involved manipulating lists or arrays and could make use of various library utilities, if available. An example is something James used a few times but which I hadn't previously come across: each_cons (called natatime in Perl) which iterates over a given number of consecutive members of an array. Here's a summary of some list utilities that frequently cropped up in the problems and where, if they exist, to find them. Before the challenge I hadn't come across some of the Perl modules nor the List.Extra Elm package.

Ruby Perl Elm
all/any Enumerable List::MoreUtils List
combination Array Algorithm::Combinatorics
natatime Enumerable (each_cons) List::MoreUtils
max/min Enumerable List::Util List
permutation Array Algorithm::Permute List.Extra
product/sum List::Util List
uniq Array List::MoreUtils Set (fromList)

Speed Comparisons

Often the problems would involve searches of some kind, some taking as much as a couple of minutes to complete on my computer, while others just took a few milliseconds. Despite using similar algorithms, most of the Perl programs were faster than their Ruby counterparts. The difference was greatest (almost a factor of 10) for the problems that took the least time to complete. Such a large factor was a surprise, at least for me.

Another surprise (again, maybe just for me) was that the Elm programs, running in a browser, were not always the slowest. In some cases they even beat the Ruby version.

Elm Code

Elm is a purely functional language (not at all like Perl or Ruby) built on top of JavaScript (so it's a good fit for web applications). The language designers are trying to make it appealing to "mainstream" developers by, for example, having unusually helpful compiler error messages and avoiding esoteric technical jargon.

My Elm solutions to the advent problems can be run and viewed. There's also a small bit of JavaScript to embed the Elm.

Elm was also used for the family tree on this site and consists of the main Elm code and some JavaScript to communicate with the back-end Rails app.

Blog