A few days ago, I had to do a "rescue intervention" (legal and needed). This required me to bypass a door code. Luckily, the keypad was worn and you could clearly see that only two numbers where used, so it was a quick thing to brute force the 14 possible combinations.
However, I noticed that the lock did not require four numbers, then pause, then the next four numbers. As long as you pressed the four numbers in correct order, regardless of what you had pressed before, it worked.
Now, the programmer in me hasn't been able to drop this. How do one generate a sequence that contains all possible combinations in as few keypresses as possible.
For the example above, a short sequence which works (assuming the numbers are 0 and 1), is:
10001110010110100
This sequence contains all the possible combinations in as few keypresses as possible.
However, that was done manually, with some trial and error to avoid repetitions or numbers which were not used in an optimal number of combinations. There are some traps which would cause it to be suboptimal, for instance, if I had started with 0001, I would have had to duplicate three zeros elsewhere to be able to get 1000, and they are not needed in any other combination.
Also, say that three numbers are used? Four? If the code is 5 or 6 digits long?
Can you come up some kind of generic algorithm that can generate a sequence that is the shortest possible sequence, given the following inputs:
* Used numbers (in the example above, 0 and 1)
* Length of code (in the example above, 4)
I can fairly easily do one which gives a sequence that works and is fairly short, but I can't guarantee it will be the shortest possible sequence.
Hint: the shortest sequence should be this long:
Possible_combinations + Length_of_code -1
There are several possible solutions in each case, but as long as one of them is found, it's good enough.
Do you dare to accept the challenge?
However, I noticed that the lock did not require four numbers, then pause, then the next four numbers. As long as you pressed the four numbers in correct order, regardless of what you had pressed before, it worked.
Now, the programmer in me hasn't been able to drop this. How do one generate a sequence that contains all possible combinations in as few keypresses as possible.
For the example above, a short sequence which works (assuming the numbers are 0 and 1), is:
10001110010110100
This sequence contains all the possible combinations in as few keypresses as possible.
However, that was done manually, with some trial and error to avoid repetitions or numbers which were not used in an optimal number of combinations. There are some traps which would cause it to be suboptimal, for instance, if I had started with 0001, I would have had to duplicate three zeros elsewhere to be able to get 1000, and they are not needed in any other combination.
Also, say that three numbers are used? Four? If the code is 5 or 6 digits long?
Can you come up some kind of generic algorithm that can generate a sequence that is the shortest possible sequence, given the following inputs:
* Used numbers (in the example above, 0 and 1)
* Length of code (in the example above, 4)
I can fairly easily do one which gives a sequence that works and is fairly short, but I can't guarantee it will be the shortest possible sequence.
Hint: the shortest sequence should be this long:
Possible_combinations + Length_of_code -1
There are several possible solutions in each case, but as long as one of them is found, it's good enough.
Do you dare to accept the challenge?