base R
strings
loops
Author

Ella Kaye

Published

December 6, 2022

Setup

The original challenge

My data

Part 1

Toggle the code
library(aochelpers)
input <- aoc_input_vector(6, 2022)
Toggle the code
data <- strsplit(input, "") |> unlist() 


for (i in 4:length(data)) {
  
  last_4 <- data[(i-3):i]
  last_4_unique <- last_4 |> unique() |> length()
  
  if (last_4_unique == 4) {
    break
  }
}
i
[1] 1080

Part 2

Toggle the code
for (i in 14:length(data)) {
  
  last_14 <- data[(i-13):i]
  last_14_unique <- last_14 |> unique() |> length()
  
  if (last_14_unique == 14) {
    break
  }
}
i
[1] 3645

Session info

Toggle
─ Session info ───────────────────────────────────────────────────────────────
 setting  value
 version  R version 4.3.1 (2023-06-16)
 os       macOS Sonoma 14.1
 system   aarch64, darwin20
 ui       X11
 language (EN)
 collate  en_US.UTF-8
 ctype    en_US.UTF-8
 tz       Europe/London
 date     2023-11-18
 pandoc   3.1.1 @ /Applications/RStudio.app/Contents/Resources/app/quarto/bin/tools/ (via rmarkdown)
 quarto   1.4.504 @ /usr/local/bin/quarto

─ Packages ───────────────────────────────────────────────────────────────────
 package     * version    date (UTC) lib source
 aochelpers  * 0.0.0.9000 2023-11-17 [1] local
 sessioninfo * 1.2.2      2021-12-06 [1] CRAN (R 4.3.0)

 [1] /Users/ellakaye/Library/R/arm64/4.3/library
 [2] /Library/Frameworks/R.framework/Versions/4.3-arm64/Resources/library

──────────────────────────────────────────────────────────────────────────────