Advent of Code 2024, Day 1

advent of code
Author

Michael Luu

Published

December 1, 2024

The following is the solution for AoC 2024 Day 1.

library(tidyverse)
library(here)

data <- read_delim('input.txt', delim = '   ', col_names = FALSE)

Part 1

out <- sum(abs(sort(data$X2) -  sort(data$X1)))

out
[1] 3246517

Part 2

out <- data |> 
  count(X2) |> 
  left_join(x = data, y = _, by = join_by(X1 == X2)) |> 
  mutate(n = ifelse(is.na(n), 0, n)) |> 
  mutate(sim_score = X1 * n) |> 
  summarise(total_sim_score = sum(sim_score)) |> 
  pull(total_sim_score)

out
[1] 29379307

Reuse

Citation

BibTeX citation:
@online{luu2024,
  author = {Luu, Michael},
  title = {Advent of {Code} 2024, {Day} 1},
  date = {2024-12-01},
  langid = {en}
}
For attribution, please cite this work as:
Luu, Michael. 2024. “Advent of Code 2024, Day 1.” December 1, 2024.