Day 5

Advent of Code: Worked Solutions

Puzzle Source
Puzzle Date

December 5, 2025

Setup

Import libraries:

library(tidyverse)
library(unglue)
library(sets)

Disable scientific notation to display all digits of large numbers:

options(scipen = 999)

Read text input from file:

input <- read_lines("../input/day05.txt", skip_empty_rows = TRUE)

Separate text input into (1) ranges of “fresh” IDs, and (2) a single vector of “available” IDs:

fresh <- input |>
  unglue_data("{start}-{end}", convert = TRUE) |> 
  drop_na()

available <- input |>
  keep(\(x) str_detect(x, "^\\d+$")) |> 
  as.numeric()

Part 1

Count how many of the “available” IDs are in any “fresh” ID range:

available |> 
  enframe() |> 
  cross_join(fresh) |> 
  filter(between(value, start, end)) |> 
  distinct(value) |> 
  nrow()

Part 2

Convert each pair of start/end “fresh” IDs into intervals, take their union, and count the size of the result:

fresh |> 
  with(map2(start, end, \(a, b) interval(a, b, bounds = "closed"))) |> 
  reduce(interval_union) |> 
  as.list() |> 
  map_dbl(\(x) interval_measure(x) + 1) |> 
  sum()