Skip to content

Instantly share code, notes, and snippets.

@ursulams
Last active December 3, 2023 01:25
Show Gist options
  • Save ursulams/9e79aa2f478c83da14e78751139f03c2 to your computer and use it in GitHub Desktop.
Save ursulams/9e79aa2f478c83da14e78751139f03c2 to your computer and use it in GitHub Desktop.
2023 advent of code day 1
# 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",
gsub("four", "f4r",
gsub("five", "f5e",
gsub("six", "s6x",
gsub("seven", "se7n",
gsub("eight", "e8t",
gsub("nine", "n9e", x)))))))))
})
df$digits_V2 <- as.numeric(paste0(sub(".*?(\\d).*", "\\1", df$V2), sub(".*(\\d+).*$", "\\1", df$V2)))
sum_cal_values_V2 <- sum(df$digits_V2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment