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)

I think the way to go with this is the distance between beacons? That will make a big matrix. Maybe the angle between beacons will have to be involved too.

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"),
                      relationship = "many-to-many") %>%
    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))

dist_order = bcn_cross %>% arrange(dd)

distinct_distance <- bcn_cross %>% distinct(dd)

beacon_table <- table(bcn_cross$dd) |> 
    as_tibble(.name_repair="unique") |> 
    filter(n>1)
New names:
• `` -> `...1`

Paste part two here