In [1]:
library("stringr")
options(warn=-1)
In [2]:
instructions <- readLines("data.txt")
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=7,b=0,c=0,d=0)
while(position <= length(instructions)){
instruc <- str_split(instructions[position]," ")[[1]]
switch(instruc[1],
cpy={
if(is.na(as.numeric(instruc[3]))){
buffer[instruc[3]] <- as.integer(getCustomValue(buffer,instruc[2]))
}
else {
print("------------------")
}
position <- position+1
},
inc={
if(is.na(as.numeric(instruc[2]))){
buffer[instruc[2]] <- as.integer(buffer[instruc[2]]) + 1
}
position <- position+1
},
dec={
if(is.na(as.numeric(instruc[2]))){
buffer[instruc[2]] <- as.integer(buffer[instruc[2]]) - 1
}
position <- position+1
},
tgl={
toChange <- as.integer(getCustomValue(buffer,instruc[2])) + position
if(toChange<=length(instructions)){
newInstruct <- str_split(instructions[toChange]," ")[[1]]
if(length(newInstruct) == 2){
if (newInstruct[1]=="inc"){
newInstruct[1] <- "dec"
} else {
newInstruct[1] <- "inc"
}
} else {
if (newInstruct[1]=="jnz"){
newInstruct[1] <- "cpy"
} else {
newInstruct[1] <- "jnz"
}
}
instructions[toChange] <- paste(newInstruct, collapse = " ")
}
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 [8]:
egg1 <- 7
big1 <- 94
big2 <- 82
Q1 <- factorial(egg1) + big1*big2
Q1
In [9]:
egg2 <- 12
Q2 <- factorial(egg2) + big1*big2
Q2
In [9]:
cat("Q1- Value in the A buffer when the egg hint is 7: ", Q1, '\n')
cat("Q2- Value in the A buffer when the egg hint is 12: ", Q2, '\n')