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) |
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 <- 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 |
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 <- 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) |
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
# include <cs50.h> | |
# include <stdio.h> | |
void print_row(int spaces, int bricks); | |
int main(void) | |
{ | |
int n; | |
do | |
{ |
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 <- 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) | |
} |
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 | |
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") { |
NewerOlder