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(bit64)
library(igraph)
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)
}
res <- text_block %>%
str_extract_all("\\d+") %>%
do.call(rbind, .)
storage.mode(res) <- "integer"
colnames(res) <- c("l", "w", "h")
res
}
puzzle_data <- local({
GET(paste0(base_url, "/input"),
session_cookie) %>%
content(encoding = "UTF-8") %>%
parse_puzzle_data()
})
2 Puzzle Day 2
2.1 Part 1
2.1.1 Description
— Day 2: I Was Told There Would Be No Math —
The elves are running low on wrapping paper, and so they need to submit an order for more. They have a list of the dimensions (length l, width w, and height h) of each present, and only want to order exactly as much as they need.
Fortunately, every present is a box (a perfect right rectangular prism), which makes calculating the required wrapping paper for each gift a little easier: find the surface area of the box, which is 2lw + 2wh + 2hl. The elves also need a little extra paper for each present: the area of the smallest side.
For example:
-
A present with dimensions
2x3x4requires26 + 212 + 2*8 = 52square feet of wrapping paper plus6square feet of slack, for a total of58square feet. -
A present with dimensions
1x1x10requires21 + 210 + 2*10 = 42square feet of wrapping paper plus1square foot of slack, for a total of43square feet.
All numbers in the elves’ list are in feet. How many total square feet of wrapping paper should they order?
2.1.2 Solution
We calculate the surface area of each side and the respective minimum and sum up all numbers.
all_sides <- combn(3L, 2L) %>%
t()
surfaces <- apply(all_sides, 1L, function(s) {
puzzle_data[, s[1L]] * puzzle_data[, s[2L]]
}) %>%
cbind(
.,
apply(., 1, min)
)
t(t(surfaces) * rep(2:1, c(3, 1))) %>%
colSums() %>%
sum()
## [1] 1586300
2.2 Part 2
2.2.1 Description
— Part Two —
The elves are also running low on ribbon. Ribbon is all the same width, so they only have to worry about the length they need to order, which they would again like to be exact.
The ribbon required to wrap a present is the shortest distance around its sides, or the smallest perimeter of any one face. Each present also requires a bow made out of ribbon as well; the feet of ribbon required for the perfect bow is equal to the cubic feet of volume of the present. Don’t ask how they tie the bow, though; they’ll never tell.
For example:
-
A present with dimensions
2x3x4requires2+2+3+3 = 10feet of ribbon to wrap the present plus234 = 24feet of ribbon for the bow, for a total of34feet. -
A present with dimensions
1x1x10requires1+1+1+1 = 4feet of ribbon to wrap the present plus1110 = 10feet of ribbon for the bow, for a total of14feet.
How many total feet of ribbon should they order?
2.2.2 Solution
For each combination we calculate the perimeter, take the smallest and add the volume.
perimeter <- apply(all_sides, 1L, function(s) {
2L * rowSums(puzzle_data[, s])
}) %>%
apply(1L, min)
volume <- apply(puzzle_data, 1L, prod)
sum(perimeter + volume)
## [1] 3737498