Chapter 168
Inside The Cube And The Three-Lock Safe
Riddler Express
Take a unit cube. One face diagonal runs from to . A second diagonal runs from to . Find the coordinates of the two endpoints of the shortest segment bridging the gap between these two skew diagonals.
The Riddler, FiveThirtyEight, December 1, 2017(original post)
Solution
Parametrise points on the two diagonals.
The squared distance is Setting and gives Both values sit strictly inside , so the optimum is interior to each diagonal. The endpoints are and the minimum length is
Why this is the global minimum. The function is a positive-definite quadratic in (its Hessian is the diagonal matrix ), so the critical point is its unique global minimum. The fact that the optimum lies strictly inside both parameter intervals confirms that the segment between and is the common perpendicular of the two lines, and its length is the standard skew-line distance.
The computation
Solve the optimisation symbolically with sympy; this independently re-encodes the geometric problem (two parametrised lines, the squared-distance functional, and its minimum) rather than the closed-form derivation above.
Define the parameter variables and the two parametrised points.
Form the squared-distance function.
Solve the system .
Evaluate the endpoints and the distance.
import sympy as sp
x, y = sp.symbols('x y', real=True)
P = sp.Matrix([0, y, y]) # first diagonal
Q = sp.Matrix([x, 1 - x, x]) # second diagonal
D2 = sum((P - Q).T * (P - Q))
sol = sp.solve([sp.diff(D2, x), sp.diff(D2, y)], [x, y])
ep1 = P.subs(sol)
ep2 = Q.subs(sol)
dist = sp.sqrt(D2.subs(sol))
print(ep1.T, ep2.T, sp.simplify(dist)) # (0,1/2,1/2) (1/3,2/3,1/3) sqrt(6)/6
Sympy returns the same endpoints and minimum distance .
Riddler Classic
A safe has three locks (1, 2, 3) and three key cards (A, B, C). Each lock is opened by a unique card; you do not know which card belongs to which lock. You insert the three cards into the three slots in some order and press the attempt button. Each lock whose correct card was inserted at the moment of the press flips state (locked unlocked); the other locks do not change. The safe opens when all three are unlocked, and you cannot tell partial progress. What is the minimum number of attempts that guarantees opening the safe, and what sequence works?
The Riddler, FiveThirtyEight, December 1, 2017(original post)
Solution
The answer is attempts.
Reframing. The catch is the double hidden information: the initial lock states (an unknown element of ) and the unknown card-to-lock bijection . A press is a permutation of the cards into the three slots. Under labelling , lock flips at this press iff . So a single press performs different lock-flip operations under different ’s. The six possible ’s and six press permutations give a table of lock-flip patterns; for any fixed , the six presses produce one identity-permutation (), three single-fixed-point permutations (), and two derangement permutations (, no flips). Crucially, the press that achieves depends on .
A strategy is a fixed sequence of press permutations chosen before you know or the initial state; it succeeds if, for every one of the hidden configurations, some prefix of the sequence drives the locks to all-open.
A combinatorial bound. For each fixed , the sequence of presses induces a walk of steps in (starting at , jumping by a flip pattern per press); it must reach every one of the states at some prefix XOR, to handle every initial state under that . Across the different ’s the walks share the same press sequence but interpret it differently, and the coupling is what makes presses (the bare-minimum to enumerate ) insufficient. An exhaustive computer search (Igor Kopylov’s, cited in the official write-up) confirms that no strategy of length works for all configurations, and a strategy of length does:
ABC, ABC, BCA, CAB, ABC, BCA, ABC,
ACB, BAC, ACB, CBA, BAC, ACB.
The computation
Encode the puzzle’s two hidden variables ( and the initial state) and confirm the cited -press sequence opens the safe under every one of the hidden configurations.
Enumerate the six possible card-to-lock bijections .
For each and each of the nonzero initial states, walk the press sequence and find the first prefix where all three locks are unlocked.
Report success and the worst-case opening step across all configurations.
from itertools import permutations
def flip_pattern(press, f):
out = 0
for i in range(3):
if press[i] == f[i]:
out |= 1 << i
return out
seq = ['ABC','ABC','BCA','CAB','ABC','BCA','ABC',
'ACB','BAC','ACB','CBA','BAC','ACB']
worst = 0
for f in permutations('ABC'):
flips = [flip_pattern(p, f) for p in seq]
for s0 in range(1, 8):
s = s0
opened_at = None
for k, fl in enumerate(flips, 1):
s ^= fl
if s == 0:
opened_at = k
break
assert opened_at is not None
worst = max(worst, opened_at)
print("All 48 hidden configurations open within", worst, "presses")
The script confirms every hidden configuration opens within presses; the lower bound is the official exhaustive-search result of Igor Kopylov (FiveThirtyEight, 2017).