2020: Day 6

tidyverse
Author

Ella Kaye

Published

December 6, 2020

Setup

The original challenge

My data

Part 1

library(dplyr)
library(tidyr)
library(stringr)

Within each group, we need to find the number of unique letters within each group. We read in and separate the data using the tricks learnt for Day 4, and take advantage of the rowwise() feature in dplyr 1.0.0.

customs_groups <- 
  readLines(here::here("2020", "day", "6", "input")) %>%
  as_tibble() %>%
  mutate(new_group = value == "") %>%
  mutate(group_ID = cumsum(new_group) + 1) %>%
  filter(!new_group) %>%
  select(-new_group) %>%
  group_by(group_ID) 

customs_groups %>%
  summarise(qs = str_c(value, collapse = "")) %>%
  ungroup() %>%
  mutate(qss = str_split(qs, "")) %>%
  rowwise() %>%
  mutate(qsu = list(unique(qss))) %>%
  mutate(count = length(qsu)) %>%
  ungroup() %>%
  summarise(total = sum(count)) %>%
  pull(total)
[1] 6585

Part 2

Now, instead of unique letters in a group, we need to find the number of letters which appear in all the answers for everyone in the same group. I first note how many people are in each group, then tabulate the number of occurrences of each letter in the group, then count (by summing a logical vector) the number of matches between occurrences of letter and the number in group. Finally, we sum across all groups.

customs_groups %>%  
  add_count(group_ID, name = "num_in_group") %>%
  group_by(group_ID, num_in_group) %>%
  summarise(qs = str_c(value, collapse = "")) %>%
  ungroup() %>%
  mutate(qss = str_split(qs, "")) %>%
  rowwise() %>%
  mutate(letter_table = list(table(qss))) %>%
  slice(1) %>%
  mutate(in_common = sum(num_in_group == letter_table)) %>%
  ungroup() %>%
  summarise(total = sum(in_common)) %>%
  pull(total)
[1] 3276

Session info

Toggle
─ Session info ───────────────────────────────────────────────────────────────
 setting  value
 version  R version 4.3.1 (2023-06-16)
 os       macOS Sonoma 14.0
 system   aarch64, darwin20
 ui       X11
 language (EN)
 collate  en_US.UTF-8
 ctype    en_US.UTF-8
 tz       Europe/London
 date     2023-11-06
 pandoc   3.1.1 @ /Applications/RStudio.app/Contents/Resources/app/quarto/bin/tools/ (via rmarkdown)
 quarto   1.4.466 @ /usr/local/bin/quarto

─ Packages ───────────────────────────────────────────────────────────────────
 package     * version date (UTC) lib source
 dplyr       * 1.1.2   2023-04-20 [1] CRAN (R 4.3.0)
 sessioninfo * 1.2.2   2021-12-06 [1] CRAN (R 4.3.0)
 stringr     * 1.5.0   2022-12-02 [1] CRAN (R 4.3.0)
 tidyr       * 1.3.0   2023-01-24 [1] CRAN (R 4.3.0)

 [1] /Users/ellakaye/Library/R/arm64/4.3/library
 [2] /Library/Frameworks/R.framework/Versions/4.3-arm64/Resources/library

──────────────────────────────────────────────────────────────────────────────