Advent of code Day 12

Goal: Create a small Microcontroller for custom assembly

Ref: http://adventofcode.com/2016/day/12


In [1]:
library("stringr")

In [2]:
instructions <- readLines("data.txt")

In [3]:
instructions


  1. "cpy 1 a"
  2. "cpy 1 b"
  3. "cpy 26 d"
  4. "jnz c 2"
  5. "jnz 1 5"
  6. "cpy 7 c"
  7. "inc d"
  8. "dec c"
  9. "jnz c -2"
  10. "cpy a c"
  11. "inc a"
  12. "dec b"
  13. "jnz b -2"
  14. "cpy c b"
  15. "dec d"
  16. "jnz d -6"
  17. "cpy 16 c"
  18. "cpy 17 d"
  19. "inc a"
  20. "dec d"
  21. "jnz d -2"
  22. "dec c"
  23. "jnz c -5"

In [4]:
getCustomValue <- function(bf,x){
    if (x=='a'|x=='b'|x=='c'|x=='d')
        return(bf[x])
    return(x)
}

In [5]:
position <- 1
buffer <- list(a=0,b=0,c=0,d=0)
while(position <= length(instructions)){
    instruc <- str_split(instructions[position]," ")[[1]]
    switch(instruc[1], 
        cpy={
          buffer[instruc[3]] <- as.integer(getCustomValue(buffer,instruc[2]))
          position <- position+1
        },
        inc={
          buffer[instruc[2]] <- as.integer(buffer[instruc[2]]) + 1 
          position <- position+1
        },
        dec={
          buffer[instruc[2]] <- as.integer(buffer[instruc[2]]) - 1     
          position <- position+1
        },
        jnz={
          if(as.integer(getCustomValue(buffer,instruc[2]))!=0){
             position <- position + as.integer(getCustomValue(buffer,instruc[3]))
          } 
           
          else{
              position <- position+1
          }
        },
        {
           print('default')
        }
    )
}

In [6]:
buffer['a'][[1]]


318083

In [7]:
position <- 1
buffer2 <- list(a=0,b=0,c=1,d=0)
while(position <= length(instructions)){
    instruc <- str_split(instructions[position]," ")[[1]]
    switch(instruc[1], 
        cpy={
          buffer2[instruc[3]] <- as.integer(getCustomValue(buffer2,instruc[2]))
          position <- position+1
        },
        inc={
          buffer2[instruc[2]] <- as.integer(buffer2[instruc[2]]) + 1 
          position <- position+1
        },
        dec={
          buffer2[instruc[2]] <- as.integer(buffer2[instruc[2]]) - 1     
          position <- position+1
        },
        jnz={
          if(as.integer(getCustomValue(buffer2,instruc[2]))!=0){
             position <- position + as.integer(getCustomValue(buffer2,instruc[3]))
          } 
           
          else{
              position <- position+1
          }
        },
        {
           print('default')
        }
    )
}

In [8]:
buffer2['a'][[1]]


9227737

In [9]:
cat("Q1- Value in the A buffer: ", buffer['a'][[1]], '\n')
cat("Q2- Value in the A buffer: ", buffer2['a'][[1]], '\n')


Q1- Value in the A buffer:  318083 
Q2- Value in the A buffer:  9227737