Advent of Code 2017 in R: Day 1

My boyfriend recently introduced me to Advent of Code while I was in one of my “learn ALL of the things!” phases. Every year starting December 1st, new programming challenges are posted daily leading up to Christmas. They’re meant to be quick 5-10 minute challenges, so, wanting to test my programming skills, I figured why not try to do all of them in base R!

I went with base R because I know I can dplyr and stringr my way to victory with some of these challenges. I really want to force myself to really go back to basics and confirm that I have the knowledge to do these things on my own without Hadley Wickham‘s (very much appreciated in any other situation) assistance.

Since I’ve started, I’ve also seen a couple of other bloggers attempt to do these challenges in R so I’m really curious how my solutions will compare to theirs.

The first day of the challenge provides you with a string of numbers and asks you to sum all of the digits that match the next digit in a circular list, i.e. the digit after the last digit is the first digit.

My string was…


My first thought was that I would need to separate this string such that each character was the element of an object, either a vector or a list. I kept things simple and started by just copy-pasting the string into R. I could import it as a .txt file or otherwise but I figured that was unnecessary for such a quick problem. I stored the string as a variable named input.

# Split string after each character.

input_split <- strsplit(input, "")

# As a result, input_split is a list with 1 element:
# a vector containing each character of input as an 
# element. Annoying. Let's unlist() it to extract
# *just* the vector.

char_vector <- unlist(input_split)

# The problem now is that if we are going to sum
# the elements of our string, we need them to be
# numeric and not characters. Easy enough...

num_vector <- as.numeric(char_vector)

# Now lets just initialize our sum...

num_sum = 0

# And use a loop...

for(i in 1:length(num_vector)){

  # If we have the last element of the input string, 
  # set the next number equal to the first element
  # of the string, else select element i + 1.
  next_num <- ifelse(i = length(num_vector), 
  				num_vector[i + 1])

  # If our current element is equal to the next element,
  # update the sum.
  if(num_vector[i] == next_num){

  	num_sum = num_sum + num_vector[i]



Our sum is 1390 which is correct, huzzah.

Once you complete part 1, it surprises us with part 2 which asks us to now consider the digit “halfway around the circular list”. So, if our list is 10 digits long, we would add element 1 and element 6, element 8 and element 3, etc. We can make a really simple modification to our established loop to solve this…

num_sum = 0
skip = length(num_vector)/2

for(i in 1:length(num_vector)){

  # For the first half of the list we need to
  # add half the length of the vector to the
  # index to get the next number & substract
  # for the second half.
  next_num <- ifelse(i <= skip, 
                     num_vector[i + skip],
                     num_vector[i - skip])
  if(num_vector[i] == next_num){
    num_sum = num_sum + num_vector[i]

Our answer is 1,232 which is correct. Woo.

I know that loops in R are often discouraged due to their speed, or lack thereof. I think I might come back and play with this one again to see how I can implement vectorization here. That being said, I also think it’s important to know which tool is right for the job. With a problem of this size, I think a loop is just fine.

Published by

Emma Davies Smith

Emma Davies Smith is currently a postdoctoral research fellow at the Harvard School of Public Health. Her current research interests include clinical trial methodology, nonparametric methods, missing data, data visualization, and communication. When she's not working on expanding her knowledge of statistics, she's busy petting cats and unsuccessfully convincing her husband to let her adopt them, hiking, and concocting indie and folk rock playlists.

One thought on “Advent of Code 2017 in R: Day 1”

Leave a Reply

Your email address will not be published. Required fields are marked *