1 Setup
1.1 Libraries
library(httr)
library(xml2)
library(magrittr)
library(dplyr)
library(purrr)
library(stringr)
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 %>%
str_extract_all("[^\\[\\]]+|\\[[^\\[\\]]+\\]")
}
puzzle_data <- local({
GET(paste0(base_url, "/input"),
session_cookie) %>%
content(encoding = "UTF-8") %>%
parse_puzzle_data()
})
2 Puzzle Day 7
2.1 Part 1
2.1.1 Description
— Day 7: Internet Protocol Version 7 —
While snooping around the local network of EBHQ, you compile a list of IP addresses (they’re IPv7, of course; IPv6 is much too limited). You’d like to figure out which IPs support TLS (transport-layer snooping).
An IP supports TLS if it has an Autonomous Bridge Bypass Annotation, or ABBA. An ABBA is any four-character sequence which consists of a pair of two different characters followed by the reverse of that pair, such as xyyx
or abba
. However, the IP also must not have an ABBA within any hypernet sequences, which are contained by square brackets.
For example:
-
abba[mnop]qrst
supports TLS (abba
outside square brackets). -
abcd[bddb]xyyx
does not support TLS (bddb
is within square brackets, even thoughxyyx
is outside square brackets). -
aaaa[qwer]tyui
does not support TLS (aaaa
is invalid; the interior characters must be different). -
ioxxoj[asdfgh]zxcvbn
supports TLS (oxxo
is outside square brackets, even though it’s within a larger string).
How many IPs in your puzzle input support TLS?
2.1.2 Solution
We use regex to find palindroms and then apply it to all strings and apply the logic on whether it must o must not appear.
is_palindrome <- function(string) {
str_detect(string, "(?=(.)(.)\\2\\1)(?!\\1\\1\\1\\1)")
}
check_ip <- function(tokens) {
is_p <- is_palindrome(tokens)
is_n <- str_detect(tokens, "\\[")
any(is_p[!is_n]) &&
all(!is_p[is_n])
}
sum(map_lgl(puzzle_data, check_ip))
## [1] 115
2.2 Part 2
2.2.1 Description
— Part Two —
You would also like to know which IPs support SSL (super-secret listening).
An IP supports SSL if it has an Area-Broadcast Accessor, or ABA, anywhere in the supernet sequences (outside any square bracketed sections), and a corresponding Byte Allocation Block, or BAB, anywhere in the hypernet sequences. An ABA is any three-character sequence which consists of the same character twice with a different character between them, such as xyx
or aba
. A corresponding BAB is the same characters but in reversed positions: yxy
and bab
, respectively.
For example:
-
aba[bab]xyz
supports SSL (aba
outside square brackets with correspondingbab
within square brackets). -
xyx[xyx]xyx
does not support SSL (xyx
, but no correspondingyxy
). -
aaa[kek]eke
supports SSL (eke
in supernet with correspondingkek
in hypernet; theaaa
sequence is not related, because the interior character must be different). -
zazbz[bzb]cdb
supports SSL (zaz
has no correspondingaza
, butzbz
has a correspondingbzb
, even thoughzaz
andzbz
overlap).
How many IPs in your puzzle input support SSL?
2.2.2 Solution
We extract first all palindromes in the respective parts, and check then whether they can be found in the reverted form in the respective parts of the IP.
extract_palindromes <- function(string) {
res <- str_match_all(string, "(?=(.)(.)(\\1))(?!\\1\\1\\1)")
map(res, function(matches) {
if (nrow(matches) == 0L) {
character(0)
} else {
apply(matches[, -1L, drop = FALSE], 1L, paste, collapse = "")
}
})
}
reverse_palindromes <- function(palindrome) {
str_replace(palindrome, "(.)(.)(.)", "\\2\\1\\2")
}
check_ip_ssl <- function(tokens) {
all_p <- extract_palindromes(tokens)
is_n <- str_detect(tokens, "\\[")
n_rp <- reverse_palindromes(unlist(all_p[is_n]))
any(unlist(all_p[!is_n]) %in% unlist(n_rp))
}
sum(map_lgl(puzzle_data, check_ip_ssl))
## [1] 231