Task 10

Thorn Thaler - <

2025-10-09

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)
  }
  res <- text_block %>% 
   str_extract_all("-?\\d+") %>% 
    do.call(rbind, .) %>% 
    set_colnames(c("x0", "y0", "dx", "dy"))
  storage.mode(res) <- "integer"
  class(res) <- "elve_msg"
  res
}

puzzle_data <- local({
  GET(paste0(base_url, "/input"),
      session_cookie) %>% 
    content(encoding = "UTF-8") %>% 
    parse_puzzle_data()
})

2 Puzzle Day 10

2.1 Part 1

2.1.1 Description

— Day 10: The Stars Align —

It’s no use; your navigation system simply isn’t capable of providing walking directions in the arctic circle, and certainly not in 1018.

The Elves suggest an alternative. In times like these, North Pole rescue operations will arrange points of light in the sky to guide missing Elves back to base. Unfortunately, the message is easy to miss: the points move slowly enough that it takes hours to align them, but have so much momentum that they only stay aligned for a second. If you blink at the wrong time, it might be hours before another message appears.

You can see these points of light floating in the distance, and record their position in the sky and their velocity, the relative change in position per second (your puzzle input). The coordinates are all given from your perspective; given enough time, those positions and velocities will move the points into a cohesive message!

Rather than wait, you decide to fast-forward the process and calculate what the points will eventually spell.

For example, suppose you note the following points:

position=< 9,  1> velocity=< 0,  2>
position=< 7,  0> velocity=<-1,  0>
position=< 3, -2> velocity=<-1,  1>
position=< 6, 10> velocity=<-2, -1>
position=< 2, -4> velocity=< 2,  2>
position=<-6, 10> velocity=< 2, -2>
position=< 1,  8> velocity=< 1, -1>
position=< 1,  7> velocity=< 1,  0>
position=<-3, 11> velocity=< 1, -2>
position=< 7,  6> velocity=<-1, -1>
position=<-2,  3> velocity=< 1,  0>
position=<-4,  3> velocity=< 2,  0>
position=<10, -3> velocity=<-1,  1>
position=< 5, 11> velocity=< 1, -2>
position=< 4,  7> velocity=< 0, -1>
position=< 8, -2> velocity=< 0,  1>
position=<15,  0> velocity=<-2,  0>
position=< 1,  6> velocity=< 1,  0>
position=< 8,  9> velocity=< 0, -1>
position=< 3,  3> velocity=<-1,  1>
position=< 0,  5> velocity=< 0, -1>
position=<-2,  2> velocity=< 2,  0>
position=< 5, -2> velocity=< 1,  2>
position=< 1,  4> velocity=< 2,  1>
position=<-2,  7> velocity=< 2, -2>
position=< 3,  6> velocity=<-1, -1>
position=< 5,  0> velocity=< 1,  0>
position=<-6,  0> velocity=< 2,  0>
position=< 5,  9> velocity=< 1, -2>
position=<14,  7> velocity=<-2,  0>
position=<-3,  6> velocity=< 2, -1>

Each line represents one point. Positions are given as <X, Y> pairs: X represents how far left (negative) or right (positive) the point appears, while Y represents how far up (negative) or down (positive) the point appears.

At 0 seconds, each point has the position given. Each second, each point’s velocity is added to its position. So, a point with velocity <1, -2> is moving to the right, but is moving upward twice as quickly. If this point’s initial position were <3, 9>, after 3 seconds, its position would become <6, 3>.

Over time, the points listed above would move like this:

Initially:
........#.............
................#.....
.........#.#..#.......
......................
#..........#.#.......#
...............#......
....#.................
..#.#....#............
.......#..............
......#...............
...#...#.#...#........
....#..#..#.........#.
.......#..............
...........#..#.......
#...........#.........
...#.......#..........

After 1 second:
......................
......................
..........#....#......
........#.....#.......
..#.........#......#..
......................
......#...............
....##.........#......
......#.#.............
.....##.##..#.........
........#.#...........
........#...#.....#...
..#...........#.......
....#.....#.#.........
......................
......................

After 2 seconds:
......................
......................
......................
..............#.......
....#..#...####..#....
......................
........#....#........
......#.#.............
.......#...#..........
.......#..#..#.#......
....#....#.#..........
.....#...#...##.#.....
........#.............
......................
......................
......................

After 3 seconds:
......................
......................
......................
......................
......#...#..###......
......#...#...#.......
......#...#...#.......
......#####...#.......
......#...#...#.......
......#...#...#.......
......#...#...#.......
......#...#..###......
......................
......................
......................
......................

After 4 seconds:
......................
......................
......................
............#.........
........##...#.#......
......#.....#..#......
.....#..##.##.#.......
.......##.#....#......
...........#....#.....
..............#.......
....#......#...#......
.....#.....##.........
...............#......
...............#......
......................
......................

After 3 seconds, the message appeared briefly: HI. Of course, your message will be much longer and will take many more seconds to appear.

What message will eventually appear in the sky?

2.1.2 Solution

The idea is to calculate the index where the variability of the positions is minimal (pixels arranged in letters scatter around the same place are thus closer together).

Once we found the index, we can plot it

move_stars <- function(stars, n) {
  stars[, 1:2] + n * stars[, 3:4]
}

find_min_entropy <- function(stars = puzzle_data, n = 1e6L) {
  entropy <- function(n) {
    pos <- move_stars(stars, n)
    vars <- apply(pos, 2, var) 
    sum(vars)
  }
  vapply(seq_len(n), entropy, numeric(1L)) %>% 
    which.min()
}

min_entropy <- find_min_entropy(puzzle_data)
as.matrix.elve_msg <- function(x, n = 0L, border = 6L, fill = c(0L, 1L)) {
  coords <- move_stars(x, n)
  ## need to flip because x coordinates correspond to columns
  coords <- coords[, 2:1]
  min_x <- apply(coords, 2, min)
  cnt <- sweep(coords, 2, min_x, "-") + 1
  maxs <- apply(cnt, 2, max)
  mat <- matrix(fill[1L], maxs[1L] + border, maxs[2L] + border)
  mat[cnt + border / 2] <- fill[2L]
  mat
}

plot.elve_msg <- function(x, n = 0L) {
  mat <- as.matrix(x, n) %>% 
    t()
  par(mar = rep(0, 4L))
  image(mat[, ncol(mat):1], col = c("transparent", "black"), axes = FALSE, asp = 1)
}

print.elve_msg <- function(x, n = 0L) {
  mat <- as.matrix(x, n, border = 4L, fill = c(".", "#"))
  apply(mat, 1, paste, collapse = "") %>% 
    paste(collapse = "\n") %>% 
    cat()
}
print(puzzle_data, min_entropy)
..................................................................
..................................................................
..######..#####...#....#..#....#..#.......######....##.....####...
..#.......#....#..#....#..#...#...#.......#........#..#...#....#..
..#.......#....#..#....#..#..#....#.......#.......#....#..#.......
..#.......#....#..#....#..#.#.....#.......#.......#....#..#.......
..#####...#####...######..##......#.......#####...#....#..#.......
..#.......#....#..#....#..##......#.......#.......######..#..###..
..#.......#....#..#....#..#.#.....#.......#.......#....#..#....#..
..#.......#....#..#....#..#..#....#.......#.......#....#..#....#..
..#.......#....#..#....#..#...#...#.......#.......#....#..#...##..
..#.......#####...#....#..#....#..######..######..#....#...###.#..
..................................................................
..................................................................
plot(puzzle_data, min_entropy)

2.2 Part 2

2.2.1 Description

— Part Two —

Good thing you didn’t have to wait, because that would have taken a long time - much longer than the 3 seconds in the example above.

Impressed by your sub-hour communication capabilities, the Elves are curious: exactly how many seconds would they have needed to wait for that message to appear?

2.2.2 Solution

The seconds are exactly the minimum entropy we calculated earlier.

min_entropy
## [1] 10009