## DEV Community is a community of 603,444 amazing developers

We're a place where coders share, stay up-to-date and grow their careers.

# Advent of Code 2020-06 with R

Colin Fay Originally published at colinfay.me on ・1 min read

Solving Advent of Code 2020-06 with R.

[Disclaimer] Obviously, this post contains a big spoiler about Advent of Code.

## R solution

### Part one

``````library(magrittr)
library(purrr, warn.conflicts = FALSE)
# pasting everything into one big character string
paste(collapse = "\n") %>%
# splitting where there are two \n\n (new lines)
strsplit("\n\n") %>%
.[[1]] %>%
map_dbl(~ {
# Removing the new lines of each group
.x <- gsub("\n", "", .x)
# Splitting the string
strsplit(.x, "") %>%
.[[1]] %>%
# Computing the number of unique answer
unique() %>%
length()
}) %>%
sum()

## [1] 6534
``````

### Part two

``````library(dplyr, warn.conflicts = FALSE)
# pasting everything into one big character string
paste(collapse = "\n") %>%
# splitting where there are two \n\n (new lines)
strsplit("\n\n") %>%
.[[1]] %>%
map_dbl(~ {
# Removing the new lines of each group
x <- strsplit(.x, "\n")[[1]]
# Getting the group size
ngroup <- length(x)
# Splitting the original string
strsplit(.x, "")[[1]] %>%
data.frame(
x = .
) %>%
# Counting the number of occurrences of each letter
count(x) %>%
# Keeping only the questions that occur in all
filter(n == ngroup) %>%
nrow()
}) %>%
sum()

## [1] 3402
``````