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, .) %>%
set_colnames(c("reg", "op", "n", ".skip", "cond_reg", "cond_eq", "cond_n")) %>%
as_tibble() %>%
mutate(across(ends_with("n"), as.integer)) %>%
select(-.skip)
}
puzzle_data <- local({
GET(paste0(base_url, "/input"),
session_cookie) %>%
content(encoding = "UTF-8") %>%
parse_puzzle_data()
})
2 Puzzle Day 8
2.1 Part 1
2.1.1 Description
— Day 8: I Heard You Like Registers —
You receive a signal directly from the CPU. Because of your recent assistance with jump instructions, it would like you to compute the result of a series of unusual register instructions.
Each instruction consists of several parts: the register to modify, whether to increase or decrease that register’s value, the amount by which to increase or decrease it, and a condition. If the condition fails, skip the instruction without modifying the register. The registers all start at 0. The instructions look like this:
b inc 5 if a > 1
a inc 1 if b < 5
c dec -10 if a >= 1
c inc -20 if c == 10
These instructions would be processed as follows:
-
Because
astarts at0, it is not greater than1, and sobis not modified. -
ais increased by1(to1) becausebis less than5(it is0). -
cis decreased by-10(to10) becauseais now greater than or equal to1(it is1). -
cis increased by-20(to-10) becausecis equal to10.
After this process, the largest value in any register is 1.
You might also encounter <= (less than or equal to) or != (not equal to). However, the CPU doesn’t have the bandwidth to tell you what all the registers are named, and leaves that to you to determine.
What is the largest value in any register after completing the instructions in your puzzle input?
2.1.2 Solution
We write a parser, which uses a list as register.
parse_line <- function(reg, op, n, cond_reg, cond_eq, cond_n, register) {
## first check whether the registers are already present, if not create them
if (!reg %in% names(register)) {
register[[reg]] <- 0L
}
if (!cond_reg %in% names(register)) {
register[[cond_reg]] <- 0L
}
cond <- do.call(cond_eq, list(register[[cond_reg]], cond_n))
if (cond) {
sign <- if_else(op == "dec", -1L, 1L)
register[[reg]] <- register[[reg]] + sign * n
}
register
}
evaluate_ops <- function(ops = puzzle_data, return_max = FALSE) {
max_val <- -Inf
register <- list()
for (i in 1:nrow(ops)) {
line <- ops %>%
slice(i) %>%
as.list()
line$register <- register
register <- do.call(parse_line, line)
max_val <- max(c(max_val, unlist(register)))
}
if (return_max) {
max_val
} else {
max(unlist(register))
}
}
evaluate_ops(puzzle_data)
## [1] 4567
2.2 Part 2
2.2.1 Description
— Part Two —
To be safe, the CPU also needs to know the highest value held in any register during this process so that it can decide how much memory to allocate to these operations. For example, in the above instructions, the highest value ever held was 10 (in register c after the third instruction was evaluated).
2.2.2 Solution
We keep track about the intermediate results and return their maximum.
evaluate_ops(puzzle_data, TRUE)
## [1] 5636