Data
Input is a series of CPU instructions.
input_file <- here("DATA", "2022", "input_day_10.txt")
cat(system(paste0("head ", input_file), intern = TRUE), sep = "\n")
noop
noop
addx 5
addx 29
addx -28
addx 5
addx -1
noop
noop
addx 5
all_instructions <- scan(
input_file,
what = "character",
sep = "\n"
)
There are two types of instructions:
noop
- lasts one cycle and doesn’t do anything;
addx V
- lasts two cycles and after two cycles, the X register has increased by V
(can be also negative)
I will need to follow the cycles and the X register.
Functions
increase_cycle <- function(){
cycle <<- cycle + 1
}
copy_last_reg <- function(){
register[cycle + 1] <<- register[cycle]
}
add_to_reg <- function(V){
register[cycle + 1] <<- register[cycle] + V
}
Run cycles
I need to know of only 220 cycles.
register <- rep(NA, 240)
# register starts with 1
register[1] <- 1
head(register)
I will need to start from cycle 1 instead of 0.
start_cycle <- 1
cycle <- start_cycle
idx <- 1
copy_last_reg()
while(idx <= length(all_instructions)){
instruction <- all_instructions[idx]
if(instruction == "noop"){
increase_cycle()
copy_last_reg()
} else {
V <- as.numeric(word(instruction, 2))
increase_cycle()
copy_last_reg()
increase_cycle()
add_to_reg(V)
}
idx <<- idx + 1
}
Compute total strength
The signal strength is equal to the multiplication of the X register value and cycle number. We focus on cycles: 20, 60, 100, 140, 180, and 220.
chosen_cycles <- c(20, 60, 100, 140, 180, 220)
sum_signal_strengths <- sum(chosen_cycles*register[chosen_cycles + 1])
The sum of chosen signal strengths is 1.492^{4}! 🥳