As I was waiting for my boat on a French Guiana beach last week, I thought back about a recent riddle from The Riddler where an item does a random walk over a sequence of N integers. Behind doors. The player opens a door at the same rate as the item, door that closes immediately after. What is the fastest strategy to catch the item? With a small value of N, it seemed that repeating the same door twice and moving from 1 to N and backward was eventually uncovering the item.
Here is the cRude code I later wrote to check whether or not this was working:
p=1+t%%N #starting item position h=v=s=m=1 #h=door, v=attempt number, s=direction, m=repeat number while(h-p){ p=ifelse(p==1,2, #no choice ifelse(p==N,N-1, #no choice ifelse(p==h-1,p-1, #avoid door ifelse(p==h+1,p+1, #avoid door p+sample(c(-1,1),1))))) #random m=m+1 if(m>2){ h=h+s;m=1 if(h>N){ h=N-1;s=-1} if(!h){ s=1;h=2} } v=v+1
and the outcome for N=100 was a maximum equal to 198. The optimal strategy leads to 196 as repeating the extreme doors is not useful.