I hereby claim:
- I am ursulams on github.
- I am uak211 (https://keybase.io/uak211) on keybase.
- I have a public key ASDQHcuJc098xx6kzwfn56jW-GjRUlJ-y6alxgqf5e4_Ewo
To claim this, I am signing this object:
license: mit |
license: mit |
license:mit |
I hereby claim:
To claim this, I am signing this object:
# first star | |
df <- read.table(text = puzzle_input, header = FALSE, sep = "\n") | |
df$digits <- as.numeric(paste0(sub(".*?(\\d).*", "\\1", df$V1), sub(".*(\\d+).*$", "\\1", df$V1))) | |
sum_cal_values <- sum(df$digits) | |
# second star | |
df$V2 <- sapply(df$V1, function(x){ | |
gsub("one", "o1e", | |
gsub("two", "t2o", | |
gsub("three", "thr3e", |
# part 1 | |
df <- read.table(text = puzzle_input, header = FALSE, sep = "\n") | |
df$game <- as.numeric(row(df)) | |
get_max <- function(string, pattern) { | |
cubes <- gsub("[^0-9,]", "", regmatches(string, gregexpr(pattern, string))) | |
sapply(cubes, function(x) max(as.numeric(unlist(strsplit(as.character(x), split = ","))))) | |
} | |
df$red_max <- get_max(df$V1, "\\d+\\s+red\\b") |
# first star | |
input <- read.table(file = "puzzle_input.txt", header = FALSE, sep = "\n") | |
matches <- mapply(function(x, y){sum(x%in%y)}, | |
strsplit(gsub(".*:\\s+(.+)\\s+\\|.*", "\\1", input$V1),"\\s+"), | |
strsplit(gsub(".*\\|\\s+(.+)", "\\1", input$V1), "\\s+")) | |
sum(sapply(matches[matches > 0], function(x){2^(x-1)})) | |
# second star | |
total_cards <- rep.int(1L, length(row(input))) # start cumulative total for each card |
# first star | |
hands <- data.frame(read.table("hands.txt", header = FALSE, sep = " ", col.names = c("cards", "bid"))) | |
hands$hand <- lapply(strsplit(hands$cards, ""), function(x){ | |
type.convert(gsub("A", 14, | |
gsub("K", 13, | |
gsub("Q", 12, | |
gsub("J", 11, | |
gsub("T", 10, x))))), as.is = TRUE) | |
}) |
# first star | |
puzzle_input <- "Time: 52 94 75 94 | |
Distance: 426 1374 1279 1216" | |
df <- data.frame("times" = type.convert(unlist(strsplit(sub("^.*:\\s+(.*)\\n.*", "\\1", puzzle_input), "\\s+")), as.is = TRUE), | |
"distances" = type.convert(unlist(strsplit(sub(".*Distance:\\s+(.*).*", "\\1", puzzle_input), "\\s+")), as.is = TRUE)) | |
# distance as function of (racing - button) * button with time as constant | |
# 0 = -1*(button^2) +- (racing * button) - distance | |
# get all possible values of b for any given distance | |
get_b <- function(t, d){ |
# first star | |
input <- type.convert(strsplit(readLines("puzzle_input.txt"), "\\s"), as.is = TRUE) | |
predict <- function(element) { | |
new_element <- element[length(element)] | |
while(length(element != 0) > 0) { | |
element <- diff(element) | |
new_element <- c(new_element, element[length(element)]) | |
} | |
sum(new_element) | |
} |