Advent of Code 2020-08 with R

1 minute(s) read

Solving Advent of Code 2020-08 with R.

[Disclaimer] Obviously, this post contains a big spoiler about Advent of Code.

Instructions

Input looks like this

nop +0
acc +1
jmp +4
acc +3
jmp -3
acc -99
acc +1
jmp -4
acc +6
  • There is a global var called accumulator, starting at 0

  • acc n increases or decreases accumulator by n, and goes to next row

  • jmp n jumps to a its position + n

  • nop does nothing but going to the next execution

  • If the program tries to execute a row twice, we stop, and the eturned value is the value just before that

Find the complete instructions at: https://adventofcode.com/2020/day/8.

R solution

Part one

# Reading the data
input <- read.delim(
  sep = " ",
  "2020-08-aoc.txt",
  header = FALSE,
  stringsAsFactors = FALSE
)

give_me_a_break <- function(input){

  accumultor <- 0
  current_row <- 1
  done_row <- c()
  exit_code <- 0

  while(TRUE){
    if (current_row %in% done_row){
      exit_code <- 1
      break
    }
    if (current_row > nrow(input)){
      break
    }
    done_row <- c(done_row, current_row)
    curr_row <- input[current_row, ]
    if (curr_row$V1 == "nop") {
      current_row <- current_row + 1
    }
    if (curr_row$V1 == "acc") {
      accumultor <- accumultor + curr_row$V2
      current_row <- current_row + 1
    }
    if (curr_row$V1 == "jmp") {
      current_row <- current_row + curr_row$V2
    }
  }
  return(
    list(
      accumultor = accumultor,
      exit_code = exit_code
    )
  )
}


give_me_a_break(input)
## $accumultor
## [1] 1528
##
## $exit_code
## [1] 1

Part Two

for (i in which(input$V1 %in% c("nop", "jmp"))){

  modified_ipt <- input

  if (modified_ipt[i, 'V1'] == "nop"){
    modified_ipt[i, 'V1'] <- "jmp"
  } else {
    modified_ipt[i, 'V1'] <- "nop"
  }

  res <- give_me_a_break(modified_ipt)

  if (res$exit_code == 0){
    print(res$accumultor)
    break
  }

}
## [1] 640

What do you think?