helper1_day4 <- function (vec, strs_to_drop) {
vec %>%
stringr::str_split(" ") %>%
map(~.x %>%
setdiff(strs_to_drop) %>%
as.numeric() %>%
.[!is.na(.)]
)
}
input_df = readr::read_delim(
"input.txt",
delim = "|",
col_names = FALSE
)
my_counts <- input_df %>%
transmute(
card = 1:n(),
winning_numbers = helper1_day4(X1, strs_to_drop = c("Card", "")),
my_numbers = helper1_day4(X2, strs_to_drop = "")
) %>%
rowwise() %>%
mutate(count = sum(my_numbers %in% winning_numbers)) %>%
ungroup()Day 4 ⋆⋆
Puzzle
Solution
Preprocessing
Part One
answer1 <- sum(ifelse(my_counts$count > 0, 2^(my_counts$count - 1), 0))
answer1[1] 21821
Part Two
I find tasks like this – “bespoke” rowwise data frame manipulation – to be easier with for loops. Not pretty, but it gets the job done:
tmp1_pt2 <- my_counts %>%
mutate(copies = 1)
for (card in 1:nrow(tmp1_pt2)) {
this_count <- pull(tmp1_pt2[card,], count)
if (this_count > 0) {
tmp1_pt2$copies[(card + 1):(card + this_count)] %<>%
`+`(tmp1_pt2$copies[card]) ## Syntactic sugar to avoid repeating a long object name
}
}
answer2 <- sum(tmp1_pt2$copies)
answer2[1] 5539496