Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Re: Golf: Movie style code cracker.

by betterworld (Curate)
on Sep 14, 2008 at 19:34 UTC ( #711315=note: print w/replies, xml ) Need Help??


in reply to Golf: Movie style code cracker.

108 characters:
$_=1x($l=pop);for$i(0..$l-1){($_=reverse)++for substr$_,$i;s/(.{$l}).+ +/$1/;print"\r$_";1e-5<rand&&redo}

This runs about 4 minutes for a 10 digit code. Change 1e-5 to 1e-4 if you want it to finish in 25 seconds.

Actually you can replace ($_=reverse)++ with $_++, which would reduce the length to 93 characters, but then the first digits will spin awfully slow (for a 10 digit code), and they will probably end up as ones.

Update: Adopted the required counting method, thus I have 108 characters, not 103.

Update 2: New version with 71 characters (however it would be a few (about 4) characters more if I hadn't copied a trick from BrowserUk; I'll elaborate in the spoiler tag):

Caveat: The second version will probably not terminate for a 1-digit code.

Update 3: :-( I just noticed that my 71 character solution has the problem that it always prints "8" as the last digit. For the moment, I offer the following fix, which makes it 75 characters long (and I'll omit the spoiler tags as the contest seems to be over):

-l15e'print|substr$_||=1x($l=pop),~rand$l,1,~~rand 10 while++$A%1e6||- +-$l'

(And you can modify the 1e6 constant to shorten the runtime.)

Replies are listed 'Best First'.
Re^2: Golf: Movie style code cracker. (Updated)
by BrowserUk (Pope) on Sep 15, 2008 at 02:37 UTC

    Update: I was too quick in awarding you the prize :( The code is not chosen fairly!

    For a code length of 3, the chosen code will rarely contain a digit < 7. For a code length of 4, it will rarely contain a digit < 6; and so on.

    It's not until you get to a code length 10, that all the digits are possible, even then (on the basis of quick tests), the higher digits will dominate choices. I don't think this is true for any other submission, though I cannot say I have checked them all scrupiously.


    I declare your second attempt the winner beating my attempt by 6, and being eligable for the bonus of 10 for running for a tension building amount of time.

    Originally, the 'bonus' was conceived as a 'penalty' (hence the +10 in a competition that is smallest wins), intended to penalise horribly slow solutions. But as everybody took it as a requirement to slow things down, and that actually turned out to be the harder (more stroke costly) thing to achieve, I let the misunderstanding stand.

    Hence, I think your second attempt has a logical score of 61, though maybe you should take a penalty for never terminating for a code length 1? Others will have to decide that. In any case, well done++


    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
      For a code length of 3, the chosen code will rarely contain a digit < 7. For a code length of 4, it will rarely contain a digit < 6; and so on.

      Unfortunately I've just noticed a problem myself, please see my third update above. (I've written it after I have read your note, before I've read your update.)

      I think that the third attempt also fixes the problems that you describe.

      I seem to have payed too little attention to the output, especially for small codes, because the ensuing command prompt always hides the output (because \r sets the cursor to the beginning of the line). Placing an "; echo" after the command in a unix shell helps with this issue.

      Just assume the door has a keypad with only six or eight buttons. I think that could be done in a movie.

        I thought I had that covered by:

        indicating the number of (numeric) digits

        But I see that is at least not fully specified, if not ambiguous. It is hard to specify golf completely unabliguously.


        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        "Science is about questioning the status quo. Questioning authority".
        In the absence of evidence, opinion is indistinguishable from prejudice.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://711315]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (5)
As of 2020-12-04 05:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    How often do you use taint mode?





    Results (58 votes). Check out past polls.

    Notices?