Advent of Code: Day 17

less than 1 minute read

Part 1

pos_to_idx <- function(x, l) {
    ifelse(x %% l == 0L, l, x %% l)
}

insert <- function(x, i, new) {    
    res <- c(x[1:i], new)
    if ((i + 1) <=  length(x)) {
        res <- c(res, x[(i+1):length(x)])
    }
    res
}

spiral <- function(step, rounds) {
    res <- 0
    i <- 1
    for (j in 1:(rounds + 1)) {
        res <- insert(res, i, j)
        i <- pos_to_idx(i + 1 + step, length(res))
    }
    res
}

res <- spiral(314, 2017)
res[which(res == 2017) + 1]
## [1] 355

Part 2

spiral2 <- function(step, rounds) {
    res <- 0
    i <- l <- 1
    for (j in 1:(rounds + 1)) {
        if (i == 1) {
            res[2] <- j
        }
        l <- l + 1
        i <- pos_to_idx(i + 1 + step, l)
    }
    res
}


res2 <- spiral2(314, 50e6)
res2[2]
## [1] 6154117

Comments