README.md

adventofcode17

These are my solutions to Advent of Code 2017, a series of 25 programming puzzles.

Package overview

The /R folder contains the R functions I wrote for each day. I used some light test-driven development for the puzzles. That is, each puzzle description provides some example inputs and outputs. Before tackling the main test input, I write a unit-test in /tests that confirms that my solution can correctly reproduce the examples. The /inst directory contains the code to handle the main test input for each day.

I limited the amount of package dependencies used for these puzzles to maximize future compatibility and to make sure that it is mostly my code that solves the problems. For example, if a puzzle requires answering questions about the data in a tree-like structure, it would be kind of cheating for me to find a library for building and traversing trees to tackle the problem. It's advent of code, not advent of load.

I have allowed myself to use:

I've put my R source code under a GPL-3 license. It should not apply to the puzzle descriptions in the code comments at the top of each file. I did not write those descriptions.

Coding approaches

Here are the programming tasks and techniques I used for the various puzzles.

By "book-keeping", I mean basic programming programming where I keep track of some changing state like a position in a vector.

By "math", I mean studying the problem and using math to find a shortcut that lets me skip some computations.

By "custom evaluation", I mean writing a parser to convert the problem input into R code and run that R code in a special environment. This technique changes the coding task into the task of running R code with some extra book-keeping. This approach was a new one for me, so I learned a great deal along the way and I could streamline my solutions for some puzzles if I had to redo them.

By "functional programming", I mean both map/filter/reduce operations as well as using first class functions. For example, on day 04, I have to count how many passphrases have a duplicated word in Part A and count how many passphrases contain anagrams in Part B. My solution is a function count_valid_passphrases(passphrases, rule) where rule is a function tailored for Part A or Part B.

By "objects", I mean object-oriented programming using closures. Something like:

counter <- function(start = 0) {
  initial <- force(start)
  num <- initial
  inc <- function() num <<- num + 1
  dec <- function() num <<- num - 1
  reset <- function() num <<- initial
  look <- function() num

  list(inc = inc, dec = dec, reset = reset, look = look)
}

robot <- counter(10)
robot$look()
#> [1] 10
robot$inc()
robot$inc()
robot$look()
#> [1] 12
robot$dec()
robot$dec()
robot$dec()
robot$dec()
robot$look()
#> [1] 8
robot$reset()
robot$look()
#> [1] 10


tjmahr/adventofcode17 documentation built on May 30, 2019, 2:29 p.m.