Chapter 290
How High Can You Count With Menorah Math?
The Riddler for December 11, 2020. The Express encodes nights on a menorah readable from either side, and the Classic finds a hat-guessing strategy that guarantees a survivor.
Riddler Express
A menorah has eight candle positions (besides the central shamash), each lit or unlit. You want to encode as many distinct “nights” as possible, but the menorah is viewed from both the front and the back, so a pattern and its left–right reversal must indicate the same night. What is the greatest number of nights you can represent?
The Riddler, FiveThirtyEight, December 11, 2020(original post)
Solution
Each of the positions is a bit, giving patterns. A front viewer sees a pattern ; a back viewer sees its reversal. To make the night unambiguous from either side, assign each night either a symmetric pattern (equal to its own reversal, so both sides agree) or a mirror pair of asymmetric patterns (front sees one, back sees the other, both decoding to the same night).
Symmetric patterns are fixed by their left half: of them. The remaining are asymmetric, forming mirror pairs. Each symmetric pattern and each mirror pair encodes one night, so distinguishable nights, far more than ternary pair-coding () and beating a naive palindromes-only scheme ().
The computation
Encode every -bit pattern, group each with its reversal, and count the distinct unordered groups.
groups = set()
for p in range(256):
bits = format(p, '08b')
rev = bits[::-1]
groups.add(frozenset({bits, rev})) # a symmetric pattern pairs with itself
print(len(groups)) # 136
The menorah can track nights.
Riddler Classic
Five citizens each get a red, green, or blue hat. They are split into a row of two () and a row of three (); each row sees the other row but not itself, and each knows their own position. With no communication, all guess their own hat colour simultaneously. What strategy guarantees at least one correct guess?
The Riddler, FiveThirtyEight, December 11, 2020(original post)
Solution
Code the colours and let the hat values be (row of two) and (row of three). The row of three sees ; the row of two sees . A guaranteed strategy:
guesses , guesses , guesses .
If any of is right, the group survives. So and may assume all three are wrong, which means , , and . Under those three constraints, with :
guesses if , else (mod );
guesses if , else (mod ).
Working through the three cases of , the two-to-four surviving possibilities are always covered by or , so whenever all fail, at least one of succeeds. Either way,
The computation
Encode the strategy and check all hat assignments: in every case at least one guess is right.
import itertools
def survives(a, b, c, d, e):
gC, gD, gE = a, b, (a + b) % 3 # row of three see A, B
s = (e - c - d) % 3
gA = (c + 1) % 3 if s in (0, 1) else (c + 2) % 3
gB = (d + 2) % 3 if s in (0, 2) else (d + 1) % 3
return gC == c or gD == d or gE == e or gA == a or gB == b
print(sum(survives(*t) for t in itertools.product(range(3), repeat=5))) # 243
All colourings survive, so the strategy is guaranteed.