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)
}
text_block %>%
as.integer()
}
puzzle_data <- local({
GET(paste0(base_url, "/input"),
session_cookie) %>%
content(encoding = "UTF-8") %>%
parse_puzzle_data()
})
2 Puzzle Day 19
2.1 Part 1
2.1.1 Description
— Day 19: An Elephant Named Joseph —
The Elves contact you over a highly secure emergency channel. Back at the North Pole, the Elves are busy misunderstanding White Elephant parties.
Each Elf brings a present. They all sit in a circle, numbered starting with position 1. Then, starting with the first Elf, they take turns stealing all the presents from the Elf to their left. An Elf with no presents is removed from the circle and does not take turns.
For example, with five Elves (numbered 1 to 5):
1
5 2
4 3
-
Elf
1takes Elf2’s present. -
Elf
2has no presents and is skipped. -
Elf
3takes Elf4’s present. -
Elf
4has no presents and is also skipped. -
Elf
5takes Elf1’s two presents. -
Neither Elf
1nor Elf2have any presents, so both are skipped. -
Elf
3takes Elf5’s three presents.
So, with five Elves, the Elf that sits starting in position 3 gets all the presents.
With the number of Elves given in your puzzle input, which Elf gets all the presents?
2.1.2 Solution
For the first part we implement the selection process straight forward:
eliminate_elves <- function(elves) {
n <- length(elves)
if (n == 1) {
return(elves)
}
if (n %% 2L == 0L) {
remaining <- elves[seq(1L, by = 2L, length.out = n / 2L)]
} else {
remaining <- elves [seq(3L, by = 2L, length.out = floor(n / 2L))]
}
Recall(remaining)
}
eliminate_elves(1L:puzzle_data)
## [1] 1834903
2.2 Part 2
2.2.1 Description
— Part Two —
Realizing the folly of their present-exchange rules, the Elves agree to instead steal presents from the Elf directly across the circle. If two Elves are across the circle, the one on the left (from the perspective of the stealer) is stolen from. The other rules remain unchanged: Elves with no presents are removed from the circle entirely, and the other elves move in slightly to keep the circle evenly spaced.
For example, with five Elves (again numbered 1 to 5):
-
The Elves sit in a circle; Elf
1goes first:1 5 2 4 3 -
Elves
3and4are across the circle; Elf3’s present is stolen, being the one to the left. Elf3leaves the circle, and the rest of the Elves move in:1 1 5 2 --> 5 2 4 - 4 -
Elf
2steals from the Elf directly across the circle, Elf5:1 1 - 2 --> 2 4 4 -
Next is Elf
4who, choosing between Elves1and2, steals from Elf1:- 2 2 --> 4 4 -
Finally, Elf
2steals from Elf4:2 --> 2 -
So, with five Elves, the Elf that sits starting in position 2 gets all the presents.
With the number of Elves given in your puzzle input, which Elf now gets all the presents?
2.2.2 Solution
For the second part, let’s have a look at the winners for n = 1, …, 27:
winners <- tibble(n = 1:27,
Winner = c(
1, 1, 3, 1, 2, 3, 5, 7, 9, 1, 2, 3, 4, 5, 6, 7, 8, 9, 11,
13, 15, 17, 19, 21, 23, 25, 27)) %>%
mutate(
m = 3 ^ floor(log(n, base = 3)),
type = case_when(
n == m ~ "power of 3",
m < n & n <= 2 * m ~ "inc 1",
2 * m < n & n < 3 * m ~ "inc 2"
)
)
winners %>%
datatable(
class = c("compact", "nowrap", "hover", "row-border"),
rownames = FALSE,
options = list(
pageLength = nrow(winners),
dom = "t",
ordering = FALSE,
columnDefs = list(
list(
className = "dt-center", targets = "_all"
),
list(
visible = FALSE,
targets = 2:3
)
))
) %>%
formatStyle(1:2,
"type",
target = "row",
backgroundColor = styleEqual(c("power of 3", "inc 1", "inc 2"),
c("#440154FF", "#21908CFF", "#FDE725FF")),
color = styleEqual(c("power of 3", "inc 1", "inc 2"),
c("white", "black", "black"))
)
We observe:
- If the numbers of elves is a power of 3 the last elf will win.
- Between powers the id of the winner elf increases linearly by a rate of 1 for the first 3m elves and then by a rate of 2.
With this we can get a closed formula for the winning elf:
get_winner <- function(N) {
m <- 3 ^ floor(log(N, base = 3L))
if (m == N) {
N
} else if (m < N && N <= 2 * m) {
N - m
} else if (2 * m < N && N < 3 * m) {
2 * N - 3 * m
}
}
get_winner(puzzle_data)
## [1] 1420280