Last active
December 1, 2024 20:01
-
-
Save mathesong/ecb55978966c9b02d50d3aa59327ce34 to your computer and use it in GitHub Desktop.
Advent_of_Code_2024_Solutions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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