A king is angry at two scientists, so he decrees the following punishment. The scientists will be imprisoned in towers at opposite ends of the kingdom. Each morning, a guard at each tower will flip a coin and show the result to his prisoner. Each prisoner must then guess the result of the coin flip at the other tower. If at least one of the two guesses is correct, they will live another day. But as soon as both guesses are incorrect, they will be executed.
On the way out of the throne room, the scientists manage to confer briefly, and they come up with a plan that will spare them indefinitely. What strategy guarantees them to escape their execution forever?
There exists a rock-solid strategy that will lead to exactly one prisoner guessing correctly at all times. It's easiest to explain this strategy by observing that either both flips give the same or a different result. That is the paradigm shift you have to make to understand that the strategy simply is to decide which of the two prisoners will answer with the same result as the coin flip at his own tower and which prisoner will answer with the opposite result of the coin flip at his own tower. It might seem unlikely, but the following figure shows that this strategy guarantees that at least one guess will always be correct.
The first two lines contain the outcomes of the coin flips at the towers of the first and second prisoner: heads or tails. The third line indicates which scientist (first or second) will always say the same result as the one of the coin flip at his own tower. The other scientist will always answer with the opposite of the coin flip at his own tower.
Two lines that respectively contain the answer (heads or tails) of the first and the second prisoner, if they follow the infallible strategy described above.
Input:
heads
tails
second
Output:
tails
tails
Just ask Google1 to toss heads or tails if you don't have a coin in your pocket.