less than 1 minute read

Today’s puzzle is easier than yesterday’s.

Part 1

Here I use strsplit() to create vector of words from each passphrase, and use the combination any(duplicated()) to detect passphrases that contain duplicated words

passphrases <- readLines("advent-data/2017-12-04-day4-data.txt")
passphrases <- passphrases[nzchar(passphrases)]

is_valid_passphrase <-  function(x) {
    x <- unlist(strsplit(x, " "))
    !any(duplicated(x))
}

n_valid <- sum(vapply(passphrases, is_valid_passphrase, logical(1)))
n_valid
## [1] 477

Part 2

Pretty similar as in part one, except that first, the words need to be sorted in alphabetical order to detect the anagram duplicates.

is_valid_passphrase2 <- function(x) {
    x <- unlist(strsplit(x, " "))
    sorted_x <- vapply(x, function(.x)
        paste0(sort(unlist(strsplit(.x, ""))), collapse = ""),
        character(1))
    !any(duplicated(sorted_x))
}

n_valid2 <- sum(vapply(passphrases, is_valid_passphrase2, logical(1)))
n_valid2
## [1] 167

Comments