Skip to content

Instantly share code, notes, and snippets.

@mathesong
Last active December 1, 2024 20:01
Show Gist options
  • Save mathesong/ecb55978966c9b02d50d3aa59327ce34 to your computer and use it in GitHub Desktop.
Save mathesong/ecb55978966c9b02d50d3aa59327ce34 to your computer and use it in GitHub Desktop.
Advent_of_Code_2024_Solutions
library(tidyverse)
input <- read_delim("input.txt", col_names = c("x", "y"), delim = " ")
#### Part 1 ####
input_ordered <- bind_cols(
x = input$x[order(input$x)],
y = input$y[order(input$y)]) %>%
mutate(dist = abs(x-y))
sum(input_ordered$dist)
#### Part 2 ####
n_ymatches <- function(xval, y) {
sum(y == xval)
}
input_x <- tibble(
x = input_ordered$x
) %>%
mutate(n_ymatch = map_dbl(x, ~n_ymatches(.x, input_ordered$y)))
sum(input_x$x * input_x$n_ymatch)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment