Day 6

Advent of Code: Worked Solutions

About
Date

December 6, 2020

Setup

Import libraries:

library(tidyverse)

Read text input from file:

input <- read_lines("../input/day06.txt")

Split input into separate groups of character sets wherever we encounter an empty newline:

groups <- input |> 
  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()