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_split("") %>%
do.call(rbind, .)
}
puzzle_data <- local({
GET(paste0(base_url, "/input"),
session_cookie) %>%
content(encoding = "UTF-8") %>%
parse_puzzle_data()
})
2 Puzzle Day 19
2.1 Part 1
2.1.1 Description
— Day 19: A Series of Tubes —
Somehow, a network packet got lost and ended up here. It’s trying to follow a routing diagram (your puzzle input), but it’s confused about where to go.
Its starting point is just off the top of the diagram. Lines (drawn with |, -, and +) show the path it needs to take, starting by going down onto the only line connected to the top of the diagram. It needs to follow this path until it reaches the end (located somewhere within the diagram) and stop there.
Sometimes, the lines cross over each other; in these cases, it needs to continue going the same direction, and only turn left or right when there’s no other option. In addition, someone has left letters on the line; these also don’t change its direction, but it can use them to keep track of where it’s been. For example:
|
| +--+
A | C
F---|----E|--+
| | | D
+B-+ +--+
Given this diagram, the packet needs to take the following path:
-
Starting at the only line touching the top of the diagram, it must go down, pass through
A, and continue onward to the first+. -
Travel right, up, and right, passing through
Bin the process. -
Continue down (collecting
C), right, and up (collectingD). -
Finally, go all the way left through
Eand stopping atF.
Following the path to the end, the letters it sees on its path are ABCDEF.
The little packet looks up at you, hoping you can help it find the way. What letters will it see (in the order it would see them) if it follows the path? (The routing diagram is very wide; make sure you view it without line wrapping.)
2.1.2 Solution
We start at the top left corner and move along he directions. To do so, we keep track of the current direction of movement (horizontal vs. vertical) and change it only at crossroads.
collect_letters <- function(path) {
start <- cbind(row = 1L, col = which(path[1, ] != " ")[1L])
dirs <- rbind(
">" = c(0L, 1L),
"v" = c(1L, 0L),
"<" = c(0L, -1L),
"^" = c(-1L, 0L)
)
nr_loots <- sum(!path %in% c(" ", "-", "|", "+"))
cur_dir <- 2L
pos <- start
loot <- character(0L)
steps <- 1L
while (length(loot) < nr_loots) {
steps <- steps + 1L
pos <- t(t(dirs[cur_dir, , drop = FALSE]) + c(pos))
next_field <- path[pos]
if (next_field == "+") {
## we need to change direction
nbs <- path[t(t(dirs) + c(pos))]
target <- if_else(cur_dir %% 2L == 0L, "-", "|")
cur_dir <- which(nbs == target)
} else if (!next_field %in% c("-", "|")) {
## we reached a letter -> include it
loot <- c(loot, next_field)
}
}
list(path = loot %>%
paste(collapse = ""),
steps = steps)
}
res <- collect_letters(puzzle_data)
res$path
## [1] "MOABEUCWQS"
2.2 Part 2
2.2.1 Description
— Part Two —
The packet is curious how many steps it needs to go.
For example, using the same routing diagram from the example above…
|
| +--+
A | C
F---|--|-E---+
| | | D
+B-+ +--+
…the packet would go:
-
6steps down (including the first line at the top of the diagram). -
3steps right. -
4steps up. -
3steps right. -
4steps down. -
3steps right. -
2steps up. -
13steps left (including theFit stops on).
This would result in a total of 38 steps.
How many steps does the packet need to go?
2.2.2 Solution
We need to return the number of steps we took.
res$steps
## [1] 18058