Skip to content
Vamshi Jandhyala

Books · The Riddler

Chapter 185

The Case Of The Smudged Secret Message

Riddler Express

Five improvisers perform a scene. They each pick a character at the start; halfway through the scene they each switch to a different character, so the five characters are unchanged but no actor plays the same one in both halves. Two of the five characters are villains and three are heroes. Assuming each valid reallocation is equally likely, what is the probability that at least one actor plays a villain in both halves?

The Riddler, FiveThirtyEight, June 8, 2018(original post)

Solution

The reallocation is a derangement of the five actors: a permutation π\pi of {1,2,3,4,5}\{1, 2, 3, 4, 5\} with π(i)i\pi(i) \ne i for every ii. The number of derangements of five elements is D5  =  5!k=05(1)kk!  =  12044120  =  44.D_{5} \;=\; 5! \sum_{k = 0}^{5} \frac{(-1)^{k}}{k!} \;=\; 120 \cdot \frac{44}{120} \;=\; 44. Label the two villain-actors V1,V2V_{1}, V_{2}. Define the events A  =  {π(V1)=V2},B  =  {π(V2)=V1}.A \;=\; \{\pi(V_{1}) = V_{2}\}, \qquad B \;=\; \{\pi(V_{2}) = V_{1}\}. A villain is played twice in a row exactly when at least one of A,BA, B occurs.

Counting AA by symmetry. Fix the villain V1V_{1}. Over all D5=44D_{5} = 44 derangements, V1V_{1} maps to one of the four other actors, and by symmetry each of those four images is equally likely. So exactly 44/4=1144/4 = 11 derangements have π(V1)=V2\pi(V_{1}) = V_{2}. By the same argument B=11|B| = 11.

Intersection. ABA \cap B fixes the swap V1V2V_{1} \leftrightarrow V_{2}. The remaining three hero-actors must derange themselves, giving D3=2D_{3} = 2. So AB=2|A \cap B| = 2.

Inclusion-exclusion. AB  =  A+BAB  =  11+112  =  20.|A \cup B| \;=\; |A| + |B| - |A \cap B| \;=\; 11 + 11 - 2 \;=\; 20. Therefore   Pr(at least one repeat villain)  =  2044  =  511    45.45%.  \boxed{\;\Pr(\text{at least one repeat villain}) \;=\; \frac{20}{44} \;=\; \frac{5}{11} \;\approx\; 45.45\%.\;}

The symmetry step is worth pausing on. Among D5=44D_{5} = 44 derangements, the image π(V1)\pi(V_{1}) ranges over the four allowed values V2,H1,H2,H3V_{2}, H_{1}, H_{2}, H_{3}. Relabelling those four targets is a symmetry of the derangement-count (a derangement remains a derangement under any permutation of the codomain that fixes V1V_{1}’s image set), so the four counts are equal at 44/4=1144/4 = 11 each.

The computation

Encode the problem: enumerate all 4444 derangements of {0,1,2,3,4}\{0, 1, 2, 3, 4\} (let 0,10, 1 index the villains and 2,3,42, 3, 4 the heroes), and count those that send 010 \to 1 or 101 \to 0.

from itertools import permutations
from fractions import Fraction

actors = (0, 1, 2, 3, 4)
villains = {0, 1}
derangements = [p for p in permutations(actors) if all(p[i] != i for i in actors)]
print("D_5 =", len(derangements))

# A villain repeat: some actor i in {0,1} satisfies p[i] in {0, 1}, i.e. a villain plays villain.
# But p[i] != i (derangement), so the only way villain i plays villain in 2nd half is
# p[i] = the other villain.
repeats = sum(1 for p in derangements if p[0] == 1 or p[1] == 0)
print("repeat villains =", repeats, "/", len(derangements))
print("probability =", Fraction(repeats, len(derangements)))

The script prints D5=44D_{5} = 44 and 20/44=5/1120/44 = 5/11, confirming the boxed answer.

Riddler Classic

Two equations have been intercepted. In each, different letters stand for different digits. In the first, every digit 0,1,,90, 1, \ldots, 9 appears, and four letters have been smudged (shown as dashes). In the second, exactly one of the letters is wrong. Recover both equations.

The first equation, with the smudges relabelled A,B,C,DA, B, C, D, is EXMREEK+EHKREKK  =  AKBHCXDE.\mathtt{EXMREEK} + \mathtt{EHKREKK} \;=\; \mathtt{AKBHCXDE}. The second equation is YTBBEDMKD+YHDBTYYDD  =  EDYTERTPTY,\mathtt{YTBBEDMKD} + \mathtt{YHDBTYYDD} \;=\; \mathtt{EDYTERTPTY}, with exactly one letter in error.

The Riddler, FiveThirtyEight, June 8, 2018(original post)

Solution (Equation 1)

The sum of two 77-digit numbers is at most 1999999819\,999\,998 and is the 88-digit AKBHCXDE\mathtt{AKBHCXDE}, so the leading digit is A=1\mathtt{A} = 1.

The units column is K+K=E\mathtt{K} + \mathtt{K} = \mathtt{E} (modulo 1010 and a possible carry). The hundreds-thousands column on the left has E+E\mathtt{E} + \mathtt{E}, which must equal C\mathtt{C} (modulo 1010) and contribute the leading carry into position 77 (the K\mathtt{K} column of the answer). Working through these constraints by digit-by-digit elimination, or, faster, by brute search over the 10!10! digit assignments respecting the letter constraints, the unique solution is A=1,B=0,C=5,D=9,E=6,H=8,K=3,M=4,R=7,X=2.\begin{aligned} \mathtt{A} &= 1, & \mathtt{B} &= 0, & \mathtt{C} &= 5, & \mathtt{D} &= 9, & \mathtt{E} &= 6, \\ \mathtt{H} &= 8, & \mathtt{K} &= 3, & \mathtt{M} &= 4, & \mathtt{R} &= 7, & \mathtt{X} &= 2. \end{aligned} Substituting,   6247663  +  6837633  =  13085296.  \boxed{\;6\,247\,663 \;+\; 6\,837\,633 \;=\; 13\,085\,296.\;} All ten digits 0099 appear, as required.

Solution (Equation 2)

By the same leading-digit argument, E=1\mathtt{E} = 1. The units column D+D=Y\mathtt{D} + \mathtt{D} = \mathtt{Y} and the hundred-millions column Y+Y+(carry)=D\mathtt{Y} + \mathtt{Y} + (\text{carry}) = \mathtt{D} (mod 1010) give the small system 2DY(mod10)2\mathtt{D} \equiv \mathtt{Y} \pmod{10}, 2Y+cD(mod10)2\mathtt{Y} + c \equiv \mathtt{D} \pmod{10} for some carry c{0,1}c \in \{0, 1\}. With c=1c = 1, D=3\mathtt{D} = 3, Y=6\mathtt{Y} = 6 works (3+3=63 + 3 = 6 and 6+6+1=136 + 6 + 1 = 13). Continuing the column-by-column logic (or by computer search over the 10910 \cdot 9 \cdots assignments with one letter freed up to vary), the unique solution is B=5,  D=3,  E=1,  H=7,  M=2,  P=8,  R=0,  T=9,  Y=6.\mathtt{B} = 5,\; \mathtt{D} = 3,\; \mathtt{E} = 1,\; \mathtt{H} = 7,\; \mathtt{M} = 2,\; \mathtt{P} = 8,\; \mathtt{R} = 0,\; \mathtt{T} = 9,\; \mathtt{Y} = 6. Substituting into the intended (correct) equation, position 88 of the first number reads Y\mathtt{Y} (digit 66), not K\mathtt{K}: 695513263  +  673596633  =  1369109896.695\,513\,263 \;+\; 673\,596\,633 \;=\; 1\,369\,109\,896.

The computation

Encode each puzzle as a constraint-satisfaction problem and brute-force the digit assignments. For equation 11, the constraint is that the letter substitution gives an arithmetically valid sum, with all ten digits appearing and the dashes A,B,C,D\mathtt{A}, \mathtt{B}, \mathtt{C}, \mathtt{D} distinct from the named letters. For equation 22, search over which of the ten letters is wrong, freeing that letter from the all-different constraint at that position and checking the sum.

  1. Enumerate digit assignments to the letters {E,X,M,R,K,A,B,C,D,H}\{\mathtt{E}, \mathtt{X}, \mathtt{M}, \mathtt{R}, \mathtt{K}, \mathtt{A}, \mathtt{B}, \mathtt{C}, \mathtt{D}, \mathtt{H}\} for equation 11; check the arithmetic.

  2. For equation 22, enumerate digit assignments to {Y,T,B,E,D,M,K,H,R,P}\{\mathtt{Y}, \mathtt{T}, \mathtt{B}, \mathtt{E}, \mathtt{D}, \mathtt{M}, \mathtt{K}, \mathtt{H}, \mathtt{R}, \mathtt{P}\}; for each, also try replacing each of the ten letter-positions in the first number with a different letter and check the arithmetic.

from itertools import permutations

def letters_to_int(s, m):
    n = 0
    for ch in s:
        n = 10 * n + m[ch]
    return n

# Equation 1: brute search over 10! / (10 - len(letters))! assignments
LET1 = "EXMRKABCDH"
A1, B1, S1 = "EXMREEK", "EHKREKK", "AKBHCXDE"
sols1 = []
for perm in permutations(range(10)):
    m = dict(zip(LET1, perm))
    if m['E'] == 0 or m['A'] == 0:
        continue
    if (letters_to_int(A1, m) + letters_to_int(B1, m)
            == letters_to_int(S1, m)):
        sols1.append(m)
        break  # unique
m1 = sols1[0]
print("equation 1:", letters_to_int(A1, m1), "+",
      letters_to_int(B1, m1), "=", letters_to_int(S1, m1))

# Equation 2: confirm the corrected letter pattern with K -> Y gives a valid sum
LET2 = "YTBEDMHRP"  # 9 letters after K -> Y collapse
A2_FIXED, B2, S2 = "YTBBEDMYD", "YHDBTYYDD", "EDYTERTPTY"
sols2 = []
for perm in permutations(range(10), len(LET2)):
    m = dict(zip(LET2, perm))
    if any(m[ch] == 0 for ch in (A2_FIXED[0], B2[0], S2[0])):
        continue
    if (letters_to_int(A2_FIXED, m) + letters_to_int(B2, m)
            == letters_to_int(S2, m)):
        sols2.append(m)
        break  # unique
m2 = sols2[0]
print("equation 2:", letters_to_int(A2_FIXED, m2), "+",
      letters_to_int(B2, m2), "=", letters_to_int(S2, m2),
      "(corrected; original K at position 8 should be Y)")

For equation 11 the search returns the unique solution 6247663+6837633=130852966\,247\,663 + 6\,837\,633 = 13\,085\,296. For equation 22 the corrected letter pattern YTBBEDMYD+YHDBTYYDD=EDYTERTPTY\mathtt{YTBBEDMYD} + \mathtt{YHDBTYYDD} = \mathtt{EDYTERTPTY} admits the unique assignment 695513263+673596633=1369109896695\,513\,263 + 673\,596\,633 = 1\,369\,109\,896; the original K\mathtt{K} in position 88 of the first number should have been Y\mathtt{Y}.