Skip to content

Instantly share code, notes, and snippets.

View ursulams's full-sized avatar
💾
regressing

Ursula Kaczmarek ursulams

💾
regressing
View GitHub Profile
@ursulams
ursulams / day_5.R
Created December 12, 2025 23:05
2025 advent of code day 5
# 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
@ursulams
ursulams / day_4.R
Created December 12, 2025 23:04
2025 advent of code day 4
# 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))]
@ursulams
ursulams / day_3.R
Last active December 8, 2025 02:52
2025 advent of code day 3
# 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))
@ursulams
ursulams / day_2.R
Created December 5, 2025 17:00
2025 advent of code day 2
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)
@ursulams
ursulams / day_1.R
Created December 3, 2025 04:00
2025 advent of code day 1
# 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))
@ursulams
ursulams / day_7.R
Last active December 21, 2024 05:44
2024 advent of code day 7
# 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)
@ursulams
ursulams / day_6.R
Created December 12, 2024 03:41
2024 advent of code day 6
# 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){
@ursulams
ursulams / day_5.R
Created December 10, 2024 04:35
2024 advent of code day 5
# 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
@ursulams
ursulams / day_4.R
Created December 5, 2024 07:04
2024 advent of code day 4
# 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)
@ursulams
ursulams / day_3.R
Created December 4, 2024 06:33
2024 advent of code day 3
# 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)