1 Setup
1.1 Libraries
library(httr)
library(xml2)
library(tibble)
library(magrittr)
library(dplyr)
library(purrr)
library(stringr)
library(stringi)
library(knitr)
library(cli)
library(digest)
1.2 Retrieve Data from AoC
session_cookie <- set_cookies(session = keyring::key_get("AoC-GitHub-Cookie"))
base_url <- paste0("https://adventofcode.com/", params$year, "/day/", params$task_nr)
puzzle <- GET(base_url,
session_cookie) %>%
content(encoding = "UTF-8") %>%
xml_find_all("///article") %>%
lapply(as.character)
parse_puzzle_data <- function(text_block = readClipboard()) {
if (length(text_block) == 1L) {
text_block <- text_block %>%
str_split("\n") %>%
extract2(1L) %>%
keep(nzchar)
}
text_block
}
puzzle_data <- local({
GET(paste0(base_url, "/input"),
session_cookie) %>%
content(encoding = "UTF-8") %>%
parse_puzzle_data()
})
2 Puzzle Day 5
2.1 Part 1
2.1.1 Description
— Day 5: Doesn’t He Have Intern-Elves For This? —
Santa needs help figuring out which strings in his text file are naughty or nice.
A nice string is one with all of the following properties:
-
It contains at least three vowels (
aeiouonly), likeaei,xazegov, oraeiouaeiouaeiou. -
It contains at least one letter that appears twice in a row, like
xx,abcdde(dd), oraabbccdd(aa,bb,cc, ordd). -
It does not contain the strings
ab,cd,pq, orxy, even if they are part of one of the other requirements.
For example:
-
ugknbfddgicrmopnis nice because it has at least three vowels (u…i…o…), a double letter (…dd…), and none of the disallowed substrings. -
aaais nice because it has at least three vowels and a double letter, even though the letters used by different rules overlap. -
jchzalrnumimnmhpis naughty because it has no double letter. -
haegwjzuvuyypxyuis naughty because it contains the stringxy. -
dvszwmarrgswjxmbis naughty because it contains only one vowel.
How many strings are nice?
2.1.2 Solution
This task cna be easily solved with the help of regular expressions.
- First filter for the naughty digrams
ab,cd,pqorxy. - For the remaining strings, remove strings which do not contain duplciated letters, or not enough vowels.
- Count the remaining strings.
N.B. For the tuples we use the backreference \\1 to count if the same letter
appeared already before.
filter_nice_words <- function(word_list) {
no_naughty_digrams <- word_list %>%
str_subset("ab|cd|pq|xy", negate = TRUE)
has_tuples <- no_naughty_digrams %>%
str_subset("(.)\\1")
has_3_vowels <- has_tuples %>%
str_count("[aeiou]") %>%
is_weakly_greater_than(3)
has_tuples[has_3_vowels]
}
puzzle_data %>%
filter_nice_words() %>%
length()
## [1] 255
2.2 Part 2
2.2.1 Description
— Part Two —
Realizing the error of his ways, Santa has switched to a better model of determining whether a string is naughty or nice. None of the old rules apply, as they are all clearly ridiculous.
Now, a nice string is one with all of the following properties:
-
It contains a pair of any two letters that appears at least twice in the string without overlapping, like
xyxy(xy) oraabcdefgaa(aa), but not likeaaa(aa, but it overlaps). -
It contains at least one letter which repeats with exactly one letter between them, like
xyx,abcdefeghi(efe), or evenaaa.
For example:
-
qjhvhtzxzqqjkmpbis nice because is has a pair that appears twice (qj) and a letter that repeats with exactly one letter between them (zxz). -
xxyxxis nice because it has a pair that appears twice and a letter that repeats with one between, even though the letters used by each rule overlap. -
uurcxstgmygtbstgis naughty because it has a pair (tg) but no repeat with a single letter between them. -
ieodomkazucvgmuyis naughty because it has a repeating letter with one between (odo), but no pair that appears twice.
How many strings are nice under these new rules?
2.2.2 Solution
We use the same overall idea, just adapting our regular expressions making use of backreferences as before.
filter_nice_words2 <- function(word_list) {
contains_pair <- word_list %>%
str_subset("(..).*\\1")
has_man_in_the_middle <- contains_pair %>%
str_subset("(.).\\1")
has_man_in_the_middle
}
puzzle_data %>%
filter_nice_words2() %>%
length()
## [1] 55