Skip to content

Instantly share code, notes, and snippets.

@ursulams
Created December 13, 2023 17:27
Show Gist options
  • Save ursulams/718608994a533e27f7c916eb0781c36c to your computer and use it in GitHub Desktop.
Save ursulams/718608994a533e27f7c916eb0781c36c to your computer and use it in GitHub Desktop.
2023 advent of code day 9
# 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)
}
sum(sapply(input, predict))
# second star
sum(sapply(input, function(x){predict(rev(x))}))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment