This one isn't so much about obfuscation, but more about brevity:
#!/usr/bin/perl -l $c.=1+int rand 6for 6..9;while(<>){/^\d{4}$|!/||next;$_.=$c;4while s+(.)(.{4})\1+?$2*+s;4while s-(\d)(.*\n.*)\1-+$2-;print/[*+]/g;/\*{4}| +!/&&die}
Game rules:
The program generates a secret four-digit number, each digit ranging from 1 to 6. The object of the game is to guess this number. In order to achieve this, the player enters a four-digit number consisting of digits from 1 to 6. Now the program does its output:
'*' for a digit that has correct value and position
'+' for a digit that has correct value and wrong position
If the correct number is entered, the game is over and the program quits. Entering '!' also quits the game.

Replies are listed 'Best First'.
Re: Mastermind in 2 lines
by chimni (Pilgrim) on Jul 29, 2003 at 10:42 UTC
    can you dissect this for me
     I am new at this .How does the pattern work
      1 while s!(.)(.{4})\1!?$2*!s;  # Match in right pos
      It's cute... Since the "secret" is concatenated with what the user entered, this will match if you get the same number separated by 4 characters (that's 3 digits and the \n in some combination).

      It replaces that with a ? for the first position, and a * for the 2nd, with $2 (the 4 characters in between) in the middle. Then, the substitution is repeated as long as it succeeds, so if you have all 4 characters right, you get 4 *'s.

      The 2nd substitution puts in +'s for any numbers that appear in both lines after the first substitutionwas done, which will get only numbers in the wrong place since all the ones in the right spots are already replaced with '*'.

      The last print will print out any * or + in the string, telling you how many you have right or nearly right.

      Very nice script... I like the cool uses of s/// and the print of the matches.

Re: Mastermind in 2 lines
by YAFZ (Pilgrim) on Jul 23, 2003 at 13:26 UTC
    ++ for such a nice and cool implementation. These kind of programs motivate me to learn Perl better. The beauty in simplicity is impressive indeed!
      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; }