Last updated: 2017-12-11

Code version: 32d98f0

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

Crossing the bridge, you’ve barely reached the other side of the stream when a program comes up to you, clearly in distress. “It’s my child process,” she says, “he’s gotten lost in an infinite grid!”

Fortunately for her, you have plenty of experience with infinite grids.

Unfortunately for you, it’s a hex grid.

The hexagons (“hexes”) in this grid are aligned such that adjacent hexes can be found to the north, northeast, southeast, south, southwest, and northwest:

  \ n  /
nw +--+ ne
  /    \
-+      +-
  \    /
sw +--+ se
  / s  \ 
 

You have the path the child process took. Starting where he started, you need to determine the fewest number of steps required to reach him. (A “step” means to move from the hex you are in to any adjacent hex.)

For example:

Let’s go

So today is all about the hex!

Back to a nice easy one today is which I keep track of hex moves in a the cartesian coordinate system (ie x, y). All I needed to do was adapt a previous wandering algorithm to include half steps in coordinate space to reflect diagonal moves allowed in a hex configuration.

Packages & functions

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

Input

input <- aoc_get_input(day= 11, cookie_path = paste0(rprojroot::find_rstudio_root_file(),
                                                 "/secrets/session_cookie.txt")) 

Functions

hex_dist <- function(input, max_out = F) {
    steps <- input %>% strsplit(., ",") %>% unlist
    max_o <- 0
    loc <- c(0,0)
    for(move in steps){
        loc <- loc + switch(move,
                            "n" = c(1,0),
                            "ne" = c(0.5, 0.5),
                            "e" = c(0, 1),
                            "se" = c(-0.5, 0.5),
                            "s" = c(-1, 0),
                            "sw" = c(-0.5, -0.5),
                            "w" = c(0, -1),
                            "nw" = c(0.5, -0.5))
        max_o <- max(max_o, loc %>% abs %>% sum)
    }
    if(max_out){max_o}else{
        loc %>% abs %>% sum
    }
}  

Test

expect_equal("se,sw,se,sw,sw" %>% hex_dist , 3)

deploy

input %>% hex_dist 
[1] 824

Success!



—- Part 2 —-

Brief

How many steps away is the furthest he ever got from his starting position?

Let’s go

Test

#expect_equal(,)

deploy

input %>% hex_dist(max_out = T)
[1] 1548

Success!


And let’s not forget the calendar!



template based on the workflowr standalone template