1 Setup
1.1 Libraries
library(httr)
library(xml2)
library(magrittr)
library(dplyr)
library(purrr)
library(stringr)
library(DT)
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)
}
str_split(text_block, " ") %>%
map(~ c(., NA_character_)[1:3] %>%
set_names(c("op", "arg1", "arg2"))) %>%
bind_rows()
}
puzzle_data <- local({
GET(paste0(base_url, "/input"),
session_cookie) %>%
content(encoding = "UTF-8") %>%
parse_puzzle_data()
})
2 Puzzle Day 12
2.1 Part 1
2.1.1 Description
— Day 12: Leonardo’s Monorail —
You finally reach the top floor of this building: a garden with a slanted glass ceiling. Looks like there are no more stars to be had.
While sitting on a nearby bench amidst some tiger lilies, you manage to decrypt some of the files you extracted from the servers downstairs.
According to these documents, Easter Bunny HQ isn’t just this building - it’s a collection of buildings in the nearby area. They’re all connected by a local monorail, and there’s another building not far from here! Unfortunately, being night, the monorail is currently not operating.
You remotely connect to the monorail control systems and discover that the boot sequence expects a password. The password-checking logic (your puzzle input) is easy to extract, but the code it uses is strange: it’s assembunny code designed for the new computer you just assembled. You’ll have to execute the code and get the password.
The assembunny code you’ve extracted operates on four registers (a
, b
, c
, and d
) that start at 0
and can hold any integer. However, it seems to make use of only a few instructions:
-
cpy x y
copiesx
(either an integer or the value of a register) into registery
. -
inc x
increases the value of registerx
by one. -
dec x
decreases the value of registerx
by one. -
jnz x y
jumps to an instructiony
away (positive means forward; negative means backward), but only ifx
is not zero.
The jnz
instruction moves relative to itself: an offset of -1
would continue at the previous instruction, while an offset of 2
would skip over the next instruction.
For example:
cpy 41 a
inc a
inc a
dec a
jnz a 2
dec a
The above code would set register a
to 41
, increase its value by 2
, decrease its value by 1
, and then skip the last dec a
(because a
is not zero, so the jnz a 2
skips it), leaving register a
at 42
. When you move past the last instruction, the program halts.
After executing the assembunny code in your puzzle input, what value is left in register a
?
2.1.2 Solution
The instructions are the following:
codes <- puzzle_data %>%
set_colnames(c("Operation", "Argument 1", "Argument 2")) %>%
mutate(outer_block = c(0L, 0L, 0L, 0L, 0L, 0L, 1L, 0L, 1L, 2L, 1L, 0L,
1L, 0L, 0L, 2L, 0L, 3L, 1L, 0L, 1L, 0L, 3L))
datatable(
codes,
class = c("compact", "nowrap", "hover", "row-border"),
options = list(
pageLength = nrow(codes),
dom = "t",
ordering = FALSE,
columnDefs = list(
list(
className = "dt-center", targets = "_all"
),
list(
visible = FALSE,
targets = 4
)
))
) %>%
formatStyle(1:3,
"outer_block",
target = "cell",
backgroundColor = styleEqual(0:3, c("transparent", "#FDE725FF", "#440154FF",
"#21908CFF")),
color = styleEqual(0:3, rep(c("black", "white", "black"), c(2L, 1L, 1L)))
)
Analyzing the instructions one will notice certain patterns:
- A
inc a | dec b | jnz b -2
block first increasesa
by one, the decreasesb
by one and jumps back two instructions - that is repeats this sequence untilb
equals zero. This is the same as addingb
toa
(we will refer to this pattern as addition loop). - More generally, a
dec a | jnz a -n
sequence forms a loop, which repeats the previousn
linesa
times. - Finally, a
jnz a n
with a positiven
skips the nextn
lines conditionally ona
.
With these observations we conclude that
- Lines 6 to 9 are skipped.
- We have 2 addition loops marked in yellow (not counting the skipped loop in lines 6 to 9).
- We have 2 outer loops marked in turquoise and purple respectively.
- The turquoise loop simply adds the value of
d
c
times.
get_code <- function(a, b, c, d) {
for (i in 1:d) {
c <- a
a <- a + b
b <- c
}
a + 14L * 14L
}
get_code(1L, 1L, 0L, 26L)
## [1] 318007
2.2 Part 2
2.2.1 Description
— Part Two —
As you head down the fire escape to the monorail, you notice it didn’t start; register c
needs to be initialized to the position of the ignition key.
If you instead initialize register c
to be 1
, what value is now left in register a
?
2.2.2 Solution
With a different starting value for c
we now include lines 6 - 9, which simply set
different values for c
and d
.
get_code(1L, 1L, 7L, 33L)
## [1] 9227661
2.3 Brute Force Algorithm
If you simply follow the rules line by line you could use the following algorithm.
However, since this would introduce a lot of loops where a simple addition would do the
same job, it would be awefully slow (imagine calculating a + 100
by adding 1 to a
a
hundred times).
automata <- function(ops = puzzle_data) {
reg = c(a = 0L, b = 0L, c = 0L, d = 0L)
line <- 1
cpy <- function(arg1, arg2) {
new <- suppressWarnings(as.integer(arg1))
if (is.na(new)) {
new <- reg[arg1]
}
reg[arg2] <<- new
line <<- line + 1L
}
inc <- function(arg1, arg2) {
reg[arg1] <<- reg[arg1] + 1L
line <<- line + 1L
}
dec <- function(arg1, arg2) {
reg[arg1] <<- reg[arg1] - 1L
line <<- line + 1L
}
jnz <- function(arg1, arg2) {
arg2 <- as.integer(arg2)
chk <- suppressWarnings(as.integer(arg1))
if (is.na(chk)) {
chk <- reg[arg1]
}
if (chk != 0L) {
line <<- line + arg2
} else {
line <<- line + 1
}
}
n <- nrow(ops)
while (line <= n) {
ops %>%
slice(line) %>%
rename(name = op) %>%
as.list() %>%
do.call(call, .) %>%
eval()
}
reg["a"]
}