1 Setup
1.1 Libraries
library(httr)
library(xml2)
library(magrittr)
library(dplyr)
library(purrr)
library(stringr)
library(collections)
library(gtools)
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 24
2.1 Part 1
2.1.1 Description
— Day 24: Air Duct Spelunking —
You’ve finally met your match; the doors that provide access to the roof are locked tight, and all of the controls and related electronics are inaccessible. You simply can’t reach them.
The robot that cleans the air ducts, however, can.
It’s not a very fast little robot, but you reconfigure it to be able to interface with some of the exposed wires that have been routed through the HVAC system. If you can direct it to each of those locations, you should be able to bypass the security controls.
You extract the duct layout for this area from some blueprints you acquired and create a map with the relevant locations marked (your puzzle input). 0
is your current location, from which the cleaning robot embarks; the other numbers are (in no particular order) the locations the robot needs to visit at least once each. Walls are marked as #
, and open passages are marked as .
. Numbers behave like open passages.
For example, suppose you have a map like the following:
###########
#0.1.....2#
#.#######.#
#4.......3#
###########
To reach all of the points of interest as quickly as possible, you would have the robot take the following path:
-
0
to4
(2
steps) -
4
to1
(4
steps; it can’t move diagonally) -
1
to2
(6
steps) -
2
to3
(2
steps)
Since the robot isn’t very fast, you need to find it the shortest route. This path is the fewest steps (in the above example, a total of 14
) required to start at 0
and then visit every other location at least once.
Given your actual map, and starting from location 0
, what is the fewest number of steps required to visit every non-0
number marked on the map at least once?
2.1.2 Solution
This is another instance of a path finding algorithm. We will use an A* search. Since the order of the targets is not determined, we will first solve this small Travelling Salesman Problem by brute force to determine the optimal order of targets.
get_route <- function(maze, roundtrip = FALSE, start = "0") {
targets <- targets <- maze %>%
str_extract_all("[^.#]") %>%
unlist()
n <- length(targets)
targets_pos <- maze %in% targets
dim(targets_pos) <- dim(maze)
targets_pos <- which(targets_pos, arr.ind = TRUE) %>%
set_rownames(., maze[.])
dist <- outer(1:n, 1:n, \(i, j) {
abs(targets_pos[i, "row"] - targets_pos[j ,"row"]) +
abs(targets_pos[i, "col"] - targets_pos[j , "col"])
})
rownames(dist) <- colnames(dist) <- targets
path <- setdiff(targets, start)
all_routes <- cbind(start,
permutations(length(path), length(path), path))
if (roundtrip) {
all_routes <- cbind(all_routes, start)
}
min_dist <- Inf
for (route_idx in 1:nrow(all_routes)) {
route <- all_routes[route_idx, ]
cur_dist <- 0L
for (i in seq(1, length(route) - 1L)) {
cur_dist <- cur_dist + dist[route[i], route[i + 1L]]
}
if (cur_dist < min_dist) {
best_route <- route
min_dist <- cur_dist
}
}
targets_pos[tail(best_route, -1L), , drop = FALSE]
}
get_neighbors <- function(maze, cur_pos, target) {
cur_pos <- cur_pos[, c("row", "col"), drop = FALSE]
dims <- dim(maze)
dir <- rbind(">" = c(0L, 1L),
"v" = c(1L, 0L),
"<" = c(0L, -1L),
"^" = c(-1L, 0L)) %>%
set_colnames(c("row", "col"))
target_pos <- target[, c("row", "col"), drop = FALSE]
cand <- t(t(dir) + c(cur_pos))
cand <- cand[between(cand[, 1L], 1L, dims[1L]) &
between(cand[, 2L], 1L, dims[2L]), , drop = FALSE]
walkable <- maze[cand] != "#"
nbs <- cand[walkable, , drop = FALSE]
heuristic <- t(abs(t(nbs) - c(target_pos))) %>%
rowSums()
cbind(nbs, h = heuristic)
}
add_to_queue <- function(queue, cost, nbs, next_target, visited, target_index) {
if (nrow(nbs) > 0) {
for (i in 1:nrow(nbs)) {
new_pos <- nbs[i, , drop = FALSE]
f <- cost + new_pos[, "h"]
new_pos <- cbind(new_pos, "cost" = cost + 1L)
load <- list(visited = visited,
next_target = next_target[, c("row", "col"), drop = FALSE],
cur_pos = new_pos[, c("row", "col", "cost"), drop = FALSE],
target_index = target_index)
queue$push(load, -f)
}
}
}
find_shortest_path <- function(maze, roundtrip = FALSE, start = "0") {
shortest_path <- Inf
pq <- priority_queue()
hash <- list()
start_pos <- which(maze == start, arr.ind = TRUE) %>%
set_colnames(c("row", "col")) %>%
set_rownames(start) %>%
cbind(cost = 0L)
route <- get_route(maze, roundtrip, start)
visited <- rep(FALSE, nrow(route)) %>%
set_names(rownames(route))
target_index <- 1L
next_target <- route[target_index, , drop = FALSE]
nbs <- get_neighbors(maze, start_pos, next_target)
add_to_queue(pq, start_pos[, "cost"], nbs, next_target, visited, target_index)
target_ids <- paste(as.numeric(visited), collapse = "")
hash[[target_ids]] <- rbind(hash[[target_ids]],
start_pos[, c("row", "col"), drop = FALSE])
while (pq$size() > 0L) {
next_node <- pq$pop()
cur_pos <- next_node$cur_pos
next_target <- next_node$next_target
if (all(cur_pos[, c("row", "col")] == next_target[, c("row", "col")])) {
## we hit a target
target <- rownames(next_target)
if (!next_node$visited[target]) {
next_node$visited[target] <- TRUE
next_node$target_index <- next_node$target_index + 1
}
if (all(next_node$visited)) {
shortest_path <- cur_pos[, "cost"]
break
} else {
next_target <- route[next_node$target_index, , drop = FALSE]
}
}
target_ids <- paste(as.numeric(next_node$visited), collapse = "")
hash[[target_ids]] <- rbind(hash[[target_ids]],
cur_pos[, c("row", "col"), drop = FALSE])
nbs <- get_neighbors(maze, cur_pos, next_target)
dupes <- !duplicated(rbind(hash[[target_ids]],
nbs[, c("row", "col"), drop = FALSE]))
idx <- dupes[-(1:nrow(hash[[target_ids]]))]
add_to_queue(pq, cur_pos[, "cost"], nbs[idx, , drop = FALSE],
next_target, next_node$visited, next_node$target_index)
}
shortest_path
}
find_shortest_path(puzzle_data)
## [1] 462
2.2 Part 2
2.2.1 Description
— Part Two —
Of course, if you leave the cleaning robot somewhere weird, someone is bound to notice.
What is the fewest number of steps required to start at 0
, visit every non-0
number marked on the map at least once, and then return to 0
?
2.2.2 Solution
For the second part, we rquire the TSP to end in the start city (which leads to another route).
find_shortest_path(puzzle_data, TRUE)
## [1] 676