library(data.table) library(magrittr) dt <- readLines("input.txt") |> strsplit("") |> unlist() |> as.numeric()
data.table( "input" = c(dt), "test" = c(dt[-1], dt[1]) ) %>% .[, "valid" := ifelse(input == test, TRUE, FALSE)] %>% .[valid == TRUE, test] %>% sum()
[1] 1031
len <- length(dt) / 2 data.table( "input" = c(dt), "test" = c(dt[(len + 1):length(dt)], dt[1:len]) ) %>% .[, "valid" := ifelse(input == test, TRUE, FALSE)] %>% .[valid == TRUE, test] %>% sum()
[1] 1080