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_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)
@ursulams
ursulams / day_2.R
Created December 3, 2024 02:40
2024 advent of code day 2
input <- apply(read.delim("input.txt", sep = "\n", header = FALSE), 1, function(x) unlist(type.convert(strsplit(x, " "))))
# first star
safe <- function(x){
(all(x == cummax(x)) | all(x == cummin(x))) & # returns true if sequence is monotonic
all(abs(diff(x)) >= 1 & abs(diff(x)) <= 3)} # returns true if max diff between 1:3
safes <- sum(sapply(input, safe))
# second star
@ursulams
ursulams / day_1.R
Created December 1, 2024 07:43
2024 advent of code day 1
# first star
input <- apply(read.delim("input.txt", sep = "", header = FALSE), 2, sort, decreasing = FALSE, simplify = FALSE)
diffs <- mapply(function(x,y){abs(x-y)}, input[["V1"]], input[["V2"]])
sum(diffs)
# second star
mat <- matrix(input[["V1"]][match(input[["V2"]], input[["V1"]])])
sum(mat, na.rm = TRUE)
@ursulams
ursulams / mario.c
Last active March 11, 2024 13:11
cs50 mario.c
# include <cs50.h>
# include <stdio.h>
void print_row(int spaces, int bricks);
int main(void)
{
int n;
do
{
@ursulams
ursulams / day_15.R
Last active December 21, 2023 03:31
2023 advent of code day 15
input <- data.frame("label" = t(read.table("input.txt", sep = ",")))
input$row <- seq_along(input[,1])
hasher <- function(x){
current <- 0L
for(i in 1:length(x)){
current <- ((current + utf8ToInt(x[i])) * 17L) %% 256L
}
return(current)
}
@ursulams
ursulams / day_8.R
Created December 14, 2023 19:27
2023 advent of code day 8
# first star
network <- read.table("nodes.txt", header = TRUE, sep = "\n")
edges <- as.numeric(factor(unlist(strsplit(colnames(network)[1], "")))) + 1
network$node <- sapply(strsplit(gsub("[^A-Z]", " ", network[, 1]), "\\s+"), "[", 1)
network$left_node <- sapply(strsplit(gsub("[^A-Z]", " ", network[, 1]), "\\s+"), "[", 2)
network$right_node <- sapply(strsplit(gsub("[^A-Z]", " ", network[, 1]), "\\s+"), "[", 3)
network <- network[, c(2:4)]
follow <- function(node, end_node = "ZZZ") {