Advent of Code 2023
  • Solutions

Day 7 ⋆

Published

December 7, 2023

Puzzle

https://adventofcode.com/2023/day/7

Solution

Preprocessing

records <- readr::read_delim(
  "input.txt", 
  delim = " ", 
  col_names = FALSE
)
records2 <- records %>%
  transmute(
    hand_number = 1:n(),
    cards = X1 %>% 
      stringr::str_split("") %>% 
      map(as_tibble),
    card_count = map(cards, ~count(.x, value)),
    bid = X2
  ) 
helper1_day7 <- function (input_df, card_ordering) {
  to_return <- left_join(
    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),
      outer_tbl$cards %>% 
        map_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)
}

Part One

answer1 <- records2 %>% 
  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!