01: Historian Hysteria

library(mistlecode)

options(scipen = 999)
dt <- 
  'input.txt' |>
  readr::read_delim(delim = '   ', col_names = FALSE, show_col_types = FALSE)
Registered S3 methods overwritten by 'bit64':
  method               from  
  as.double.integer64  cheapr
  as.integer.integer64 cheapr

Part 1

This was nice and quick. I’m pretty happy with my global rank 518.

dt1 <- dt$X1 |> sort()
dt2 <- dt$X2 |> sort()

abs(dt1 - dt2) |> sum()
[1] 1151792

Part 2

This took way too long for me. I was really struggling with the counting portion for some reason and it took a long time for me to internalize that x %in% y and which don’t actually count things. Might need to add that to mistlecode

dt |>
  dplyr::mutate('count' = purrr::map_int(.data$X1, \(x) {
    which(.data$X2 == x) |> length()
  })) |>
  dplyr::mutate('count' = .data$X1 * .data$count) |>
  dplyr::pull(.data$count) |>
  sum()
[1] 21790168