Skip to content

Instantly share code, notes, and snippets.

@ursulams
Last active December 3, 2023 20:26
Show Gist options
  • Save ursulams/34f51c4120fad10e38c21891a47fb612 to your computer and use it in GitHub Desktop.
Save ursulams/34f51c4120fad10e38c21891a47fb612 to your computer and use it in GitHub Desktop.
2023 advent of code day 2
# 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")
df$green_max <- get_max(df$V1, "\\d+\\s+green\\b")
df$blue_max <- get_max(df$V1, "\\d+\\s+blue\\b")
sum(df[(df$red_max <= 12 & df$green_max <= 13 & df$blue_max <= 14), "game"])
# part 2
df$product <- df$red_max*df$green_max*df$blue_max
sum(df$product)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment