Gift Shop

Published

December 2, 2025

library(mistlecode)

options(scipen = 999)

Part 1

Fun litle bit of regex. I’m pleased it was that easy.

'input.txt' |>
  readLines() |>
  stringr::str_split_1(',') |>
  stringr::str_split('-') |>
  purrr::map(\(x) {
    sq <- x[1]:x[2]
    sq[
      sq |>
        as.character() |>
        stringr::str_detect('^(\\d+)(\\1)$')
    ]
  }) |>
  unlist() |>
  sum()
[1] 12850231731

Part 2

Just a little regex modification. Nice and simple.

'input.txt' |>
  readLines() |>
  stringr::str_split_1(',') |>
  stringr::str_split('-') |>
  purrr::map(\(x) {
    sq <- x[1]:x[2]
    sq[
      sq |>
        as.character() |>
        stringr::str_detect('^(\\d+)(\\1){1,}$')
    ]
  }) |>
  unlist() |>
  sum()
[1] 24774350322