Created
December 12, 2025 23:05
-
-
Save ursulams/22608ddfb5acb8c4ff6614b3aa945585 to your computer and use it in GitHub Desktop.
2025 advent of code day 5
This file contains hidden or 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
| # day 5 2025 | |
| # first star | |
| options(scipen = 999) | |
| input <- lapply(strsplit(readLines("input.txt"), "-"), as.numeric) | |
| fresh_ranges <- Filter(function(x) {length(x) == 2}, input) | |
| ids <- Filter(function(x) {length(x) == 1}, input) | |
| length(unique(unlist(mapply(function(id, range){which(id >= range[1] & id <= range[2])}, list(ids), fresh_ranges)))) | |
| # second star | |
| # sort ranges, if next lower and upper bounds > range, replace original | |
| # if not then compare just upper bound | |
| fresh_ranges <- lapply(fresh_ranges, sort, decreasing = FALSE) | |
| compare <- function(x, i){ | |
| upper <- 0 | |
| fresh_ids <- 0 | |
| if (x[[i]][2] > upper) { | |
| if (x[[i]][1] > upper) { | |
| count <- diff(x[[i]]) + 1 | |
| } else { | |
| count <- diff(c(upper, x[[i]][2])) | |
| } | |
| fresh_ids <- fresh_ids + count | |
| upper <- x[[i]][2] | |
| } | |
| return(ids) | |
| } | |
| sapply(seq_along(fresh_ranges), compare, x = fresh_ranges)[1] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment