Last updated: 2017-12-01

Code version: f21acfd

See more puzzles

Advent of Code

Session information

sessionInfo()
R version 3.4.2 (2017-09-28)
Platform: x86_64-apple-darwin15.6.0 (64-bit)
Running under: macOS Sierra 10.12.6

Matrix products: default
BLAS: /Library/Frameworks/R.framework/Versions/3.4/Resources/lib/libRblas.0.dylib
LAPACK: /Library/Frameworks/R.framework/Versions/3.4/Resources/lib/libRlapack.dylib

locale:
[1] en_GB.UTF-8/en_GB.UTF-8/en_GB.UTF-8/C/en_GB.UTF-8/en_GB.UTF-8

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

loaded via a namespace (and not attached):
 [1] compiler_3.4.2  backports_1.1.0 magrittr_1.5    rprojroot_1.2  
 [5] tools_3.4.2     htmltools_0.3.6 yaml_2.1.14     Rcpp_0.12.13   
 [9] stringi_1.1.5   rmarkdown_1.8   knitr_1.17      git2r_0.19.0   
[13] stringr_1.2.0   digest_0.6.12   evaluate_0.10.1

Brief

The night before Christmas, one of Santa’s Elves calls you in a panic. “The printer’s broken! We can’t print the Naughty or Nice List!” By the time you make it to sub-basement 17, there are only a few minutes until midnight. “We have a big problem,” she says; “there must be almost fifty bugs in this system, but nothing else can print The List. Stand in this square, quick! There’s no time to explain; if you can convince them to pay you in stars, you’ll be able to–” She pulls a lever and the world goes blurry.

When your eyes can focus again, everything seems a lot more pixelated than before. She must have sent you inside the computer! You check the system clock: 25 milliseconds until midnight. With that much time, you should be able to collect all fifty stars by December 25th.

Collect stars by solving puzzles. Two puzzles will be made available on each day millisecond in the advent calendar; the second puzzle is unlocked when you complete the first. Each puzzle grants one star. Good luck!

You’re standing in a room with “digitization quarantine” written in LEDs along one wall. The only door is locked, but it includes a small interface. “Restricted Area - Strictly No Digitized Users Allowed.”

It goes on to explain that you may only leave by solving a captcha to prove you’re not a human. Apparently, you only get one millisecond to solve the captcha: too fast for a normal human, but it feels like hours to you.

The captcha requires you to review a sequence of digits (your puzzle input) and find the sum of all digits that match the next digit in the list. The list is circular, so the digit after the last digit is the first digit in the list.

For example:

1122 produces a sum of 3 (1 + 2) because the first digit (1) matches the second digit and the third digit (2) matches the fourth digit. 1111 produces 4 because each digit (all 1) matches the next. 1234 produces 0 because no digit matches the next. 91212129 produces 9 because the only digit that matches the next one is the last digit, 9. What is the solution to your captcha?

Let’s go

Packages

library(tidyverse)
library(testthat)

With the help of my colleague Phil Tooley, I’ve written a function to call the input for each day in directly from the Advent of Code server. It defaults to inputs for 2017 puzzles and to the path I store my session cookie at (inputs are user specific so the user needs to know who you are to give you the right input). But you can override that by supplying a different year to year or the path to a .txt file containing your session cookie to argument cookie_path. See function & more details here.

source(paste0(rprojroot::find_rstudio_root_file(), "/R/functions.R"))

Input

input <- aoc_get_input(day = 1) %>% strsplit(split = numeric(), fixed = T) %>% unlist %>% as.numeric

Functions

get_tally <- function(input, compare = 1) {
    tally <- 0
    tally_length <- length(input)
    input <- c(input, head(input, compare))
    
    for(i in 1:tally_length){
        if(input[i + compare] == input[i]){
            tally <- tally + input[i]  
        }else{next}
    }
    tally
}

Test

expect_equal(get_tally(c(1, 1, 2, 2)),3)
expect_equal(get_tally(c(1, 1, 1, 1)),4)
expect_equal(get_tally(c(1, 2, 3, 4)),0)
expect_equal(get_tally(c(9, 1, 2, 1, 2, 1, 2, 9)), 9)

deploy

get_tally(input)
[1] 1223

Success!


—- Part 2 —-

Brief

You notice a progress bar that jumps to 50% completion. Apparently, the door isn’t yet satisfied, but it did emit a star as encouragement. The instructions change:

Now, instead of considering the next digit, it wants you to consider the digit halfway around the circular list. That is, if your list contains 10 items, only include a digit in your sum if the digit 10/2 = 5 steps forward matches it. Fortunately, your list has an even number of elements.

For example:

  • 1212 produces 6: the list contains 4 items, and all four digits match the digit 2 items ahead.
  • 1221 produces 0, because every comparison is between a 1 and a 2.
  • 123425 produces 4, because both 2s match each other, but no other digit has a match.
  • 123123 produces 12.
  • 12131415 produces 4.

What is the solution to your new captcha?

Let’s go

Test

expect_equal(get_tally(c(1, 2, 1, 2), length(c(1, 2, 1, 2))/2),6)
expect_equal(get_tally(c(1, 2, 2, 1), 2),0)
expect_equal(get_tally(c(1, 2, 3, 1, 2, 3), length(c(1, 2, 3, 1, 2, 3))/2),12)

deploy

get_tally(input, length(input)/2)
[1] 1284

Success!