Last updated: 2017-12-05

Code version: fc3c51d

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.1 magrittr_1.5    rprojroot_1.2  
 [5] tools_3.4.2     htmltools_0.3.6 yaml_2.1.15     Rcpp_0.12.14   
 [9] stringi_1.1.6   rmarkdown_1.8   knitr_1.17      git2r_0.19.0   
[13] stringr_1.2.0   digest_0.6.12   evaluate_0.10.1

Brief

An urgent interrupt arrives from the CPU: it’s trapped in a maze of jump instructions, and it would like assistance from any programs with spare cycles to help find the exit.

The message includes a list of the offsets for each jump. Jumps are relative: -1 moves to the previous instruction, and 2 skips the next one. Start at the first instruction in the list. The goal is to follow the jumps until one leads outside the list.

In addition, these instructions are a little strange; after each jump, the offset of that instruction increases by 1. So, if you come across an offset of 3, you would move three instructions forward, but change it to a 4 for the next time it is encountered.

For example, consider the following list of jump offsets:

0 3 0 1 -3 Positive jumps (“forward”) move downward; negative jumps move upward. For legibility in this example, these offset values will be written all on one line, with the current instruction marked in parentheses. The following steps would be taken before an exit is found:

In this example, the exit is reached in 5 steps.

How many steps does it take to reach the exit?

Let’s go

Packages & functions

library(tidyverse)
library(testthat)
library(aocodeR)

Input

input <- aoc_get_input(day = 5, cookie_path = paste0(rprojroot::find_rstudio_root_file(),
                                                 "/secrets/session_cookie.txt")) 
input <- input %>% strsplit("\n") %>% unlist %>% as.numeric

Functions

steps_to_exit <- function(offsets){
    i <- 1
    steps <- 0
    while(i <= length(offsets) & i > 0){
        j <- i
        i <- i + offsets[i]
        offsets[j] <- offsets[j] + 1
        steps <- steps + 1
    }
    steps
}

Test

expect_equal(steps_to_exit(c(0, 3, 0, 1, -3)), 5)

deploy

steps_to_exit(input)
[1] 376976

Success!



—- Part 2 —-

Brief

Now, the jumps are even stranger: after each jump, if the offset was three or more, instead decrease it by 1. Otherwise, increase it by 1 as before.

Using this rule with the above example, the process now takes 10 steps, and the offset values after finding the exit are left as 2 3 2 3 -1.

How many steps does it now take to reach the exit?

Let’s go

steps_to_exit2 <- function(offsets){
    i <- 1
    steps <- 0
    while(i <= length(offsets) & i > 0){
        j <- i
        i <- i + offsets[i]
        offsets[j] <- offsets[j] + if(offsets[j] >= 3){-1}else{1}
        steps <- steps + 1
    }
    steps
}

Test

expect_equal(steps_to_exit2(c(0, 3, 0, 1, -3)), 10)

deploy

steps_to_exit2(input)
[1] 29227751

Success!



template based on the workflowr standalone template