Home > Riddles > The door that it opens sola

The door that it opens sola

Master playsLevel 3/5

The best thing about The door that opens by itself is that it seems to go one way and resolves another. That little twist is what turns a correct problem into a memorable puzzle.

A safe uses a 4-digit code, from 0000 to 9999. You can type an arbitrarily long sequence of digits.

The box opens as soon as the last four digits typed match the code, even if you don't know when this will happen. What is the minimum length of a sequence that guarantees opening the box?

Hints

Show hints
  1. You don't need to test separate codes; you can write a single long sequence.
  2. What matters is that every 4-digit key ever appears as a consecutive block.
  3. You are looking for a Of Bruijn sequence for decimal alphabet and length 4.

Solution

Show full solution

Answer: minimum 10003 digits. Lower level:
A string of length $L$ contains at most $L-3$ distinct substrings of length 4.
To cover the 10000 codes:

$$ L-3\ge 10000\Rightarrow L\ge 10003. $$ **Upper level (construction):** There is a Of Bruijn sequence $B(10,4)$, cyclic, of length 10000, containing each 4-digit block exactly once. By linearizing it and adding the first 3 digits at the end, you get a string of: $$

10000+3=10003

$$ which covers all 4-digit codes. Since the lower and upper bounds matches, the exact minimum is 10003. $$

Related riddles

Keep practicing

If you enjoyed this one, try more pure-logic riddles, explore this theme, browse the full archive, or read the riddle-solving guide.

← Previous: The desert expedition · Next: The last passenger →