R/day-03.R

#' For a given input list of vectors, calculate sum of all their ranges
#' Used in Day-02 of the Advent of Code 2017
#'
#' @param list_num_vec (list) : list of numeric vectors
#'
#' @export
get_range_numvec <- function(list_num_vec){
    list_num_vec %>%
        purrr::map(range) %>%
        purrr::map(diff) %>%
        base::unlist() %>%
        sum(na.rm = TRUE) %>%
        base::return()
}


#' For a given txt file containing rows of  numbers, calculates the sum
#' of their ranges
#' Used in Day-02 of the Advent of Code 2017
#' @param txt_file_path (character) : The path to the valid Day-02 input txt
#' file
#'
#' @export
get_range_txtfile <- function(txt_file_path){
    txt_file_path %>%
        base::readLines(con = .) %>%
        stringr::str_split(string = ., pattern = "\t") %>%
        purrr::map(as.numeric) %>%
        get_range_numvec(list_num_vec = .) %>%
        base::return()
}
shamindras/adventofcode2017 documentation built on May 14, 2019, 7:37 a.m.