There's a simple digital number lock. After each button press the lock checks if the previous four presses constitute the correct code. I.e. typing 44445 tests two codes: 4444 and 4445. You must brute force it with a code golfed program.


  • Your program must test numbers by printing ASCII digits 0-9 to standard output. Other output, if any, will be ignored.
  • The program must be deterministic. If you need "random" numbers, you must use a deterministic generator with a well defined seed.
  • Infinite output beyond all 10^4 codes is allowed. Opening the lock is considered to stop the program.


  • Calculate the average digits of output needed to crack the lock. I.e. search the output string for every possible four digit sequence and record where it first appears.
  • Add the source code length in bytes. The program must be fully functional, you are not allowed to omit imports or other boilerplate.

Score = Average digits to crack + codelength in bytes

Example entry

Python, 35 bytes.

for i in range(9001):print '%.4d'%i

Score: 8444.9837 + 35 = 8479.9837


Simple, stupid Python program to verify score of an output string:

d = {}
s = ''.join(c for c in s if '0' <= c <= '9')
for i in range(len(s)-4):
    t = s[i:i+4]
    if t in d:
    d[t] = i + 4
    if len(d) == 10000:
print sum(d.values()) / 10000.
  • \$\begingroup\$ The example uses range(9000), how does it print lock with for example code: 9999? \$\endgroup\$ Sep 15, 2014 at 12:30
  • \$\begingroup\$ @RoyvanRijn, whoops, typo. Should be fixed. \$\endgroup\$
    – fhyqrkka
    Sep 15, 2014 at 12:37
  • \$\begingroup\$ Good concept but the scoring needs to be clarified. If you want to use a mixed scoring system, code-golf and fastest-algorithm tags are redundant, just leave code challenge. Time in milliseconds, minutes or days? On whose machine? (presumably yours, which means you will have to test.) Note that bytestime is a lot less sensitive to machine speed than bytes+time. With bytestime running on a machine 10 times as slow will multiply everyone's score by 10 without changing the order of the scores but bytes+time will favour long fast programs on a slow machine and short slow ones on a fast machine. \$\endgroup\$ Sep 15, 2014 at 13:05
  • 1
    \$\begingroup\$ @RoyvanRijn Ok, on re-reading it, you're right. It's asking for the shortest sequence. I believe optimal sequences are possible, which makes it very close to this codegolf.stackexchange.com/q/13088/15599 \$\endgroup\$ Sep 15, 2014 at 13:14
  • 1
    \$\begingroup\$ This is basically a fight to get all sequences in minimal length of both code and sequence. Exactly what asked in the other question. \$\endgroup\$
    – Optimizer
    Sep 15, 2014 at 13:36

2 Answers 2


Java 5218

This is the (golfed) code:

class a{int[]a=new int[5];public static void main(String[]a){new a().b(1,1);}void b(int t,int p){if(t>4)for(t=0;++t<=p&&4%p==0;)System.out.print(a[t]);else{a[t]=a[t-p];b(t+1,p);for(a[t]=a[t-p];++a[t]<=9;)b(t+1,t);}}}

It prints a specially crafted sequence to stdout. The completed sequence has all combinations, only once, in the shortest way possible. The total length is 10.000 with the first combination after 4 (obviously) and the last after 10.000. So if I understand the challenge correctly my score is:

4 + ((10000-4)/2) = 5002 avg length to break the code
5002 + 216 (code length) = 5218

Obviously this score will be improved (Java is pretty verbose), so I'm not going to tell you the name of the sequence I'm using...

  • 3
    \$\begingroup\$ You mean De Brujin sequence? :D \$\endgroup\$
    – yasen
    Sep 15, 2014 at 12:45

Haskell, 120 bytes + 5001.5 = 5121.5

import Data.List
main=print$(nub$sort[sort s|x<-[1,2,5,10],s<-sequence$replicate x['0'..'9'],any(/=s!!0)s||x<2])>>=id++"000"

Not the answer you're looking for? Browse other questions tagged or ask your own question.