The Lord of the Coins: The Two Towers

8 minute read

Recently there was an interesting puzzle posted on the ODS Slack channel. An ancient evil dragon captures Alice and Bob and puts them in two different towers. The dragon tosses a fair coin infinitely many times for Alice, and then does the same for Bob. The adventures should each name a number, and the dragon checks the coin toss outcome of the Alice’s infinite sequence at the position number, named by Bob, and then the coin toss outcome of the Bob’s sequence at the position number, named by Alice. If the outcomes are the same, our heroes are free. What strategy can you think of to maximise the probability of the successful release?

If you want to read the final thoughts on the possible solution, please go to the section ‘Scalar products, decision vectors and gradients’. Otherwise, here’s the story how I came up with it.

First things first

This problem looked suspicious to me, because neither Alice nor Bob know each others sequences, and naturally don’t hear the second person’s choice (and therefore can’t infer anything from that).

Three questions you might want to ask before approaching this problem. First, why does the dragon have towers, if they usually live in caves filled with gold and gems? Second, how fast the dragon should toss the coins before Alice and Bob get bored? Third, is there a way to predict a simingly stochastic value?

We can start from a simple question: apparently, there is a bunch of possible strategies.

I can put the success condition in a slighltly more formal way. Let’s call the sequence Alice sees , and the sequence Bob sees . If Alice picks as the index for Bob, and Bob’s choice is , they are doing great if . Bad luck otherwise! They are going to solve stupid puzzles for the rest of their days.

Minimum viable strategy

Intuitively, the odds of winning are 50/50. Indeed, a simple strategy: “pick 1” works like that. A way to illustrate this is to use this table:

H T
H
T

The first row and the first column are the players’ possible outcomes at their first coin toss. In two out of four times the first items are the same, i.e. the total winning probability is .

Interestingly, since Alice and Bob have never actually used any information about their sequences, it doesn’t matter what numbers the pick.

Can the first coin help us?

The next stage of developing our strategy is to think whether the outcome of the first coin toss can anyhow help us. Let’s try out the following strategy:

If this first coin is heads (H), pick "1". Otherwise, pick any other number.

We can explicitly calculate the probability of winning in all possible cases, and then get the full probability. Without loss of generality we assume Alice picks number , nd Bob picks (when they don’t see heads at the start of their sequences). The corresponding outcomes table looks somewhat like this:

$$a_1 = H, a_m = H$$ $$a_1 = H, a_m = T$$ $$a_1 = T, a_m = H$$ $$a_1 = T, a_m = T$$
$$b_1 = H, b_k = H$$
$$b_1 = H, b_k = T$$
$$b_1 = T, b_k = H$$
$$b_1 = T, b_k = T$$

The table provides all possible outcomes for current strategy. For example, if Alice and Bob both have heads first , they win regardless of what are the other elements of their sequences. Otherwise, there’s a 50% match probability. Overall, this gives us , which is already great!

I believe this ‘imbalance’ comes from the fact Alice and Bob have actually communicated before the game. We have removed some uncertainty in the decision making process, and improved the odds.

Which came first, the mathematician or the puzzle?

Let’s try to add some extra a priory information into the process. Now, the decision is based on the first appearance of, say, heads in the sequence, instead of just the first item.

Pick number equal to the position of the first heads in your sequence.

Naturally, the probability of having the first heads on -th trial is given by geometric distribution with , and therefore equals to . Let’s consider as an example the case when Alice has heads on the first position .

Bob has a 0.5 probability to have heads on the first position, and therefore it’s a match - they both call “1” and point to each other’s heads. If Bob has his first heads on any other position (say, ), then the probability of winning is essentially 0.5 (we don’t know anything about Alice’s -th item, which should be tails, because Alice will point to Bob’s first item, tails). So, the probability of the event “Bob has first heads at and Alice has tails at ” is .

Summing up the last paragraph, if Alice has heads first, the probability of winning is

We can continue and calculate the winning odds for the cases Alice has first heads after one tail, two tails, and so on. I will leave the proof to the reader:

Finally, explicit treatment of probability of all possible Alice’s cases results in

Three is a lucky number

Before we dive into boring maths, this is a solution proposed by @mihaild. I like its symmetry, and the way it enriches the ‘first appearance’ approach.

Alice and Bob split their sequences into triplets, and focus on the first triplet with not all the same elemets ( and are called trivial and are skipped). We can show, that the probability Alice and Bob have the first non-trivial triplet at the same position is 0.6.

Then, we use this rule to pick an intermediate number:

{'HHT': 1, 'HTH': 2, 'HTT': 2, 'THH': 0, 'THT': 1, 'TTH': 0}

We add the intermediate number to the global index of the first element in the first non-trivial triplet, and this will be the number we tell to the dragon. (Here by global index I mean the index of the element in the original sequence given to us).

Again, direct simulation shows that Alice and Bob win in 5 out of 6 games (this applies to the case of Alice and Bob have the first non-trivial triplet at the same position). Otherwise, they win in 1 out of 2 games, equivalently to the Minimum Viable Strategy. This gives us probability of winning.

Scalar products, decision vectors and gradients.

I am going to use numerical values for heads and tails: let , from now on. The Heaviside step function will be also very useful.

Let’s denote a sequence of coin toss outcomes of length by vector . The elements of this vector are either +1 or -1, which is heads or tails as we called them before. There are possible different such vectors, and to distinquish one from another the upper index is used, .

The general idea of all the discussed strategies was to find a set of numbers, one per each possible state of your own sequence. Indeed, what that number really does, is pulls out a certain element of the unknown sequence. This is exactly how a scalar product works. I prefer to think about it as the dragon takes a scalar product of the Bob’s heads-tails sequence and the Alice’s decision vector. The decision vector has all zeroes (do not pick) and a single ‘one’ (pick this) value. This scalar product is:

where is the Bob’s sequence, is the Alice’s decision vector with elements (Alice picks ).

I am going to focus on a strategy, which only operates with first elements of the sequences. Also, Alice and Bob should follow the same strategy. I assume for every there exists a decision vector, , even though I have no idea what it looks like. Then, if Alice has got a sequence with the first elements being and for Bob it is , they win if and only if

For given they win exactly if

Therefore, if we sum this expression over all possible and , we are getting the total number of wins. And the total winning probability is just this number, normalized by the number of all possible Alice-Bob combinations. Namely,

Now, carefully: sleight of hand. The decision vector elements can be not only zeroes and ones. The elements of the decision vector represent the probability of choosing this particular index. From now on, for any appropriate and , , .

This is a typical optimization problem with analytical gradients. The gradient of the probability function with respect to a decision vector element equals to

A positive non-zero constant doesn’t change the gradient, so it is possible to use this expression for actual decision vectors tuning.

Gradient-based optimization: results

I could find a local optimum with , for a case with rather small . The optimal sequence vectors - decision vectors system looks like:

A = [[-1, -1, -1],
    [-1, -1,  1],
    [-1,  1, -1],
    [-1,  1,  1],
    [ 1, -1, -1],
    [ 1, -1,  1],
    [ 1,  1, -1],
    [ 1,  1,  1]]

D = [[ 0.33, 0.33, 0.34],
    [ 0.,  0.,  1.],
    [ 1.,  0.,  0.],
    [ 0.,  0.,  1.],
    [ 0.,  1.,  0.],
    [ 0.,  1.,  0.],
    [ 1.,  0.,  0.],
    [ 0.33, 0.33, 0.34]]

This solution is also a global optimum, which can be verified by brute force.

The solution is not unique. For example, the first and the last row of the decision matrix D can be shuffled as you wish: the trivial triplets (supposedly) should always result in the same contribution regardless of the decision vector elements.

If I increase the sequence length to , the winning probability is slightly higher, .

Some conclusions

I have strong doubts we can reach as . Moreover, I have shown that for the best probability you can get is below 0.7, which is reachable with the ‘triples’ strategy mentioned above. My implementation of the optimization algorithm doesn’t scale well with , so it is hard to find optimal solutions for large Ns. I’m looking forward to make it reasonably fast, even though the decision matrix size grows exponentially.

If you have any ideas on the symmetries of the optimal solutions, or, more important, you can spot any mistakes and typos, please let me know.

Thanks for reading and happy escape from the dragon!

Updated: