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; }