Advent of Code: Day 11

1 minute read

Part 1

get_steps <- function(coords) {
    coords <- strsplit(coords, ",")[[1]]
    tibble(coords = coords) %>%
        mutate(long = case_when(
                   coords == "n"  ~   0, 
                   coords == "ne" ~  .5, 
                   coords == "nw" ~ -.5, 
                   coords == "s"  ~   0, 
                   coords == "se" ~  .5, 
                   coords == "sw" ~ -.5
               ),
               lat = case_when(
                   coords == "n"  ~   1,   
                   coords == "ne" ~  .5,
                   coords == "nw" ~  .5,
                   coords == "s"  ~  -1,
                   coords == "se" ~ -.5,
                   coords == "sw" ~ -.5
               ))
    
}

count_steps <- function(steps) {
    steps %>%
        summarize(
            long_steps = sum(long),
            lat_steps = sum(lat)
        ) %>%
        abs() %>%
        sum()
}

"ne,ne,ne" %>%
    get_steps() %>% 
    count_steps()
## [1] 3
"ne,ne,sw,sw" %>%
    get_steps() %>%
    count_steps()
## [1] 0
"ne,ne,s,s" %>%
    get_steps() %>%
    count_steps()
## [1] 2
"se,sw,se,sw,sw" %>%
    get_steps() %>%
    count_steps()
## [1] 3
puzzle_input <- readLines("advent-data/2017-12-11-advent-day11.txt")
puzzle_input %>%
    get_steps() %>%
    count_steps()
## [1] 675

Part 2

max_steps <- function(steps) {
    steps %>%
        mutate(dist_long = cumsum(long),
               dist_lat = cumsum(lat), 
               dist = abs(dist_long) + abs(dist_lat)) %>%
        arrange(desc(dist)) %>%
        slice(1)
}


puzzle_input %>%
    get_steps() %>%
    max_steps()
## # A tibble: 1 x 6
##   coords  long   lat dist_long dist_lat  dist
##    <chr> <dbl> <dbl>     <dbl>    <dbl> <dbl>
## 1     ne   0.5   0.5       235     1189  1424

Comments