1 Setup
1.1 Libraries
library(httr)
library(xml2)
library(magrittr)
library(dplyr)
library(purrr)
library(stringr)
library(bit64)
library(numbers)
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)
}
text_block[nzchar(text_block)] %>%
str_split("") %>%
do.call(rbind, .)
}
puzzle_data <- local({
GET(paste0(base_url, "/input"),
session_cookie) %>%
content(encoding = "UTF-8") %>%
parse_puzzle_data()
})
2 Puzzle Day 21
2.1 Part 1
2.1.1 Description
— Day 21: Step Counter —
You manage to catch the airship right as it’s dropping someone else off on their all-expenses-paid trip to Desert Island! It even helpfully drops you off near the gardener and his massive farm.
“You got the sand flowing again! Great work! Now we just need to wait until we have enough sand to filter the water for Snow Island and we’ll have snow again in no time.”
While you wait, one of the Elves that works with the gardener heard how good you are at solving problems and would like your help. He needs to get his steps in for the day, and so he’d like to know which garden plots he can reach with exactly his remaining 64 steps.
He gives you an up-to-date map (your puzzle input) of his starting position (S), garden plots (.), and rocks (#). For example:
...........
.....###.#.
.###.##..#.
..#.#...#..
....#.#....
.##..S####.
.##..#...#.
.......##..
.##.#.####.
.##..##.##.
...........
The Elf starts at the starting position (S) which also counts as a garden plot. Then, he can take one step north, south, east, or west, but only onto tiles that are garden plots. This would allow him to reach any of the tiles marked O:
...........
.....###.#.
.###.##..#.
..#.#...#..
....#O#....
.##.OS####.
.##..#...#.
.......##..
.##.#.####.
.##..##.##.
...........
Then, he takes a second step. Since at this point he could be at either tile marked O, his second step would allow him to reach any garden plot that is one step north, south, east, or west of any tile that he could have reached after the first step:
...........
.....###.#.
.###.##..#.
..#.#O..#..
....#.#....
.##O.O####.
.##.O#...#.
.......##..
.##.#.####.
.##..##.##.
...........
After two steps, he could be at any of the tiles marked O above, including the starting position (either by going north-then-south or by going west-then-east).
A single third step leads to even more possibilities:
...........
.....###.#.
.###.##..#.
..#.#.O.#..
...O#O#....
.##.OS####.
.##O.#...#.
....O..##..
.##.#.####.
.##..##.##.
...........
He will continue like this until his steps for the day have been exhausted. After a total of 6 steps, he could reach any of the garden plots marked O:
...........
.....###.#.
.###.##.O#.
.O#O#O.O#..
O.O.#.#.O..
.##O.O####.
.##.O#O..#.
.O.O.O.##..
.##.#.####.
.##O.##.##.
...........
In this example, if the Elf’s goal was to get exactly 6 more steps today, he could use them to reach any of 16 garden plots.
However, the Elf actually needs to get 64 steps today, and the map he’s handed you is much larger than the example map.
Starting from the garden plot marked S on your map, how many garden plots could the Elf reach in exactly 64 steps?
2.1.2 Solution
The first part is rather straight forward. We use modulo arithmetic as a preparation for part 2.
walk_garden <- function(garden, n_steps) {
dirs <- rbind(
"^" = c(-1L, 0L),
">" = c(0L, 1L),
"v" = c(1L, 0L),
"<" = c(0L, -1L)
)
pos <- which(garden == "S", arr.ind = TRUE)
s <- 1L
while (s <= n_steps) {
new_pos <- map(1:4, ~ t(t(pos) + dirs[.x, ])) %>%
do.call(rbind, .)
wrap <- sweep(new_pos, 2L, dim(garden), \(m, d) (m - 1L) %% d + 1L)
new_pos <- new_pos[garden[wrap] != "#", , drop = FALSE]
pos <- new_pos[!duplicated(new_pos), , drop = FALSE]
s <- s + 1L
}
nrow(pos)
}
walk_garden(puzzle_data, 64)
## [1] 3764
2.2 Part 2
2.2.1 Description
— Part Two —
The Elf seems confused by your answer until he realizes his mistake: he was reading from a list of his favorite numbers that are both perfect squares and perfect cubes, not his step counter.
The actual number of steps he needs to get today is exactly 26501365.
He also points out that the garden plots and rocks are set up so that the map repeats infinitely in every direction.
So, if you were to look one additional map-width or map-height out from the edge of the example map above, you would find that it keeps repeating:
.................................
.....###.#......###.#......###.#.
.###.##..#..###.##..#..###.##..#.
..#.#...#....#.#...#....#.#...#..
....#.#........#.#........#.#....
.##...####..##...####..##...####.
.##..#...#..##..#...#..##..#...#.
.......##.........##.........##..
.##.#.####..##.#.####..##.#.####.
.##..##.##..##..##.##..##..##.##.
.................................
.................................
.....###.#......###.#......###.#.
.###.##..#..###.##..#..###.##..#.
..#.#...#....#.#...#....#.#...#..
....#.#........#.#........#.#....
.##...####..##..S####..##...####.
.##..#...#..##..#...#..##..#...#.
.......##.........##.........##..
.##.#.####..##.#.####..##.#.####.
.##..##.##..##..##.##..##..##.##.
.................................
.................................
.....###.#......###.#......###.#.
.###.##..#..###.##..#..###.##..#.
..#.#...#....#.#...#....#.#...#..
....#.#........#.#........#.#....
.##...####..##...####..##...####.
.##..#...#..##..#...#..##..#...#.
.......##.........##.........##..
.##.#.####..##.#.####..##.#.####.
.##..##.##..##..##.##..##..##.##.
.................................
This is just a tiny three-map-by-three-map slice of the inexplicably-infinite farm layout; garden plots and rocks repeat as far as you can see. The Elf still starts on the one middle tile marked S, though - every other repeated S is replaced with a normal garden plot (.).
Here are the number of reachable garden plots in this new infinite version of the example map for different numbers of steps:
-
In exactly
6steps, he can still reach16garden plots. -
In exactly
10steps, he can reach any of50garden plots. -
In exactly
50steps, he can reach1594garden plots. -
In exactly
100steps, he can reach6536garden plots. -
In exactly
500steps, he can reach167004garden plots. -
In exactly
1000steps, he can reach668697garden plots. -
In exactly
5000steps, he can reach16733044garden plots.
However, the step count the Elf needs is much larger! Starting from the garden plot marked S on your infinite map, how many garden plots could the Elf reach in exactly 26501365 steps?
2.2.2 Solution
For the second part we observe that
Periodic world. The map garden is a finite \(W \times H\) tile that repeats infinitely in all directions. Walkability at absolute coordinates \((x,y)\) is decided by wrapping back to the base tile via \(((y \bmod H), (x \bmod W))\).
Residue class matters (not touching the first border). We do not rely on when a wavefront “touches the first tile’s border”. What matters is the residue class of the step count modulo the tile’s stride \(L\). For a square tile with odd side length (e.g., \(131 \times 131\)) and 4‑neighborhood moves, using \(L=W=H\) works well. The start in the geometric center gives especially clean behavior for one residue class, but the method holds for every residue class.
Stride and residue subsequences. Fix a residue \(r \in \{0,\dots,L-1\}\) and consider the subsequence of step counts \[n \;=\; r + L \cdot k \quad (k = 0,1,2,\dots).\] Along this subsequence, the modulo‑tile “phase” (i.e., how moves align with the periodic pattern) remains constant.
Quadratic law per residue class. In 2D, the number of cells reachable after exactly \(n\) steps grows quadratically in the “shell index” \(k\). Consequently, for each residue \(r\) there exists an exact quadratic polynomial \[f_r(k) \;=\; A_r\,k^2 + B_r\,k + C_r,\] such that \(\#\text{reachable}(n) = f_r\big((n-r)/L\big)\) whenever \(n \equiv r \pmod L\). > Practically: the leading term is quadratic (area‑like growth); lower‑order terms encode periodic effects and become constants once the residue class is fixed.
Determine coefficients with 3 supports. To find \(A_r,B_r,C_r\), simulate three step counts within the same residue class: \[n_0 = r,\quad n_1 = r + L,\quad n_2 = r + 2L.\] Collect the exact counts \(f_r(0), f_r(1), f_r(2)\) and perform a quadratic fit (equivalently, check that the second finite difference \(f(k+2) - 2f(k+1) + f(k)\) is constant). If a residue class stabilizes a bit later, just use more supports \(k=0\ldots m\) and fit degree 2 once the second difference is constant.
Evaluate for any target \(n\). For a given \(n\), compute \(r = n \bmod L\) and \(k = (n - r)/L\). Evaluate \(f_r(k)\) from the fitted polynomial and obtain the exact number of cells reachable after exactly \(n\) steps—without simulating up to \(n\).
walk_garden_infinite <- function(garden, n_steps, supports = 0:2) {
L <- LCM(nrow(garden), ncol(garden))
if (any(dim(garden) %% 2L == 0L)) {
L <- LCM(L, 2L)
}
r <- n_steps %% L
k <- (n_steps - r) / L
collect_y <- function(k_supports) {
x <- r + k_supports * L
y <- map_int(x, ~ walk_garden(garden, .x))
list(k = as.integer(k_supports), x = as.integer(x), y = as.integer64(y))
}
is_stable <- function(k, y) {
if (length(k) < 3L) {
return(FALSE)
}
# Ensure k are consecutive integers (0,1,2,... or any contiguous range)
kd <- sort(k) %>%
diff()
if (!all(kd == 1L)) {
return(FALSE)
}
y_num <- as.numeric(y)
d1 <- diff(y_num)
d2 <- diff(d1)
length(d2) >= 1L && all(abs(d2 - d2[1L]) < 0.5)
}
k_supports <- as.integer(supports) %>%
unique() %>%
sort()
cur <- collect_y(k_supports)
extra_used <- 0L
while (!is_stable(cur$k, cur$y)) {
if (length(k_supports) == 0L) {
next_k <- 0L
} else {
next_k <- max(k_supports) + 1L
}
k_supports <- c(k_supports, next_k)
cur <- collect_y(k_supports)
extra_used <- extra_used + 1L
}
y <- tail(cur$y, 3L)
x <- r + k_supports * L
a <- (y[3L] - 2 * y[2L] + y[1L]) / 2
b <- (y[2L] - y[1L]) - a
c <- y[1L]
as.integer64(a * k ^ 2L + b * k + c)
}
walk_garden_infinite(puzzle_data, 26501365)
## integer64
## [1] 622926941971282