Created
December 5, 2023 00:12
-
-
Save ursulams/e815c7d7cddd88433277609085a46de3 to your computer and use it in GitHub Desktop.
2023 advent of code day 4
This file contains 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 <- 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 | |
for (card in seq_along(total_cards[-1])) | |
{total_cards[card + seq_len(matches[card])] <- total_cards[card + seq_len(matches[card])] + total_cards[card]} | |
sum(total_cards) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment