R bloggers

Le Monde puzzle [#1051]

FavoriteLoadingAdd to favorites

A combinatoric Le Monde mathematical puzzle of limited size: When the only allowed move is to switch two balls from adjacent boxes, what is the minimal number of moves to return all balls in the above picture to their respective boxes? Same question with six boxes and 12 balls. The question is rather interesting to code as I decided to use recursion (as usual!) but wanted to gain time by storing the number of steps needed by any configuration to reach its ordered recombination. Meaning I had to update an external vector within the recursive function for each new configuration I met. With help from Julien Stoehr, who presented me with the following code, a simplification of a common R function v.assign <- function (i,value,…) { temp <- get(i, pos = 1) temp[…] <- value assign(i, temp, pos = 1)}…
Original Post: Le Monde puzzle [#1051]