less than 1 minute read

Part 1

maze_data <- scan("advent-data/2017-12-05-advent-day5.txt")

escape_maze <- function(maze) {
    i <- 1
    steps <- 0
    while (i <=  length(maze)) {
        maze[i] <- maze[i] + 1
        i <- (maze[i] - 1) + i
        steps <- steps + 1

    }
    steps
}

escape_maze(c(0, 3, 0, 1, -3))
## [1] 5
escape_maze(maze_data)
## [1] 360603

Part 2

escape_maze2 <- function(maze) {
    i <- 1
    steps <- 0
    while (i <=  length(maze)) {
        if (maze[i] >= 3)
            chg <- -1
        else chg <- 1
        maze[i] <- maze[i] + chg
        i <- (maze[i] - chg) + i
        steps <- steps + 1
    }
    steps
}

escape_maze2(c(0, 3, 0, 1, -3))
## [1] 10
escape_maze2(maze_data)
## [1] 25347697

Comments