2019-02: 1202 Program Alarm

library(mistlecode)
dtM <- readLines("input.txt") |>
  str_split(",", simplify = TRUE) |>
  as.numeric()

Part 1

dt <- dtM
dt[1 + 1] <- 12
dt[2 + 1] <- 2
d <- 0
x <- 1
while(d != 99) {
  if (dt[x] == 1) { dt[dt[x + 3] + 1] <- dt[dt[x + 1] + 1] + dt[dt[x + 2] + 1] }
  else if (dt[x] == 2) { dt[dt[x + 3] + 1] <- dt[dt[x + 1] + 1] * dt[dt[x + 2] + 1] }
  x <- x + 4
  d <- dt[x]
}
dt[1]
[1] 3706713
x
[1] 125

Part 2

breakFlag <- FALSE
for (i in 0:99) {
  for (j in 0:99) {
    dt <- dtM
    dt[1 + 1] <- i
    dt[2 + 1] <- j
    d <- 0
    x <- 1
    while (d != 99) {
      if (dt[x] == 1) {
        dt[dt[x + 3] + 1] <- dt[dt[x + 1] + 1] + dt[dt[x + 2] + 1]
      }
      else if (dt[x] == 2) {
        dt[dt[x + 3] + 1] <- dt[dt[x + 1] + 1] * dt[dt[x + 2] + 1]
      }
      x <- x + 4
      d <- dt[x]
    }
    if (dt[1] == 19690720) {
      breakFlag <- TRUE
      break
    }
  }
  if (breakFlag) { break }
}
100 * dt[1 + 1] + dt[2 + 1]
[1] 8609