library(tidyverse)
Day 6
Advent of Code: Worked Solutions
Setup
Import libraries:
Read text input from file:
<- read_lines("../input/day06.txt") input
Split input into separate groups of character sets wherever we encounter an empty newline:
<- input |>
groups split(cumsum(input == "")) |>
map(\(vec) {
|>
vec discard(~ .x == "") |>
str_split("")
})
Part 1
Count the total unique characters in every group and sum the result:
|>
groups map_int(
~ .x |>
unlist() |>
unique() |>
length()
|>
) sum()
Part 2
In each group, count the number of characters in common over every line in the group. Sum the result.
|>
groups map_int(
~ .x |>
reduce(intersect) |>
length()
|>
) sum()