The Ant Goes Marching

In one of the best puzzle collections I know, Peter Winkler’s ​Mathematical Mind Benders​, is a little quickie that seems like it’d be interesting to un-quicken. The original is something like: “Three spiders and an ant are walking on a framework in the shape of the edges of a cube. The spiders want to eat the ant, and they all walk at last 1/3 as fast as the ant walks. Prove that some spider can catch the ant.”

We were meeting today at the Brighton Agile Roundtable, and we decided to play with the ant problem in codea. It turned out to be fun, and a good way to think about the problem from a programming viewpoint if not from a formal mathematical one.

Then we wrote a little table for our ant, so we could draw him at any fraction of the way along a line. We went through a few iterations of this and quickly wound up with a table of corners, a table of edges, and this code:

We had a rudimentary verson of the nextGoal function, which we used for an interim version that just drove the ant around the screen. This code just selects one of the three lines leading away from the corner we just arrived at:

All these are fairly straightforward: we start him on corner 1, heading toward corner 2. At first we started him at zero but when we implemented the spiders, we had everyone starting at zero and that was boring, because everyone arrived at the corners at the same time, so we started them all at a random fraction 0-1 along the line.

But what about that line

ant.nextGoal=nextGoal

Well, that second nextGoal refers to the nextGoal function up above. We see it being called here:

All this comes down to moving the bug along his line, incrementing the fraction by the speed value, and when the fraction exceeds 1, set his start to his previous stop, pick a new goal, and keep on keeping on.

The drawing is simple, we just translate to where the bug is (somewhere along his line), and draw a circle.

We went through a phase where we created three spiders and made them work the same way, except at slower speed:

The isAntDead function just checks to see if the ant is within ten pixels!pixels of any spider and if it is, we stop moving (but keep drawing, so the screen doesn’t go blank).

This worked fine and results in the halt you see in the final video below.

But we noticed one more thing. Because the ant selects its edge randomly, it has a good chance of selecting an edge that has a spider coming the other way, with tragic results for our poor ant. So we changed things to give the ant its own way of selecting an edge. That turned out to be a bit tricky but not bad:

We give the ant its own way of selecting the next goal, as shown above. We create a shuffled selection of edge choices, because we’re going to go through them to find a safe one. Then we try the first two to see if they are safe. If they are, we choose them. If they are not safe, we take the third choice. (You could imagine a more clever ant. For example, it might choose the path where the spider is furthest away, then wait a while and double back. Or it might just check to see if the spider is moving away on any of those edge, and go down that one, slowing down. And so on. For now, the ant isn’t that smart.)

To shuffle the edges we made a list of the integers 1,2,3, selected one at random, picked that edge, repeated, and took the last: