Last updated: 2017-12-04

Code version: 2f54606

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

A new system policy has been put in place that requires all accounts to use a passphrase instead of simply a password. A passphrase consists of a series of words (lowercase letters) separated by spaces.

To ensure security, a valid passphrase must contain no duplicate words.

For example:

aa bb cc dd ee is valid. aa bb cc dd aa is not valid - the word aa appears more than once. aa bb cc dd aaa is valid - aa and aaa count as different words. The system’s full passphrase list is available as your puzzle input. How many passphrases are valid?

Let’s go

Packages & functions

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

Input

input <- aoc_get_input(day = 4, cookie_path = paste0(rprojroot::find_rstudio_root_file(),
                                                 "/secrets/session_cookie.txt")) %>%
    strsplit("\n") %>% unlist

Functions

test_passphrase <- function(passphrase){
    !(passphrase %>% strsplit(split = " ") %>% unlist %>% duplicated %>% any)
}

Test

test_input_fail <- "aa bb cc dd aa"
test_input_pass <- "aa bb cc dd ee"

expect_equal(test_passphrase(test_input_fail), FALSE)
expect_equal(test_passphrase(test_input_pass), TRUE)

deploy

input %>% map_lgl(test_passphrase) %>% sum
[1] 477

Success!



—- Part 2 —-

Brief

For added security, yet another system policy has been put in place. Now, a valid passphrase must contain no two words that are anagrams of each other - that is, a passphrase is invalid if any word’s letters can be rearranged to form any other word in the passphrase.

For example:

abcde fghij is a valid passphrase. abcde xyz ecdab is not valid - the letters from the third word can be rearranged to form the first word. a ab abc abd abf abj is a valid passphrase, because all letters need to be used when forming another word. iiii oiii ooii oooi oooo is valid. oiii ioii iioi iiio is not valid - any of these words can be rearranged to form any other word. Under this new system policy, how many passphrases are valid?

Let’s go

test_anagram <- function(passphrase){
    
    chunks <- passphrase %>% strsplit(split = " ") %>% 
        unlist %>% map(function(.) {strsplit(., split = numeric()) %>% unlist})
    
    comps <- expand.grid(1:length(chunks), 1:length(chunks)) %>% 
        filter(Var1 != Var2)
    
    !(comps %>%
        apply(1, FUN = function(x, chunks){
            setequal(chunks[[x[1]]], chunks[[x[2]]])}, 
            chunks = chunks) %>% any)
}      

Test

input_test <- list(c("abcd", "dcba"), c("abce", "dcba"), c("abce", "dcba"))
expect_equal(input_test %>% map_lgl(test_anagram) %>% sum
,2)

input_test <- list(c("abcd", "dcba"), c("abce", "dcba"))
expect_equal(input_test %>% map_lgl(test_anagram) %>% sum
,1)

deploy

input %>% map_lgl(test_anagram) %>% sum
[1] 167

Success!



template based on the workflowr standalone template