Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re: Mastermind in 2 lines

by YAFZ (Pilgrim)
on Jul 23, 2003 at 13:26 UTC ( [id://277137]=note: print w/replies, xml ) Need Help??


in reply to Mastermind in 2 lines

++ for such a nice and cool implementation. These kind of programs motivate me to learn Perl better. The beauty in simplicity is impressive indeed!

Replies are listed 'Best First'.
Re: Re: Mastermind in 2 lines
by skyknight (Hermit) on Jul 26, 2003 at 16:50 UTC
    Of course, the fact that this is even possible gives rise to the occasional assertion that Perl is a "write only" language. It's great to be able to get such brevity, should brevity be something you desire in a particular situation, but it's pretty mean to force such brevity onto an unwitting maintainer. :-)
      Here's a version run through Perltidy - not too hard to understand if you know Perl regular expressions. The $c is the secret, $_ is the guess, and the code depends on matching the guess concatenated with the secret. Interesting code, and not a bad example of Perl really - it's just that the traditional scrunched-up formatting makes it look like line noise. I also made some minor changes for understandability/legibility. Of course, I realise this is entirely against the idea of obfuscated code :)
      #!/usr/bin/perl -l $c .= 1 + int rand 6 for 1 .. 4; # Build up the secret while (<>) { /^\d{4}$|!/ || next; # Check user entered 4 digits $_ .= $c; 1 while s!(.)(.{4})\1!?$2*!s; # Match in right position 1 while s!(\d)(.*\n.*)\1!+$2!; # Match somewhere print /[*+]/g; # Print matches /\*{4}|!/ && exit; }

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (2)
As of 2024-04-24 15:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found