library(tidyverse)
library(unglue)
Day 5
Advent of Code: Worked Solutions
Setup
Import libraries:
Read text input from file and separate the row segment of each string from the column seegment:
<- read_lines("../input/day05.txt") |>
input unglue_data("{row=[FB]+}{col=[LR]+}")
Part 1
Each F/B or R/L pair can be treated as a binary string. Replace F and L with 0, replace B and R with 1, and compute the result:
<- c("F" = "0", "B" = "1", "L" = "0", "R" = "1")
bin
<- input |>
df mutate(
across(everything(), ~ strtoi(str_replace_all(.x, bin), base = 2L)),
seat_id = row * 8 + col
)
Get the maximum seat ID:
max(df$seat_id)
Part 2
Find the missing value within the range of all seat IDs:
setdiff(min(df$seat_id):max(df$seat_id), df$seat_id)