day-04-solution.R

#-------------------------------------------------------------------------------
# Load libraries
#-------------------------------------------------------------------------------

# Clear console
cat("\014")

# Load the crud library
devtools::load_all()
devtools::document()
# devtools::test()

# Load external packages
pckgs <- c("here", "tidyverse", "magrittr")
adventofcode2017::ipak(pckgs)

# Data directory
DATA_DIR <- file.path(here::here(), "tests/testthat/data")

#-------------------------------------------------------------------------------
# Exercise Day 04-01 Solution
#-------------------------------------------------------------------------------

# Define the input string
inp_day_04_01 <- file.path(DATA_DIR, "input-day-04-01.txt")

inp_day_04_01 %>%
    adventofcode2017::parse_txtfile(txt_file = .
                                    , sep = " "
                                    , map_fun = as.character) %>%
    purrr::map(adventofcode2017::check_isunique) %>%
    unlist() %>%
    sum()

#-------------------------------------------------------------------------------
# Exercise Day 04-02 Solution
#-------------------------------------------------------------------------------

# Note we use the same input string for day-04 part 02 as we did for
# day-04 part 01
inp_day_04_01 %>%
    adventofcode2017::parse_txtfile(txt_file = .
                                    , sep = " "
                                    , map_fun = as.character) %>%
    purrr::map(.x = ., .f = adventofcode2017::sort_ind_str) %>%
    purrr::map(adventofcode2017::check_isunique) %>%
    unlist() %>%
    sum()
shamindras/adventofcode2017 documentation built on May 14, 2019, 7:37 a.m.