Last updated: 2017-11-27
Code version: ef93c91
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: /System/Library/Frameworks/Accelerate.framework/Versions/A/Frameworks/vecLib.framework/Versions/A/libBLAS.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     
other attached packages:
[1] bindrcpp_0.2    dplyr_0.7.4     purrr_0.2.2.2   readr_1.1.1     tidyr_0.6.3     tibble_1.3.4    ggplot2_2.2.1   tidyverse_1.1.1
loaded via a namespace (and not attached):
 [1] Rcpp_0.12.13     cellranger_1.1.0 compiler_3.4.2   git2r_0.19.0     plyr_1.8.4       bindr_0.1        forcats_0.2.0    base64enc_0.1-3 
 [9] tools_3.4.2      digest_0.6.12    lubridate_1.6.0  jsonlite_1.5     evaluate_0.10.1  nlme_3.1-131     gtable_0.2.0     lattice_0.20-35 
[17] pkgconfig_2.0.1  rlang_0.1.4      psych_1.7.5      yaml_2.1.14      parallel_3.4.2   haven_1.1.0      xml2_1.1.1       httr_1.3.1      
[25] stringr_1.2.0    knitr_1.17       hms_0.3          rprojroot_1.2    grid_3.4.2       glue_1.2.0       R6_2.2.2         readxl_1.0.0    
[33] foreign_0.8-69   rmarkdown_1.8    modelr_0.1.0     reshape2_1.4.2   magrittr_1.5     backports_1.1.0  scales_0.5.0     htmltools_0.3.6 
[41] rvest_0.3.2      rsconnect_0.8.5  assertthat_0.2.0 mnormt_1.5-5     colorspace_1.3-2 stringi_1.1.5    lazyeval_0.2.0   munsell_0.4.3   
[49] broom_0.4.2     
library(tidyverse)
Load string of step instructions and split into directions (step_dirs) and number of steps (step_dists)
steps <- readLines("../inputs/dir_input.txt") %>% strsplit(", ") %>% unlist()
# Test
#steps <- c("R8", "R4", "R4", "R8")
steps
  [1] "L4"   "L3"   "R1"   "L4"   "R2"   "R2"   "L1"   "L2"   "R1"   "R1"   "L3"   "R5"   "L2"   "R5"   "L4"   "L3"   "R2"   "R2"   "L5"  
 [20] "L1"   "R4"   "L1"   "R3"   "L3"   "R5"   "R2"   "L5"   "R2"   "R1"   "R1"   "L5"   "R1"   "L3"   "L2"   "L5"   "R4"   "R4"   "L2"  
 [39] "L1"   "L1"   "R1"   "R1"   "L185" "R4"   "L1"   "L1"   "R5"   "R1"   "L1"   "L3"   "L2"   "L1"   "R2"   "R2"   "R2"   "L1"   "L1"  
 [58] "R4"   "R5"   "R53"  "L1"   "R1"   "R78"  "R3"   "R4"   "L1"   "R5"   "L1"   "L4"   "R3"   "R3"   "L3"   "L3"   "R191" "R4"   "R1"  
 [77] "L4"   "L1"   "R3"   "L1"   "L2"   "R3"   "R2"   "R4"   "R5"   "R5"   "L3"   "L5"   "R2"   "R3"   "L1"   "L1"   "L3"   "R1"   "R4"  
 [96] "R1"   "R3"   "R4"   "R4"   "R4"   "R5"   "R2"   "L5"   "R1"   "R2"   "R5"   "L3"   "L4"   "R1"   "L5"   "R1"   "L4"   "L3"   "R5"  
[115] "R5"   "L3"   "L4"   "L4"   "R2"   "R2"   "L5"   "R3"   "R1"   "R2"   "R5"   "L5"   "L3"   "R4"   "L5"   "R5"   "L3"   "R1"   "L1"  
[134] "R4"   "R4"   "L3"   "R2"   "R5"   "R1"   "R2"   "L1"   "R4"   "R1"   "L3"   "L3"   "L5"   "R2"   "R5"   "L1"   "L4"   "R3"   "R3"  
[153] "L3"   "R2"   "L5"   "R1"   "R3"   "L3"   "R2"   "L1"   "R4"   "R3"   "L4"   "R5"   "L2"   "L2"   "R5"   "R1"   "R2"   "L4"   "L4"  
[172] "L5"   "R3"   "L4"  
steps_dirs <- gsub("[0-9]", "", steps)
steps_dists <- gsub("[A-Z]", "", steps) %>% as.numeric()
L & R to compass directions (N, S, E, W)dirs_lat <- c("N", "S")
dirs_lon <- c("E", "W")
dirs <- data.frame(facing = c("N", "S", "E", "W"), R = c(dirs_lon, rev(dirs_lat)), L = c(rev(dirs_lon), dirs_lat), stringsAsFactors = F)
facing <- "N"
compass_dirs <- NULL
for(step_dir in steps_dirs){
   compass_dirs <- c(compass_dirs, dirs[dirs$facing == facing, step_dir])
   facing <- tail(compass_dirs, 1)
}
tibble(compass_dirs, steps_dists) %>% group_by(compass_dirs) %>% summarise(sum = sum(steps_dists)) %>% mutate(orient = case_when(
    compass_dirs %in% dirs_lat ~ "lat",
    TRUE ~ "lon")) %>% 
    group_by(orient) %>% summarise(diff = diff(sum)) %>% pull(diff) %>% sum
[1] 332
steps.tbl <- tibble(compass_dirs, steps_dists, steps_dirs) %>% mutate(
    steps_dists_sign = case_when(
        compass_dirs %in% c(dirs_lat[1], dirs_lon[1]) ~ steps_dists,
        TRUE ~ -steps_dists),
    orient = case_when(
        compass_dirs %in% dirs_lat ~ "lat",
        TRUE ~ "lon"))
steps.tbl
trace_steps <- function(last_step, steps = steps.tbl$steps_dists_sign[i], on.axis = TRUE) {
    switch(as.character(on.axis),
           "TRUE" = last_step + sign(steps):steps,
           "FALSE" = rep(last_step, abs(steps)))
}
loc <- list(lon = 0, lat = 0)
for(i in 1:nrow(steps.tbl)){
    axis <- steps.tbl$orient[i] 
    switch(axis,
           "lat" = {
               loc$lat <- c(loc$lat, trace_steps(last_step = tail(loc$lat,1), on.axis = TRUE))
               loc$lon <- c(loc$lon, trace_steps(last_step = tail(loc$lon,1), on.axis = FALSE))},
           "lon" = {
               loc$lon <- c(loc$lon, trace_steps(last_step = tail(loc$lon,1), on.axis = TRUE))
               loc$lat <- c(loc$lat, trace_steps(last_step = tail(loc$lat,1), on.axis = FALSE))}
    )
}
loc <- loc %>% as.tibble()
loc
first_dup <- loc %>% duplicated() %>% which() %>% min
loc %>% slice(first_dup) %>% map_df(abs) %>% rowSums()
[1] 166
Phew…that took ages… 20.06 and I’m still at work. Time to go home!