Day 3

Advent of Code: Worked Solutions

About
Date

December 3, 2022

Setup

# Libraries
library(tidyverse)

# Read input from file
input <- read_table("../input/day03.txt", col_names = "str")

Part 1

input |>
  mutate(
    # Separate each line into two compartments
    str_length = str_length(str) / 2,
    str_1 = str_sub(str, start = 1L, end = str_length),
    str_2 = str_sub(str, start = str_length + 1, end = -1L),

    # Find the letter in common between each pair of compartments
    across(c(str_1, str_2), ~ str_split(.x, "")),
    dup = map2_chr(str_1, str_2, ~ intersect(.x, .y)),

    # Convert to priority value
    priority = match(dup, c(letters, LETTERS))
  ) |>

  # Compute total sum
  pull(priority) |>
  sum()
[1] 8252

Part 2

input |>

  # Reshape to one row per group, one column per elf
  mutate(
    str = str_split(str, ""),
    group_num = floor((row_number() - 1) / 3),
    elf_num = as.character(row_number() %% 3)
  ) |>
  pivot_wider(names_from = elf_num, values_from = str, names_prefix = "elf_") |>

  # Find the character in common between all 3 elves & convert to priority val
  mutate(
    dup = pmap_chr(
      list(elf_0, elf_1, elf_2),
      ~ reduce(list(..1, ..2, ..3), intersect)
    ),
    priority = match(dup, c(letters, LETTERS))
  ) |>

  # Compute total sum
  pull(priority) |>
  sum()
[1] 2828