1 Setup
1.1 Libraries
library(httr)
library(xml2)
library(magrittr)
library(tibble)
library(dplyr)
library(purrr)
library(igraph)
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/2021/day/", params$task_nr)
puzzle <- GET(base_url,
session_cookie) %>%
content(encoding = "UTF-8") %>%
xml_find_all("///article") %>%
lapply(as.character)
## use curly braces in the last pipe to avoid that . is additionally added
## as the first argument
puzzle_data <- GET(paste0(base_url, "/input"),
session_cookie) %>%
content(encoding = "UTF-8") %>%
str_split("\n") %>%
`[[`(1) %>%
head(-1L) %>%
lapply(str2lang) %>%
do.call(graph_from_literal, .)
2 Puzzle Day 12
2.1 Part 1
2.1.1 Description
— Day 12: Passage Pathing —
With your submarine’s subterranean subsystems subsisting suboptimally, the only way you’re getting out of this cave anytime soon is by finding a path yourself. Not just a path - the only way to know if you’ve found the best path is to find all of them.
Fortunately, the sensors are still mostly working, and so you build a rough map of the remaining caves (your puzzle input). For example:
start-A
start-b
A-c
A-b
b-d
A-end
b-end
This is a list of how all of the caves are connected. You start in the cave named start
, and your destination is the cave named end
. An entry like b-d
means that cave b
is connected to cave d
- that is, you can move between them.
So, the above cave system looks roughly like this:
start
/ \
c--A-----b--d
\ /
end
Your goal is to find the number of distinct paths that start at start
, end at end
, and don’t visit small caves more than once. There are two types of caves: big caves (written in uppercase, like A
) and small caves (written in lowercase, like b
). It would be a waste of time to visit any small cave more than once, but big caves are large enough that it might be worth visiting them multiple times. So, all paths you find should visit small caves at most once, and can visit big caves any number of times.
Given these rules, there are 10
paths through this example cave system:
start,A,b,A,c,A,end
start,A,b,A,end
start,A,b,end
start,A,c,A,b,A,end
start,A,c,A,b,end
start,A,c,A,end
start,A,end
start,b,A,c,A,end
start,b,A,end
start,b,end
(Each line in the above list corresponds to a single path; the caves visited by that path are listed in the order they are visited and separated by commas.)
Note that in this cave system, cave d
is never visited by any path: to do so, cave b
would need to be visited twice (once on the way to cave d
and a second time when returning from cave d
), and since cave b
is small, this is not allowed.
Here is a slightly larger example:
dc-end
HN-start
start-kj
dc-start
dc-HN
LN-dc
HN-end
kj-sa
kj-HN
kj-dc
The 19
paths through it are as follows:
start,HN,dc,HN,end
start,HN,dc,HN,kj,HN,end
start,HN,dc,end
start,HN,dc,kj,HN,end
start,HN,end
start,HN,kj,HN,dc,HN,end
start,HN,kj,HN,dc,end
start,HN,kj,HN,end
start,HN,kj,dc,HN,end
start,HN,kj,dc,end
start,dc,HN,end
start,dc,HN,kj,HN,end
start,dc,end
start,dc,kj,HN,end
start,kj,HN,dc,HN,end
start,kj,HN,dc,end
start,kj,HN,end
start,kj,dc,HN,end
start,kj,dc,end
Finally, this even larger example has 226
paths through it:
fs-end
he-DX
fs-he
start-DX
pj-DX
end-zg
zg-sl
zg-pj
pj-he
RW-he
fs-DX
pj-RW
zg-RW
start-pj
he-WI
zg-he
pj-fs
start-RW
How many paths through this cave system are there that visit small caves at most once?
2.1.2 Solution
We use the library igraph
to deal with the network. First of all, lets plot the graph:
V(puzzle_data)$single <- str_detect(V(puzzle_data)$name, "^[:lower:]+$")
V(puzzle_data)$color <- "#F9DB6D"
V(puzzle_data)[c("start", "end")]$color <- c("#464D77", "#36827F")
V(puzzle_data)[c("start", "end")]$label.color <- "white"
set.seed(1) ## to keep plot the same
op <- par(bg = "transparent")
plot(puzzle_data, layout = layout_with_lgl(puzzle_data, root = "start"),
vertex.size = 25)
Figure 2.1: Cave Network
Then we can define an easy recursive function which walks the graph according to the following rules:
- In each iteration we maintain a list of visitable neighbors. Those are the neighbors which are not yet visited in the current walk, unless it is a big cave, which can - by definition - visited nore than once.
- If we reach
end
we increase the counter by 1 and the function ends. - Otherwise we call the function recursively again with the new neighbor as next node and where we add the current node to the path of visited nodes.
walk_graph <- function(G = puzzle_data) {
do_walk <- function(node, path) {
path <- c(node, path)
nbs <- neighbors(G, node)
free_nbs <- difference(nbs, path[single])
if (node$name == "end") {
## we reached "end"
res <- 1
} else {
res <- 0
for (idx in free_nbs) {
res <- res + Recall(V(G)[idx], path)
}
}
res
}
do_walk(V(G)["start"], NULL)
}
walk_graph()
## [1] 4707
2.2 Part 2
2.2.1 Description
— Part Two —
After reviewing the available paths, you realize you might have time to visit a single small cave twice. Specifically, big caves can be visited any number of times, a single small cave can be visited at most twice, and the remaining small caves can be visited at most once. However, the caves named start
and end
can only be visited exactly once each: once you leave the start
cave, you may not return to it, and once you reach the end
cave, the path must end immediately.
Now, the 36
possible paths through the first example above are:
start,A,b,A,b,A,c,A,end
start,A,b,A,b,A,end
start,A,b,A,b,end
start,A,b,A,c,A,b,A,end
start,A,b,A,c,A,b,end
start,A,b,A,c,A,c,A,end
start,A,b,A,c,A,end
start,A,b,A,end
start,A,b,d,b,A,c,A,end
start,A,b,d,b,A,end
start,A,b,d,b,end
start,A,b,end
start,A,c,A,b,A,b,A,end
start,A,c,A,b,A,b,end
start,A,c,A,b,A,c,A,end
start,A,c,A,b,A,end
start,A,c,A,b,d,b,A,end
start,A,c,A,b,d,b,end
start,A,c,A,b,end
start,A,c,A,c,A,b,A,end
start,A,c,A,c,A,b,end
start,A,c,A,c,A,end
start,A,c,A,end
start,A,end
start,b,A,b,A,c,A,end
start,b,A,b,A,end
start,b,A,b,end
start,b,A,c,A,b,A,end
start,b,A,c,A,b,end
start,b,A,c,A,c,A,end
start,b,A,c,A,end
start,b,A,end
start,b,d,b,A,c,A,end
start,b,d,b,A,end
start,b,d,b,end
start,b,end
The slightly larger example above now has 103
paths through it, and the even larger example now has 3509
paths through it.
Given these new rules, how many paths through this cave system are there?
2.2.2 Solution
For the second part we need to relax the condition in so far that one small caves can be visited at most twice. To do so, we check if there is already a small cave, which was visited twice and only in this condition we remove small caves from the list of visitable caves.
N.B. The following code includes 2 versions of the same (and part one is largely a clone
of solution 1): a version with the igraph
framework and one with a much simpler data
structure. It turned out that the igraph
algo, though correct runs by far too long. My
guess is that this due to copy on change semantics and the fact that an igraph
object is
rather heavy. Thus, I include a simpler - yet less verbose - version relying on basic data
structures.
walk_graph2 <- function(G = puzzle_data, use_igraph = FALSE) {
do_walk_igraph <- function(node, path) {
path <- c(node, path)
nbs <- difference(neighbors(G, node), V(G)["start"])
if (any(duplicated(path[single]))) {
free_nbs <- difference(nbs, path[single])
} else {
free_nbs <- nbs
}
if (node$name == "end") {
## we reached "end"
res <- 1
} else {
res <- 0
for (idx in free_nbs) {
res <- res + Recall(V(G)[idx], path)
}
}
res
}
do_walk_basic <- function(node, path) {
path <- c(node, path)
nbs <- setdiff(G[[node]], "start")
small_caves <- path[str_detect(path, "^[:lower:]+$")]
if (any(duplicated(small_caves))) {
free_nbs <- setdiff(nbs, small_caves)
} else {
free_nbs <- nbs
}
if (node == "end") {
## we reached "end"
res <- 1
} else {
res <- 0
for (nb in free_nbs) {
res <- res + Recall(nb, path)
}
}
res
}
if (use_igraph) {
do_walk_igraph(V(G)["start"], NULL)
} else {
G <- as_data_frame(G) %>%
### add edges in both directions
bind_rows((.) %>% rename(from = to, to = from)) %>%
group_by(from) %>%
group_map(~ list(.x$to) %>%
set_names(.y %>% pull(from))) %>%
flatten()
do_walk_basic("start", NULL)
}
}
walk_graph2()
## [1] 130493