library(tidyverse)
library(unglue)Day 2
Advent of Code: Worked Solutions
Setup
Import libraries:
Read text input from file into a set of start/end values:
input <- read_lines("../input/day02.txt") |>
str_split_1(",") |>
unglue_data("{start}-{end}", convert = TRUE)Part 1
Use regex to match all instances of single-repeated substrings within the range of integers:
input |>
with(map2(start, end, seq)) |>
unlist() |>
keep(\(x) str_detect(x, "^(.+)\\1$")) |>
sum()Part 2
Alter the regex from ^(.+)\\1$ to ^(.+)\\1+$ to allow any number of repeated matches (not just one):
input |>
with(map2(start, end, seq)) |>
unlist() |>
keep(\(x) str_detect(x, "^(.+)\\1+$")) |>
sum()