<- readr::read_delim(
records "input.txt",
delim = " ",
col_names = FALSE
)<- records %>%
records2 transmute(
hand_number = 1:n(),
cards = X1 %>%
::str_split("") %>%
stringrmap(as_tibble),
card_count = map(cards, ~count(.x, value)),
bid = X2
) <- function (input_df, card_ordering) {
helper1_day7 <- left_join(
to_return %>%
input_df unnest(cols = c(card_count)) %>%
summarise(
type = case_when(
any(n == 5) ~ "five of a kind",
any(n == 4) ~ "four of a kind",
any(n == 3) & any(n == 2) ~ "full house",
any(n == 3) ~ "three of a kind",
sum(n == 2) == 2 ~ "two pairs",
any(n == 2) ~ "one pair",
TRUE ~ "high card"
),.by = hand_number
),%>%
input_df select(hand_number, cards, bid),
by = "hand_number"
%>%
) group_split(type) %>%
setNames(., map_chr(., \(x) as.character(x$type[1]))) %>%
map_dfr(\(outer_tbl, name) bind_cols(
select(outer_tbl, -cards),
$cards %>%
outer_tblmap_dfr(\(inner_tbl) as_tibble(t(inner_tbl)))
%>%
)) mutate(
across(type, ~.x %>% factor(levels = c(
"five of a kind",
"four of a kind",
"full house",
"three of a kind",
"two pairs",
"one pair",
"high card"
))),across(c(V1, V2, V3, V4, V5), ~factor(.x, levels = card_ordering))
%>%
) arrange(type, V1, V2, V3, V4, V5) %>%
mutate(rank = n():1, winnings = bid * rank) %>%
pull(winnings) %>%
sum()
return(to_return)
}
Day 7 ⋆
Puzzle
Solution
Preprocessing
Part One
<- records2 %>%
answer1 helper1_day7(card_ordering = c("A", "K", "Q", "J", "T", "9", "8", "7", "6", "5", "4", "3", "2"))
answer1
[1] 248179786
Part Two
I haven’t solved this part yet!