2021 Day 19

Author

Nathan Moore

— Day 19: Beacon Scanner —

As your probe drifted down through this area, it released an assortment of beacons and scanners into the water. It’s difficult to navigate in the pitch black open waters of the ocean trench, but if you can build a map of the trench using data from the scanners, you should be able to safely reach the bottom.

Assemble the full map of beacons. How many beacons are there?

library(tidyverse)

my_file <- here::here("2021", "data-2021-19.txt")
x <- readLines(my_file)

Maybe write an explanation of the solution approach here

bcn = tibble(zz = x) %>%
    separate(zz, into = c("x", "y", "z"), sep = ",", remove = FALSE) %>%
    mutate(scn = ifelse(str_detect(zz, "scanner"),
                         parse_number(substr(zz, 5, 15)),
                         NA)) %>%
    fill(scn, .direction = "down") %>%
    filter(!is.na(z)) %>%
    select(-zz) %>%
    group_by(scn) %>%
    mutate(bb = row_number()) %>%
    mutate(across(c(x, y, z), as.numeric)) %>%
    ungroup()
Warning: Expected 3 pieces. Missing pieces filled with `NA` in 59 rows [1, 28, 29, 55,
56, 83, 84, 111, 112, 139, 140, 167, 168, 195, 196, 223, 224, 251, 252, 279,
...].
bcn_cross = full_join(bcn, bcn, by = c("scn"), suffix = c(".l", ".r")) %>%
    filter(bb.l < bb.r) %>%
    mutate(dd = round(sqrt((x.l - x.r)**2 + (y.l - y.r)**2 + (z.l - z.r)**2), 2))
Warning in full_join(bcn, bcn, by = c("scn"), suffix = c(".l", ".r")): Detected an unexpected many-to-many relationship between `x` and `y`.
ℹ Row 1 of `x` matches multiple rows in `y`.
ℹ Row 1 of `y` matches multiple rows in `x`.
ℹ If a many-to-many relationship is expected, set `relationship =
  "many-to-many"` to silence this warning.
dists = bcn_cross %>%
    arrange(dd)

bcn_cross %>% distinct(dd)
# A tibble: 7,368 × 1
      dd
   <dbl>
 1 1324.
 2 1910.
 3 1403.
 4 2012.
 5  144.
 6 1994.
 7 1070.
 8 2346.
 9 1164.
10 1432.
# ℹ 7,358 more rows
  # 6248

Paste part two here