Created
December 5, 2025 17:00
-
-
Save ursulams/a02911b0472711ecab76ebb581478c56 to your computer and use it in GitHub Desktop.
2025 advent of code day 2
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
| input <- sapply(read.csv("input.txt", header = FALSE), strsplit, "-") | |
| input <- lapply(input, function(x){as.character(x[1]:x[2])}) | |
| get_invalids <- function(x, new = FALSE){ | |
| ranges <- sapply(x, function(x){subset(x, nchar(x) %% 2 == 0)}) # repeating numbers are of even length | |
| invalids <- sapply(ranges, function(x) {as.numeric(grep("^(\\d+)\\1$", x, value = TRUE))}) | |
| if (new == TRUE){ | |
| invalids <- sapply(x, function(i) {as.numeric(grep("^(\\d+)(\\1)+$", i, value = TRUE))}) | |
| } | |
| return(invalids) | |
| } | |
| invalids <- lapply(sapply(input, get_invalids), unlist) | |
| do.call(sum, invalids) | |
| # second star | |
| new_invalids <- lapply(sapply(input, function (x) {get_invalids(x, new = TRUE)}), unlist) | |
| do.call(sum, new_invalids) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment