There is a puzzle in the video game West of Loathing where the protagonist needs to apply 3200 pounds of pressure to activate an elevator following the mechanisms were
turning the number 3 bolt applies 411 pounds of pressure
turning the number 5 bolt applies 295 pounds of pressure
turning the number 7 bolt applies 161 pounds of pressure
but if the protagonist applies too much pressure, the situation completely resets. Thus we appear to have encountered the knapsack problem. Perhaps there is a sweet, vectorized way to solve this in R, but for now I will apply brute force. Also, each bolt can only be twisted up to 8 turns.
start_time <- Sys.time()
for(num3bolt in 8:0){
for(num5bolt in 8:0){
for(num7bolt in 8:0){
pressure <- sum(411*num3bolt + 295*num5bolt + 161*num7bolt)
if(pressure == 3200){
print(paste("Turn the #3 bolt", num3bolt, "times"))
print(paste("Turn the #5 bolt", num5bolt, "times"))
print(paste("Turn the #7 bolt", num7bolt, "times"))
break
}
}
}
}
## [1] "Turn the #3 bolt 4 times"
## [1] "Turn the #5 bolt 2 times"
## [1] "Turn the #7 bolt 6 times"
end_time <- Sys.time()
print(paste(end_time - start_time, "seconds passed."))
## [1] "0.0272798538208008 seconds passed."