# Rolling digital number lock [duplicate]

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.

## Rules

• 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.

## Scoring

• 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

## Other

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:
continue
d[t] = i + 4
if len(d) == 10000:
break
print sum(d.values()) / 10000.

• The example uses range(9000), how does it print lock with for example code: 9999? Sep 15, 2014 at 12:30
• @RoyvanRijn, whoops, typo. Should be fixed. Sep 15, 2014 at 12:37
• 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. Sep 15, 2014 at 13:05
• @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 Sep 15, 2014 at 13:14
• This is basically a fight to get all sequences in minimal length of both code and sequence. Exactly what asked in the other question. Sep 15, 2014 at 13:36

# 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...

• You mean De Brujin sequence? :D 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"