For this coding challenge/exercise I decided to restrict myself to only single-line responses. In order to achieve this I employ shell scripting to download the problem input, and Ruby code passed directly to an interpreter in order to transform it.
More important than the literal single-line nature of the code is the single "statement" aspect. I've prefered to use Ruby features and library methods that chain together, resulting in one long run-on expression. In doing so I've forced myself to think about the code as a sequence of data transformations that flow from one piece of the chain into the next.
A few Enumerable methods for many to many transformations (map
and select
), and many to one
(inject
and find
) are the backbone of this style of programming, along with using basic arrays to
structure data and destructing to
access it again. In many Lisp-style languages this is the preferred way of designing your code.
The code examples in this Gist rely upon an "environment variable" (a variable within your bash
terminal) called $ADVENT_TOKEN
. This variable should be set to a value copied from your browser's
cookie data for the Advent of Code site. You can set the environment variable in your terminal with
the following command, it will remain set until you close the terminal:
export ADVENT_TOKEN=8e280b8ca1ff0cae4194b271df98a28cb31f929e5
The value 8e280b8c...
can be found in Chrome with the following steps:
- Visit the Advent of Code website.
- Open the Developer Tools (Cmd + Option + I).
- Switch to the Resources tab.
- Open Cookies in the left-hand menu and select adventofcode.com
- Under the Value column in the right-hand pane, right-click on the entry for the row named 'session'.
- Select Copy.