algorithm - Two robots in an infinite line -


this little bit different "parachuted robots" puzzle. there 2 robots hanging on infinite line. distance not relative, therefore doesn't matter if 5m or 5km apart. suppose need program 2 robots in order meet @ magnet , options have are:

  • one move left
  • one move right
  • two moves left
  • two moves right
  • stop , check if robot on magnet

the robots not know if there next magnet, , moving 1 robot move other robot on same direction. formulate algorithm 2 robots meet @ magnet.

i unable formulate solution question, there solution puzzle?

i think both robots can run 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 elegant way formulate it, there room improvement. idea go steps left double amount right, again double amount left , on... find magnet.


Comments