Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Golf: Movie style code cracker.

by BrowserUk (Pope)
on Sep 12, 2008 at 15:10 UTC ( #710927=perlmeditation: print w/ replies, xml ) Need Help??

You see it in the movies all the time. The good/bad guy plugs a (large or small, depending upon the age of the movie), device into the safe/door lock/ATM/whatever and the LCD/LED/NIXIE tube display rolls through the 6/8/10 digit password/pin blurringly fast and, one by one, the digits become static as the device 'cracks' the code.

So, supplied with a command line argument, D, indicating the number of (numeric) digits, code a routine that picks (fairly) a random code, C, in the range 0 .. 1eD -1, and then scrolls the display (sequentially or randomly), until the first digit (left most) matches the first digit of C. Then scrolls the remaining digits until second digit matches the second digit of C; and so on until the randomly selected code is completely matched.

Rules: every character used, including command line (excepting "perl" and any preceding path), but including all switches and other CLI syntax; and every character of any non-pre-existing (as of midnight 11th September 2008) CPAN/core modules. Lowest character count wins.

(+10 for any solution that runs more than 2.5 minutes (average time for a 'tense' movie scene (guestimate:)), for a 10 digit code.)


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.

Comment on Golf: Movie style code cracker.
Re: Golf: Movie style code cracker.
by kyle (Abbot) on Sep 12, 2008 at 16:33 UTC

    OK, I'll bite. 139 characters.

    It does not take 2.5 minutes to run. On my machine, it's less than .1 sec, actually. Perhaps a slow motion camera or digital special effects department could compensate.

Re: Golf: Movie style code cracker.
by mr_mischief (Monsignor) on Sep 12, 2008 at 16:52 UTC
    I'm sure it will be beat, but here's a first stab at it with 137 characters. It doesn't do any fancy screen control to keep it all on the same line, which some movie versions probably would.

    Consider it 125 characters if you don't want to see the random code for reference. As a middle ground, how about 128 for seeing the original on every line?

    I've always wondered how many people notice that it's smarter to check the whole passcode as a unit than to be able to short circuit each character match. I guess the tension doesn't quite build the same when the WOPR just keeps failing as when the camera shows it getting closer, though. Joshua even had the advantage of scrolling all the digits simultaneously and locking each in separately from those before it in the code. At least they made the end of the world base 36.

    Update: added spoiler tags

    Update 2008-09-12, 18:27 UTC:
    How about 122 for seeing the original on every line and 119 for never seeing it?

      Changes from your 2008-09-12, 18:27 UTC update, plus find $m the way I did, plus a few other little changes...108 strokes.

Re: Golf: Movie style code cracker.
by duelafn (Priest) on Sep 12, 2008 at 17:24 UTC

    122, but the true "movie version" is longer

    Update: make movie version spin wheels more to break 2.5min bonus point time.

    Good Day,
        Dean

      I know BrowserUK stated "including all switches and other CLI syntax", but I'm not sure that includes the arguments to the program itself and the space before it. Those are constants other than the length of the argument. Perhaps we need that clarified.

      If that really counts we can trim one character just by making it 6 or 8 digits instead of 10 or 16. If not, you're down to 119.

      While we're clarifying minor rule variations, mine doesn't use any command-line switches to perl, so I was counting on it being in a file and not counting the ' -e'. Do I then need to count the '#!' or the name of the file? If so, just run perl and paste my solution into STDIN. ;-) Do I then need to count the ctrl-D for end of file?

      It took me a while to work out how to speed it up so I could check it would produce all the digits 0..9. I love the use of statistics to control the matching speed. '1111'==r.r.r.r is soo cool.

      You can gain a couple of strokes by dropping the prototype on the sub.


      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.
Re: Golf: Movie style code cracker.
by atcroft (Monsignor) on Sep 12, 2008 at 17:50 UTC
    Would 115 characters (123 characters, including the 'perl ' and ' 10') be good?

    Update: (2008-09-12) In haste I forgot about the following in the description: "...then scrolls the display (sequentially or randomly), until the first digit (left most) matches the first digit of C. Then scrolls the remaining digits until second digit matches the second digit of C; and so on until the randomly selected code is completely matched." My solution does not accomplish this, but acts as a simple odometer instead. :(

Re: Golf: Movie style code cracker.
by blokhead (Monsignor) on Sep 12, 2008 at 19:24 UTC
    Fun. Here's my interpretation of what a scrolling hacker toy would look like... It's 90 characters.

    You could make it run for >2.5 minutes by ...

    Update: Here is another one that fills in the fixed positions not left-to-right, but randomly, to look more Matrix-like. As it gets more and more characters fixed, it slows down to increase the drama (this is me trying to sell a design inefficiency as a feature). It's 124 characters.

    On my machine, it looks nice doing 20-digit combinations.

    blokhead

      You're not matching the randomly chosen code C. On your way to decipher C, you would have likely iterated through the "correct" left-digits of C a few times before "locking" in.
Re: Golf: Movie style code cracker.
by zentara (Archbishop) on Sep 12, 2008 at 19:47 UTC
    I didn't see anything about no Tk. :-) I don't care about golf, I want it to look realistic as in a movie. Jason Bourne uses this because it looks cool! :-)
    #!/usr/bin/perl use warnings; use strict; use Tk; my $dig_num = shift || 7; my $bignum = 10**$dig_num; #print "$bignum\n"; my $rand = int rand $bignum; my @coded = $rand =~ /\d{1}/g; print "@coded\n"; my @slots = ( 1 .. scalar @coded ); my $mw = MainWindow->new; $mw->title("$rand"); $mw->fontCreate( 'big', -family => 'arial', -size => 38 ); my %lab; foreach my $num ( @slots ) { $lab{ $num }{ 'count' } = 0; $lab{ $num }{ 'done' } = 0; $lab{ $num }{ 'label' } = $mw->Label( -bg => 'black', -fg => 'green', -font => 'big', -textvariable => \$lab{ $num }{ 'count' }, )->pack( -side => 'left', -fill => 'x', -expand => 1 ); } $mw->Button( -text => 'Go', -command => \&click )->pack(); MainLoop; sub click { my $watch = shift @coded; my $sel = 1; # print "$watch $sel\n"; my $timer; $timer = $mw->repeat( 10, sub { foreach my $num ( @slots ) { if ( $lab{ $num }{ 'done' } == 1 ) { next } else { $lab{ $num }{ 'count' } = int rand 10; } if ( $lab{ $sel }{ 'count' } == $watch ) { $lab{ $sel }{ 'done' } = 1; $watch = shift @coded; if( ! defined $watch ){$timer->cancel} else{ $sel++; #print "$watch $sel\n"; } } } } ); } __END__

    I'm not really a human, but I play one on earth Remember How Lucky You Are
Re: Golf: Movie style code cracker.
by toolic (Chancellor) on Sep 12, 2008 at 20:01 UTC
Re: Golf: Movie style code cracker.
by w-ber (Hermit) on Sep 12, 2008 at 20:44 UTC

    124 characters, with the following features:

    • Digits are displayed on a single line instead of scrolling the screen. (Tested only on Linux with bash.)
    • Execution takes anything between $D and $D*10 seconds to run, when using base 10. Thus, if $D == 10, the program may run for as long as 100 seconds or 1 m 40 s. $D == 15 will break the "bonus barrier".

    --
    say "Just Another Perl Hacker";

Re: Golf: Movie style code cracker.
by shmem (Canon) on Sep 12, 2008 at 23:34 UTC
    (+10 for any solution that runs more than 2.5 minutes (average time for a 'tense' movie scene (guestimate:)), for a 10 digit code.)

    Uh. Two and a half minutes for guessing a 10 digit code? That's a challenge for the book writer. If I had a television, I would start zapping. In the cinema, I'd leave for the toilet, and get a beer. Anyways, 86 strokes.

      Just throw in a ninja battle, 2.5 minutes will go by in no time!

      --
      I'd like to be able to assign to an luser

      I like your 110 stroke version. Though I'd have to penalise for never producing a '9' in the final code.

      However, on my system at least, you could trade the extra stroke of rand 10 for ommitting the final print$/ as the system supplies one when the program ends.


      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.
Re: Golf: Movie style code cracker.(My attempt:77)
by BrowserUk (Pope) on Sep 13, 2008 at 08:49 UTC
Re: Golf: Movie style code cracker.
by Anonymous Monk on Sep 13, 2008 at 10:29 UTC
    What is the range (0 .. ???-1) ?

      This (from the OP):

      So, supplied with a command line argument, D, indicating the number of (numeric) digits, code a routine that picks (fairly) a random code, C, in the range 0 .. 1eD -1,

      Means that if the command line argument (D) is 4, then the range is 0 .. 1e4-1. Ie 0 .. 9999

      For D = 10, then the range is 0 .. 1e10 -1. Ie. 0 .. 9999999999


      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.
        Thanks. In the op you have d not D, so it confused.
Re: Golf: Movie style code cracker.
by hypknotizzed (Beadle) on Sep 13, 2008 at 14:50 UTC
    I came up with a poor 257, but it scrolls realistically... Oh well.
    $d=10;$c;for(1..$d){$c=$c.int(rand(10));}print"$c\n";$g;$w='';$m=280;$ +t=$m;$|=1;do{$g='';for(1..$d){$g=$g.int(rand(10));}print"$w$g\r";if(s +ubstr($g,0,1)eq substr($c,0,1)){if($t>0){$t--;}else{$w=$w.substr($c,0 +,1);substr($c,0,1)='';$d=$d- 1;$t=$m;}}}until($d==0)
    Update: Got some tips from Albannach and got down to 204, but I'm spending characters for nice appearance:
    $d=shift;for(1..$d){$c.=int(rand(10))}$t=$m=320;$|=1;while($d){$g='';f +or(1..$d){$g.=int(rand(10))}print"$w$g\r";if(substr($g,0,1)eq substr( +$c,0,1)){if($t){$t--;}else{$w.=substr($c,0,1,'');$d--;$t=$m;}}}

    hypknotizzed

Re: Golf: Movie style code cracker.
by Jenda (Abbot) on Sep 14, 2008 at 01:58 UTC
      It scrolls very fast on my comp.

      Very fast. But you aren't going to be able to build much tension in 5 milliseconds, and no one is going to believe the bad guy remembers a 500 digit pin :)


      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.
Re: Golf: Movie style code cracker.
by repellent (Priest) on Sep 14, 2008 at 02:16 UTC
    Here's my entry at 105 characters:

    A check for correctness would be to print out each sequence on its own line and look for a triangle effect:
    axxxx axxxx axxxx 1bxxx 1bxxx 1bxxx 12cxx 12cxx 12cxx 123dx 123dx 123dx 1234e 1234e 1234e 12345 12345 where a != 1, b != 2, c != 3, d != 4, e != 5 x = can be any digit

      A problem is that this always produces the same code: 4294967295?

      Which would be good for movie continuity I suppose, but the nerds would pick up on the number and complain bitterly :)


      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.
        Works for me. The inconsistency is most probably caused by non-portable code:
        (~0 & $a) != int($a)

        Fine! I've updated my entry by one extra character :)
Re: Golf: Movie style code cracker.
by betterworld (Deacon) on Sep 14, 2008 at 15:44 UTC
    code a routine that picks (fairly) a random code, C, in the range 0 .. 1eD -1, and then scrolls the display (sequentially or randomly), until the first digit (left most) matches the first digit of C.

    Does this mean that the implementation must actually choose C before displaying it digit by digit? Or would it (theoretically) be okay if the digits were chosen as they are being displayed (i.e. the second digit is not known to the program before the first has been shown)?

      Does this mean that the implementation must actually choose C before displaying it digit by digit? Or would it (theoretically) be okay if the digits were chosen as they are being displayed (i.e. the second digit is not known to the program before the first has been shown)?

      As my own attempt uses on the latter approach, I have to say it is acceptable. Others are free to disagree and discount my attempt. Here is how I justify it. Istarted out with this to fulfill the 'spec':

      #! perl -slw use strict; $|++; sub r{int rand pop} my $n = pop; my @c = map int rand 10,1..$n; my @x = (0) x $n; for my $d ( 0 .. $n -1 ){ map{ $x[ $d + r( $n - $d ) ] = r( 10 ); printf "\r@x"; $x[ $d ] = $c[ $d ] }1..1e5 }

      I then started golfing and here are the steps I went through:

      -e"sub r{int rand(pop||10)};@c=map{r}1..($n=pop);for$d(0..$n-1){map{$x +[$d+r($n-$d)]=r,print qq[@x\r]}1..1e5;$x[$d]=$c[$d]}" -e"sub r{int rand(pop||10)};$n=pop;@c=map{r}1..$n;for$d(0..$n-1){map{$ +x[$d+r($n-$d)]=r,print qq[@x\r]}1..1e5;$x[$d]=$c[$d]}" -e"$n=pop;for$d(0..$n-1){map{$x[$d+rand($n-$d)]=int(rand 10),print qq[ +@x\r]}1..1e5;}" -e"for$d(0..($n=pop)-1){map{$x[$d+rand($n-$d)]=int(rand 10),print qq[@ +x\r]}1..1e5}" -l15e"for$d(0..($n=pop)-1){map{$x[$d+rand($n-$d)]=int(rand 10),print @ +x}1..1e5}" -l15e"for$d(0..($n=pop)-1){$x[$d+rand($n-$d)]=int(rand 10),print@x,for ++1..1e5}" -l15e"for$d(0..($n=pop)-1){$x[$d+rand($n-$d)]=rand(10)&15,print@x,for+ +1..1e5}" -l15e"for$d(0..($n=pop)-1){$x[$d+rand$n-$d]=rand(10)&15,print@x,for+1. +.1e5}"

      At the transition from step 2 to step 3, I notice that I had these 3 pieces of code:

      1. @c=map{r}1..$n;

        Put random digits into @c.

      2. $x[$d+r($n-$d)]=r

        put a random digits into $x[...].

      3. $x[$d]=$c[$d]

        replace the random digit in @x with corresponding (random digit) from @c.

      Which is equivalent to $tmp = val; $target = other value; $target = $tmp.

      Eliminating the temporary value and intermediate steps is the essence of golf, so I conclude that step 3 above is logically equivalent to step 2. Others may wish they had thought of it...or condemn it as a step too far?


      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.
Re: Golf: Movie style code cracker.
by betterworld (Deacon) on Sep 14, 2008 at 19:34 UTC
    108 characters:

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

      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.

Re: Golf: Movie style code cracker.
by ambrus (Abbot) on Sep 15, 2008 at 06:17 UTC

    Do you measure the 2.5 minutes in a slow terminal like gnome-terminal (with an xft font) or a really fast one like linux console in text mode? For some reason I think the speed of printing the numbers will take the most time in this golf.

      Do you measure the 2.5 minutes in a slow terminal like gnome-terminal (with an xft font) or a really fast one like linux console in text mode?

      On my command line! :)

      But seriously, people that attempt to comply with this, generally had a constant that could be adjusted (from 1e1 to 1e9) without compromising their stroke count.


      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: perlmeditation [id://710927]
Approved by toolic
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (14)
As of 2014-09-02 21:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite cookbook is:










    Results (30 votes), past polls