views:

377

answers:

3

A helicopter drops two trains, each on a parachute, onto a straight infinite railway line.

There is an undefined distance between the two trains.

Each faces the same direction, and upon landing, the parachute attached to each train falls to the ground next to the train and detaches.

Each train has a microchip that controls its motion. The chips are identical.

There is no way for the trains to know where they are.

You need to write the code in the chip to make the trains bump into each other.

Each line of code takes a single clock cycle to execute.

You can use the following commands (and only these):

  • MF - moves the train forward
  • MB - moves the train backward
  • IF (P) - conditional that is satisfied if the train is next to a parachute. There is no "then" to this IF statement.
  • GOTO
+23  A: 

Make each train move forwards slowly until it finds a parachute. When the back train finds the parachute of the front train, make it move forwards faster to catch up with the front train.

1.  MF
2.  IF(P)
3.    GOTO 5
4.  GOTO 1
5.  MF
6.  GOTO 5

If you want to make it take less time for the trains to reach each other, at the cost of some extra lines of code, you can unroll the second loop.

jchl
If that's the correct syntax for the `IF` instruction, this is one correct answer. Even if the 2nd and 3rd instructions are on the same line, it'll still work.
Lasse V. Karlsen
This is a hard one. It needs the understand of unified leader election.
none
I don't think this is a correct answer. I think that MF and MB do not regulate the speed of the train, but just move it some distance.
Dialecticus
@Dialecticus, it's not the MF and MB per se which regulate the speed of the train, but their frequency of execution. Note that initially MF is executed on every 3rd (or 4th, depending on the syntax of IF) cycle, but after GOTO 5, it is executed on every 2nd cycle.
Péter Török
However, we can simulate a third of the normal speed by replacing first MF with MF MF MB.
Dialecticus
@Péter, this is assuming that MF and MB cost the same as IF and GOTO.
Dialecticus
@Dialecticus, no need to assume, it is clearly stated above: _"Each line of code takes a single clock cycle to execute."_
Péter Török
@Péter: Oh. Sweet.
Dialecticus
+2  A: 
label1: MF
If (P)
{
   // Do nothing (because of no then?)
}
ELSE
{
   MF;
   MB;
   GOTO label1;
}
label 2:MF
GOTO label2;

GO forward 2 times, backward 1 times until meet the other train's parachute go forward like crazy (to bump into the other - it's still Forward then backward - meaning it's go slower). I use MF one time in label 2, mean it take 2 clock cycle to go one step forward. In label1 it took 5 clock cycle to go forward one steps. So if we use more MF in label2 two of them will bump into eachother faster.
No variable used.

Kiennx
I formatted your code; please use the `101010` button or indent your code by 4 spaces next time :-)
Péter Török
Btw this basically uses the same logic as @jchl's solution, except that it uses blocks and ELSE, which apparently don't exist in this language.
Péter Török
Yes, I see that. But I didn't see his solution when I write mine. If I refresh the page soon enough then I'll happy with his solution. I use block for easier understand, and ELSE because of "no then" in the question.
Kiennx
A: 

I had this question in a Google Interview -- It was posed as a Mars rover question. The question formating here is significantly better. But they are both a bit unclear.

Ivan