02: Corruption Checksum

library(tidyverse)
── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
✔ dplyr     1.1.2     ✔ readr     2.1.4
✔ forcats   1.0.0     ✔ stringr   1.5.0
✔ ggplot2   3.4.2     ✔ tibble    3.2.1
✔ lubridate 1.9.2     ✔ tidyr     1.3.0
✔ purrr     1.0.1     
── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
✖ dplyr::filter() masks stats::filter()
✖ dplyr::lag()    masks stats::lag()
ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
dt <- data.table::fread("input.txt")

Part 1

dt %>%
  rowwise() %>%
  mutate(
    "min" = min(across()),
    "max" = max(across())
  ) %>%
  mutate("diff" = max - min) %>%
  pull(diff) %>%
  sum()
Warning: There was 1 warning in `mutate()`.
ℹ In argument: `min = min(across())`.
ℹ In row 1.
Caused by warning:
! Using `across()` without supplying `.cols` was deprecated in dplyr 1.1.0.
ℹ Please supply `.cols` instead.
[1] 36174

Part 2

apply(dt, 1, function(x) {
  expand.grid(x, x) %>%
    filter(Var1 != Var2) %>%
    mutate("valid" = ifelse(Var1 %% Var2 == 0, TRUE, FALSE)) %>%
    filter(valid) %>%
    mutate("val" = Var1 / Var2) %>%
    pull(val)
}) %>%
  sum()
[1] 244