An Advent of Clojure

Paula Gearon
3 min readJan 20, 2018

--

In December 2016, I noticed that a few friends were participating in a programming challenge called, “Advent of Code”. We were already well into December before I heard about it, and I wasn’t interested in trying to catch up, so I did not pay too much attention to it. But when I heard about it again after only a few days into December 2017, I decided that I should take a look.

Advent of Code is a series of programming puzzles which has been put together by Eric Wastl since 2015. A new puzzle is presented each day, which participants attempt to solve using any means that they like. Each participant is given their own unique input for the puzzle, which leads to answers being specific to that user. This means that solutions can’t be shared trivially, though the programs that solve the problem can be. Solving the first question each day reveals a second part with the remainder of the day’s problem.

Like a traditional Advent calendar, each puzzle is revealed at midnight, and the first 100 solutions each day are awarded points, with a leader board showing who has been the most successful over time. A number of enthusiasts stay up and attempt to get as high on the board as possible, but between work and getting my children on the school bus in time, I had no interest in an evening race every midnight. All the same, I found the puzzles fun, and enjoyed doing them early on most days.

Through the course of the month, different problems lent themselves more to different styles of solution. It was often clear that one programming language or another would be ideally suited for specific problems, and indeed, many participants regularly switched between languages to find their solutions. While this would be great to keep up to date on a variety of languages, I decided that I was going to stick to my favorite programming language: Clojure.

Clojure is a functional programming language, and is variety of Lisp. It runs on a number of platforms, particularly the Java Virtual Machine, which is the platform I focus on. It also happens to be the language I use at work. Unlike other Lisps, it has a great set of libraries, can leverage the libraries of the host platform (in this case, Java) and has built-in syntax to support useful data structures, such as hash tables and vectors.

Some of the Advent of Code challenges were perfect for Clojure, and really show the power of this language. Others were clearly going to be easier using an imperative programming language using memory that gets continuously modified.

My choice to stick to Clojure was pragmatic. I had limited time each day, and the language I used most of the time was going to be the easiest way to solve each problem. And for those puzzles which were harder to solve with in a functional style, I thought it would be good practice for me to see through to the functional solution, since I occasionally have to deal with these situations in my day to day coding.

In the course of solving these programs, I would catch up with friends who were also working their way through, and I realized that some of my coding solutions may be of interest for others to see. On top of that, I occasionally learnt something unexpected, and figured that this might be of interest to share as well. Most importantly, I haven’t written much for some time now, and I thought that this could be a fun way to get back to it.

So on to my Advent of Clojure, starting with how I set up my environment

--

--