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 %>%
as.integer()
}
puzzle_data <- local({
GET(paste0(base_url, "/input"),
session_cookie) %>%
content(encoding = "UTF-8") %>%
parse_puzzle_data()
})
2 Puzzle Day 1
2.1 Part 1
2.1.1 Description
— Day 1: Report Repair —
After saving Christmas five years in a row, you’ve decided to take a vacation at a nice resort on a tropical island. Surely, Christmas will go on without you.
The tropical island has its own currency and is entirely cash-only. The gold coins used there have a little picture of a starfish; the locals just call them stars. None of the currency exchanges seem to have heard of them, but somehow, you’ll need to find fifty of these coins by the time you arrive so you can pay the deposit on your room.
To save your vacation, you need to get all fifty stars by December 25th.
Collect stars by solving puzzles. Two puzzles will be made available on each day in the Advent calendar; the second puzzle is unlocked when you complete the first. Each puzzle grants one star. Good luck!
Before you leave, the Elves in accounting just need you to fix your expense report (your puzzle input); apparently, something isn’t quite adding up.
Specifically, they need you to find the two entries that sum to 2020 and then multiply those two numbers together.
For example, suppose your expense report contained the following:
1721
979
366
299
675
1456
In this list, the two entries that sum to 2020 are 1721 and 299. Multiplying them together produces 1721 * 299 = 514579, so the correct answer is 514579.
Of course, your expense report is much larger. Find the two entries that sum to 2020; what do you get if you multiply them together?
2.1.2 Solution
Hohoho! Welcome to this year’s AoC. Let the games begin (again).
For this puzzle we simply need to find the two numbers that sum up to 2020. We sort the list beforehand and work with a pointer on the left and the right and move them accordingly.
find_pair <- function(nrs = puzzle_data, target = 2020L) {
nrs <- sort(nrs)
i <- 1L
j <- length(nrs)
while (i < j) {
s <- nrs[i] + nrs[j]
if (s == target) {
return(nrs[i] * nrs[j])
} else if (s < target) {
i <- i + 1L
} else { # s > target
j <- j - 1L
}
}
NA_integer_
}
find_pair(puzzle_data)
## [1] 960075
2.2 Part 2
2.2.1 Description
— Part Two —
The Elves in accounting are thankful for your help; one of them even offers you a starfish coin they had left over from a past vacation. They offer you a second one if you can find three numbers in your expense report that meet the same criteria.
Using the above example again, the three entries that sum to 2020 are 979, 366, and 675. Multiplying them together produces the answer, 241861950.
In your expense report, what is the product of the three entries that sum to 2020?
2.2.2 Solution
Pretty much the same as part 1, but this time we need to find a triple satisfying the condition. We fix the first number and use the same idea as in part 1 with the two pointers.
find_triple <- function(nrs = puzzle_data, target = 2020L) {
nrs <- sort(nrs)
n <- length(nrs)
for (i in 1L:(n - 2L)) {
a <- nrs[i]
l <- i + 1L
r <- n
while (l < r) {
s <- a + nrs[l] + nrs[r]
if (s == target) {
return(a * nrs[l] * nrs[r])
} else if (s < target) {
l <- l + 1L
} else { # s > target
r <- r - 1L
}
}
}
NA_integer_
}
find_triple(puzzle_data)
## [1] 212900130