Advertisement · 728 × 90
#
Hashtag
#AoCDay2Part2
Advertisement · 728 × 90
#Part 2 uses same input data frame already loaded in my part 1 solution

allpotentials <- data.frame(potentials = character())

for (i in 1:nrow(inputD2P1)) {
    j <- as.numeric(inputD2P1[i,1])
    k <- as.numeric(inputD2P1[i,2])
  for (n in j:k) { 
    p <- as.character(n)
    if (grepl("^(.+)\\1+$", p)) {allpotentials <- rbind(allpotentials,p) } } }

allpotentials <- allpotentials |> rename(potentials = 1) |> mutate(repeaters = as.numeric(potentials))

print(paste("answer is",sum(allpotentials$repeaters)))

#Part 2 uses same input data frame already loaded in my part 1 solution allpotentials <- data.frame(potentials = character()) for (i in 1:nrow(inputD2P1)) { j <- as.numeric(inputD2P1[i,1]) k <- as.numeric(inputD2P1[i,2]) for (n in j:k) { p <- as.character(n) if (grepl("^(.+)\\1+$", p)) {allpotentials <- rbind(allpotentials,p) } } } allpotentials <- allpotentials |> rename(potentials = 1) |> mutate(repeaters = as.numeric(potentials)) print(paste("answer is",sum(allpotentials$repeaters)))

Don't you just love it when a #AdventOfCode part 2 is easier than the part 1?

Luckily I had this niggling feeling that I'd seen someone use a regex to identify repeating patterns in an old AoC puzzle a couple of years back, and clever past Sarah had made a note of it, go me!

#AoCDay2Part2 #RStats

2 0 1 0