Reputation: 131
This is a little bit different from the "Parachuted Robots" puzzle. There are two robots hanging on an infinite line. Their distance is not relative, therefore it doesn't matter if they are 5m or 5km apart. Suppose that you need to program the two robots in order to meet at the magnet and the only options you have are:
The robots will not know if there are already next to the magnet, and moving one robot will also move the other robot on the same direction. Formulate an algorithm for the two robots to meet at the magnet.
I am unable to formulate a solution to this question, is there a solution to the puzzle?
Upvotes: 1
Views: 1310
Reputation: 8773
I think both robots can run the same program:
var steps = 10, count = 10, left = true;
while (!isRobotOnMagnet()) {
if (count-- > 0)
if (left)
oneMoveLeft();
else
oneMoveRight();
else {
steps *= 2;
left = !left;
count = steps;
}
}
Not the most elegant way to formulate it, there is room for improvement. The idea is go some steps to the left then double the amount to the right, then again double the amount to the left and so on... you will eventually find the magnet.
Upvotes: 1