Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Canadian Cryptography Contest

by jryan (Vicar)
on Jan 22, 2002 at 13:59 UTC ( [id://140630]=perlmeditation: print w/replies, xml ) Need Help??

Read the original story on slashdot here

Some Canadian college offered a computer science scholarship to anyone who could crack a code (a more text friendly version can be found here). The first winner recieved a full ride, and the next 100 recieved placement in the computer science department. The code is laughable: the numbers are base 4 (and are kindly delimted by a '/'); if you convert each to decimal, you get a number corresponding to a letter of english alphabet. I managed to crack it with one line of not-very-complex perl:

perl -e 's/(\d)(\d)(\d)\//chr($1*16+$2*4+$3+64)/ge;print' CODE_HERE
Does this mean I can go to college now? :)

P.S. : Further golfing welcome.

Replies are listed 'Best First'.
Re (tilly) 1: Canadian Cryptography Contest
by tilly (Archbishop) on Jan 22, 2002 at 17:55 UTC
    You are missing the loop. After adding that and usual straightforward golf tricks, plus an examination of their data set, you get down to:
    perl -pe's~(\d)(.)(.)/~chr$1*16+$2*4+$3+64~ge' DATA_FILE
      Ah, I was assuming a line to be pasted on the command line, you were assuming a data file (eg. an array). Whichever :)
Re: Canadian Cryptography Contest
by Aristotle (Chancellor) on Jan 22, 2002 at 18:02 UTC

    Apparently. :-)

    Golf: (just the most obvious things)

    perl -pe's!(\d)(\d)(\d)/!chr(64+$1*16+$2*4+$3)!ge' codedata

    Know thine commandline switches.

    Update: Doh, tilly was a few minutes faster.

    Update 2: Slightly longer but I like this better:

    perl -pe's!(\d..)/!$x=1;($x*=4)+=$_ for split//,$1;chr$x!ge' codedata

    Update 3: Thanks to tadman:

    perl -pe's!(\d..)/!$x=1;($x*=4)+=$_ for$1=~/./g;chr$x!ge' codedata

    Makeshifts last the longest.

      Then, of course, you would do the following:
      for(1..1e6){$s+=$_ for/./g};print$s
      Unfortunately, map seems to choke on 1e6 entries.
        perl -e'map$\+=$_,/./g for 1..1e6;print'
        also works. OTOH before this is done running you should be able to reason out why the answer is 27,000,001. (Big hint. The 1 is the contribution from the 7'th digit.) As for map chocking, if you are on any Perl before 5.6.1, the many for one map shows quadratic performance, so it will indeed choke and remain choked for a good while on a million entries.
        Ok, explain. Try as I might I can't figure out what you're trying to get at.

        Makeshifts last the longest.

Re: Canadian Cryptography Contest
by nikos (Scribe) on Jan 22, 2002 at 16:28 UTC
    Yeah, super! I won't go to that college, I guess. Thanks. And noone from perlmonks.com shouldn't go there ;) P.S. Every trivial task can be solved in one line of perl code.
Re: Canadian Cryptography Contest
by random (Monk) on Jan 23, 2002 at 06:24 UTC
    Here's the thing: on the first read, I chuckled, too. But think about it: maybe you'd want possible grad students cracking hard-core encryption, but if you set the bar too high for possible undergrads, you're not going to get anyone. I'd say there are two major reasons for this: one, to encourage interest in the college; two, for interested students to prove some level of competence in programming. Don't forget that a college wants smart students, but not students that believe they already know everything.

    -Mike-

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlmeditation [id://140630]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (4)
As of 2024-04-24 19:56 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found