In [1]:
library("stringr")
In [2]:
instructions <- readLines("data.txt")
In [3]:
instructions
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]]
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]]
In [9]:
cat("Q1- Value in the A buffer: ", buffer['a'][[1]], '\n')
cat("Q2- Value in the A buffer: ", buffer2['a'][[1]], '\n')