This post contains some simple code for simulating the Flatland Paradox in R. This is not meant to be optimal code or a perfect solution, it's just a quick look at a fun problem.
You can read about the paradox and its solution here. As it's described on the linked site, the paradox is essentially that the path that shortens the length of the string is most likely to reveal the treasure. When I first read about it, it took me a while to understand the problem, so I wrote up a little simulation to get my head around it. This post has the code to the simulation I wrote.
The code is pretty short and pretty annotated, so I'll let it speak for itself:
#go through 500 trials of the paradox, enough to get a good sense of what happens
#N is the length of the "trip"
#vect will record the steps of the trip
#here, 1 through 4 represent the directions (N,S,E,W), that one might travel
#if you're past the first step, check whether the last two steps cancel with each other
#here, we check cancellation by checking summation to 5 (isomorphic to backtracking)
#if the last two steps cancel, remove the last two steps taken on the trip
#store "vect no x", the vector before the last step is taken
}#end inside while
#"longer" records whether the trip is shorter or longer after the last step
#longer also tells us whether backtracking helped (1 means it did)
}#end outside while
#the mean will be around .5, meaning that backtracking helps about 3/4 of the time.
This simulation gives us the counter-intuitive answer: that backtracking is indeed the best strategy for locating the treasure. What do you think of this simulation? I would love to read your thoughts in the comments.