[Community Puzzle] Entry code

Coding Games and Programming Challenges to Code Better

Send your feedback or ask for help here!

Created by @Eniidras,validated by @Snef,@LittleDarkBug and @Westicles.
If you have any issues, feel free to ping them.

In case anyone is interested in some of the math behind this puzzle, Ian Stewart wrote an article for “Pour la Science” (the French translation of “Scientific American”, mostly) with the (English) title “The Autovoracious Ourotorus”. You can find it republished in Stewart’s Game, Set and Math (1989) where he talks about an idea that connects:

  • A mythical serpent of ancient Egypt.
  • An alchemical symbol.
  • Kekule’s discovery of the benzene ring.
  • An Indian theory of rhythm, a thousand years old.
  • The seven bridges of Konigsberg.
  • The theory of telephone circuits.
  • Radar maps of Venus.

Not bad, for 12 1/2 pages written for nonprofessionals, huh?


Thank you, I already solved it but now I think I can do a better code

Good puzzle. But for existing constraints it’s simple to solve by “brute force”.
In my opinion, for biggest ‘x’ and ‘n’ (when the list of all combinations is large for creating and keeping it in memory) we should build solution-tree and get answer after pass the tree. It will be more harder.

1 Like

Does it always have to be harder?

PS: I know, I know. “That’s what she said” :wink:


I generate a list of all possible codes, then using backtracking to generate all possible sequences, then find the min sequence. During backtracking, I cut off the code which add more than 1 to the length.
Appearently, this algo is not performante enough. I got only the 2 first simple cases pass. Do you have any hints on how can I improve. Thank you.


1 Like


I don’t know if you have any fundation in graph theory, but this problem can be re-written as finding a euclidian path within a directed graph. I won’t give you all the answers, so for now, you are going to have to figure what the nodes and edges would be.

Maybe a small hint, though: each node is of the same degree (both in and out) as the number of keys on the keypad.

Finally, what would happen with a greedy algorithm?

If you want to strictly work off of your code, look at the solutions you find. What are their lengths?

You will find that they form circular sequences. This means that you can fix the start of the sequence (To what should you fix them to have the smallest number overall?), and remove a lot of symetry within the problem.

Please keep in mind that the numbers of solutions possible can be prohibitively big.

With a two digit code and 2 possible values, the possible codes are 00, 01, 10 and 11.
The optimal chain is 00110.
By removing the symmetry and forcing to start with 00, you avoid the following sequences: 01100, 11001, 10011.
With a 3 digit code, you have the optimal solution as 0001011100.
Removing the symmetry avoids: 0010111000, 0101110001, 1011100010, 0111000101, 1110001011, 1100010111, 1000101110.
but you also have solutions changing the order in which codes are explored:
0001110010, for example. Can you see it being similar to another one above?
How could you avoid going through it as a potential solution? Could you do that while constructing the solution?

Hope this helps!


Thank you VCO-NSide.
I’m new to graph theory, I’ll do some study on euclidian path.
My issue with the backtracking was that the search space is too big. For d = 2, n = 3 (d: digit code, n: length), there are 8 codes, and the total search space is 8! = 40320.
With elderlybeginner’s hint, I was able to reduce the search space a lot so that the test cases were pass. What I changed is that in my backtracking algo, I just need to find one sequence rather than all possible sequence. And I need to make sure this sequence is the smallest one.
To make the sequence shortest, I suppose that the length of the answer should be (n + d^n - 1) (d: digit code, n: length) which means that every time when I append a new code to sequence, the length should be added only 1.
To make the sequence the smallest, I ordered the codes by its value.
Anyway, I’ll try your proposition about rethink the problem as a graph

Yeah, we could make this problem more difficult, but it was my first contribution, so I just wanted to publish a simple puzzle so I was constrained by an unique answer with 1000 or less characters. For me it was complicated enough to be interesting. And in my opinion, you still need quite a clever “brute force” solution to pass all the tests.

Thanks for the feedback !


Bruteforce was way to slow for me.
I don’t see graphs there either.
It’s solvable within a few lines with product and string manipulation. Just find the pattern.

1 Like

In my experience, graphs were what I implemented in Java (with a full explanation of what and how), and what enabled me to get one property based on the algorithm I used for euclidian paths… to then get the full solution in python within a few lines of code.

If I could submit two java solutions, I’d submit a version of my python one, as it is much simpler and more elegant, however I’m afraid it might be hard for reviewers to see why it works.

Honestly, I might anyways rewrite my java code for a more elegant version, getting rid of the background graphs and only keeping the main part.

Yeah. You are right. It wasn’t a criticism, just some thoughts. Anyway it’s interesting medium (not easy!) puzzle. Thanks!

1 Like

You don’t have to compute all the possible sequence : there is a simple way for your backtracking to directly generate the smallest one.
You may also assume that a smallest sequence always begin by ‘‘00…0’’.

I would like to share some thoughts. So far I have been like Salanger, obtaining the first two tests using an algorithm that was near bruteforcing.

My intuition is that for the algorithm to run in a decent amount of time, you have to make it pretty simple. And when speaking of a way to eliminate some cases, that would not be a small amount or even half. The algorithm has to eliminates 95% of the bad cases to reach the correct solution in the allowed time.

The requirement to find the lowest possible number is probably the biggest hint if I am correct

Another thing that bugged me was that I could not figure in the simple cases how a human would do using its brain to reach the correct solution. But I think I may have found out what a human would do for the “2 3” example.

EDIT: I have found out that my algorithm is wrong (but not too far). I am still hiding it just in case

  1. list all combinations: 000 001 010 011 100 101 110 111
  2. start with the lower one as your solution string: 000
  3. Notice that the combinations are naturally sorted from lowest to biggest
  4. go through all combinations and for each
    4.a) check if the combination is already in your solution string and if yes, skip to the next
    4.b) if the combination is not here yet, add the required digit to make it be there. For example for “001” to be in “000” you just have to add a “1” and it becomes “0001”.

with this algorithm, you will reach the result of 000101100111. Which is not correct, but not so far.

My next guess is that the “greedy algorithm” stuff that VCO-NSide mentioned earlier might finally takes some sense when I think how I could rework the algorithm to make it work

All solutions that I have tested with Python, including mine, fail rapidly, for example if there are 10 different digits and the length of the code is 3 or more. Code error :
"RecursionError: maximum recursion depth exceeded in comparison"
How would it be possible to go round this problem ?

Try sys.setrecursionlimit(100_000), default is 1000.

1 Like

Thank you for the tip! Now, I’m getting this error:

Segmentation fault (core dumped)
1 Like

For a period of time I was unable to understand this puzzle because it mentions “entry code”.

It assumes if the entry code is 234, and if one presses “1234”, the door will unlock.
Where in the world have a security system as foolish as this one?

In real life, people have to press “234” and then “Enter” to unlock.
“12345” will not unlock.
“1234” + “Enter” will not unlock neither.

“Finding a shortest string that contains all possibile combinations” is good enough to define the problem.
The first paragraph added no value but difficulties in understanding, imho.

I don’t understand. why the length of the sequence for the example with x = 2 & y = 3 is equal to 10?