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 |
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 4 2025 | |
| # first star | |
| input <- do.call(rbind, strsplit(readLines("input.txt"),"")) | |
| # calculate distances from each matrix cell to all others | |
| # extract each cell's neighboring values for each element- max distance is one | |
| indices <- which(input == input, arr.ind = TRUE) | |
| d <- apply(as.matrix(dist(indices, "maximum", diag = TRUE)), 1, function(i) input[i == 1]) | |
| names(d) <- input | |
| d <- d[grep("@", names(d))] |
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
| # first star | |
| input <- read.fwf("input.txt", widths = rep(1L, 100L)) | |
| find_joltage <- function(x) { | |
| joltage <- ifelse(which.max(x) == length(x), as.integer(paste0(max(x[1:length(x) - 1]), max(x))), | |
| as.integer(paste0(max(x), max(x[(which.max(x) + 1):length(x)])))) | |
| return(joltage) | |
| } | |
| sum(apply(input, 1, find_joltage)) |
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) |
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
| # first star | |
| # swap out l/r for operators | |
| input <- apply(read.table("input.txt", header = FALSE), 1, | |
| function(x) gsub("L", "-", gsub("R", "+", x))) | |
| input <- sapply(input, function(x) eval(parse(text = x))) | |
| start <- 50 | |
| clicks <- cumsum(c(start,input)) |
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
| # first star | |
| input <- readLines("input.txt") | |
| bigints <- sapply(strsplit(input, "\\:"), as.numeric)[1,] | |
| ints <- sapply(strsplit(input, " "), as.numeric) | |
| ints <- lapply(ints, function(x) x[!is.na(x)]) | |
| reductor <- function(i){ | |
| result <- Reduce(function(x, y) c(x * y, x + y), i) | |
| return(result) |
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
| # first star | |
| input <- as.matrix(read.fwf("input.txt", widths = rep(1, 130), comment.char = "")) | |
| # pad to indicate exit | |
| #input <- rbind("E", cbind("E", input, "E"), "E") | |
| starting <- as.vector(which(input == "^", arr.ind = TRUE)[1,]) | |
| # function moves n,m position in matrix | |
| step <- function(direction, mode){ |
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
| # first star | |
| input <- readLines("input.txt") | |
| pages <- as.data.frame(t(sapply(strsplit(input[grep("\\|", input)], "\\|"), as.integer))) | |
| updates <- lapply(strsplit(input[grep(",", input)], ","), as.integer) | |
| get_order <- function(u){ | |
| ordered <- pages[(pages$V1 %in% u) & (pages$V2 %in% u), ] # which page pairs apply to update | |
| ordered$V1_idx <- sapply(ordered$V1, function(x){which(u == x)}) # get indices |
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
| # first star | |
| input <- as.matrix(read.fwf("input.txt", widths = rep(1, 140))) | |
| finder <- function(x) {x <- paste(x, collapse = "") | |
| lengths(regmatches(x, gregexpr("XMAS", x))) + | |
| lengths(regmatches(x, gregexpr("SAMX", x))) | |
| } | |
| # tally horizontal & vertical matches | |
| h_finds <- apply(input, 1, finder) |
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
| # first star | |
| input <- paste0(readLines("input.txt"), collapse = " ") # ingest as one big blob | |
| expression <- "mul\\([0-9]+,[0-9]+\\)" | |
| # returns parsed string with generic function to allow for string to expression conversion | |
| renamer <- function(x){ | |
| str2lang(gsub("mul", "prod", regmatches(x, gregexpr(expression, x)))) | |
| } | |
| multipliers <- renamer(input) |
NewerOlder