Task 18

Thorn Thaler - <

2026-01-27

1 Setup

1.1 Libraries

library(httr)
library(xml2)
library(magrittr)
library(dplyr)
library(purrr)
library(stringr)
library(ravetools)
library(igraph)

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_split(",") %>% 
    do.call(rbind, .) %>% 
    set_colnames(letters[24:26])
  storage.mode(res) <- "integer"
  res
}

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

2 Puzzle Day 18

2.1 Part 1

2.1.1 Description

— Day 18: Boiling Boulders —

You and the elephants finally reach fresh air. You’ve emerged near the base of a large volcano that seems to be actively erupting! Fortunately, the lava seems to be flowing away from you and toward the ocean.

Bits of lava are still being ejected toward you, so you’re sheltering in the cavern exit a little longer. Outside the cave, you can see the lava landing in a pond and hear it loudly hissing as it solidifies.

Depending on the specific compounds in the lava and speed at which it cools, it might be forming obsidian! The cooling rate should be based on the surface area of the lava droplets, so you take a quick scan of a droplet as it flies past you (your puzzle input).

Because of how quickly the lava is moving, the scan isn’t very good; its resolution is quite low and, as a result, it approximates the shape of the lava droplet with 1x1x1 cubes on a 3D grid, each given as its x,y,z position.

To approximate the surface area, count the number of sides of each cube that are not immediately connected to another cube. So, if your scan were only two adjacent cubes like 1,1,1 and 2,1,1, each cube would have a single side covered and five sides exposed, a total surface area of 10 sides.

Here’s a larger example:

2,2,2
1,2,2
3,2,2
2,1,2
2,3,2
2,2,1
2,2,3
2,2,4
2,2,6
1,2,5
3,2,5
2,1,5
2,3,5

In the above example, after counting up all the sides that aren’t connected to another cube, the total surface area is 64.

What is the surface area of your scanned lava droplet?

2.1.2 Solution

We can solve the first part by convolution. First we need to add +1 to switch from 0 based indexing to 1 based indexing. Then, we need to add a padding layer, to be able to count neighbors from the outer rim.

count_surface_tiles <- function(coords) {
  coords <- coords + 1L + 1L # +1: 1-based, +1: add left/bottom/front outer rim
  max_d <- apply(coords, 2L, max) + 1L # +1: add right/top/back outer rim
  grid <- array(TRUE, max_d)
  grid[coords] <- FALSE
  kernel <- array(0L, rep(3L, 3L))
  kernel[rbind(
    c(2L, 1L, 2L), # left
    c(2L, 3L, 2L), # right
    c(1L, 2L, 2L), # top
    c(3L, 2L, 2L), # bottom
    c(2L, 2L, 1L), # down
    c(2L, 2L, 3L)  # up
  )] <- 1L
  res <- convolve_volume(grid, kernel)[!grid]
  sum(res)
}
count_surface_tiles(puzzle_data)
## [1] 3448

2.2 Part 2

2.2.1 Description

— Part Two —

Something seems off about your calculation. The cooling rate depends on exterior surface area, but your calculation also included the surface area of air pockets trapped in the lava droplet.

Instead, consider only cube sides that could be reached by the water and steam as the lava droplet tumbles into the pond. The steam will expand to reach as much as possible, completely displacing any air on the outside of the lava droplet but never expanding diagonally.

In the larger example above, exactly one cube of air is trapped within the lava droplet (at 2,2,5), so the exterior surface area of the lava droplet is 58.

What is the exterior surface area of your scanned lava droplet?

2.2.2 Solution

For the second part we fall back to a graph. We first construct the cube with the appropriate padding. Then, we label all neighbors of the solid blocks as surface. Then, we look at the induced subgraph, where we remove all solid nodes. This will split the original cube into components. For each component we check whether there is at least one padding node, which indicates that the component has a connection to the exterior.

With this classification, we subset all surface nodes, which are also on the outside and count the number of solid neighbors per surface node and sum these figures, which represents the number of surfaces.

flag_surface_nodes <- function(G) {
  ttype <- as.character(V(G)$type)
  allowed <- ttype != "solid"
  H <- induced_subgraph(G, vids = V(G)[allowed])
  ttype_H <- as.character(V(H)$type)
  comp <- components(H)
  has_pad_by_comp <- tapply(ttype_H == "padding", comp$membership, any)
  has_pad_H <- has_pad_by_comp[as.character(comp$membership)]
  orig_vids <- as.integer(V(G)[allowed])
  flag <- rep(FALSE, vcount(G))
  surface_mask_H <- ttype_H == "surface"
  flag[orig_vids[surface_mask_H]] <- has_pad_H[surface_mask_H]
  flag
}

construct_cube <- function(coords) {
  coords <- coords + 1L
  max_d <- apply(coords, 2L, max) + 2L
  G <- make_lattice(max_d)
  n <- vcount(G)
  i <- seq_len(n) - 1L
  x <- i %% max_d[1L]
  y <- (i %/% max_d[1L]) %% max_d[2L]
  z <- i %/% (max_d[1L] * max_d[2L])
  V(G)$name <- sprintf("%d/%d/%d", x, y, z)
  is_pad <- (x == 0L | x == (max_d[1L] - 1L) |
               y == 0L | y == (max_d[2L] - 1L) |
               z == 0L | z == (max_d[3L] - 1L))
  V(G)$type <- "empty"
  V(G)$type[is_pad]<- "padding"
  solids <- apply(coords, 1L, paste, collapse = "/") 
  V(G)[solids]$type <- "solid"
  nbs <- neighborhood(G, nodes = solids) %>% 
    reduce(union) %>% 
    setdiff(V(G)[solids])
  V(G)[nbs]$type <- "surface"
  V(G)$is_outer <- flag_surface_nodes(G)
  G
}

G <- construct_cube(puzzle_data)

sum(
  sapply(V(G)[V(G)$is_outer], function(v) {
    sum(V(G)[neighbors(G, v)]$type == "solid")
  })
)
## [1] 2052