library(mistlecode)
x2 <-
readLines("input.txt") |>
as.integer()
Part 1
I tried recursion first, had a bit of trouble, then got C stack errors. Back to while loops we go…
x <- x2
pos <- 1
inc <- 0
while(pos >= 1 & pos <= length(x)) {
x[pos] <- x[pos] + 1
pos <- pos + x[pos] - 1
inc <- inc + 1
}
inc
Part 2
For some reason I struggled with getting the logic in the right order, but a poor night’s sleep does wonders :|
x <- x2
pos <- 1
inc <- 0
while(pos >= 1 & pos <= length(x)) {
jmp <- x[pos]
x[pos] <- if(x[pos] >= 3) x[pos] - 1 else x[pos] + 1
inc <- inc + 1
pos <- pos + jmp
}
inc