Obfuscate Code Contest 3!

Online C++ programming contests.

Moderators: Darobat, RecursiveS, Dante Shamest, Bugdude, Wizard, raimo

Postby gamma » Wed Dec 01, 2004 5:29 pm

exomo wrote:Anybody know this code?
Sure, it is mine, slighty decrypted. I suspect gcc -E (or was it -S) and indent..

=>SPOILER<=
Let me explain the exomo-ned code via comments
Code: Select all
#include <stdio.h>
#include <stdlib.h>

// My sole variable. It has 32 bits.
// IIRC (if I remember correctly):
// Bit 01-01: player
// Bit 02-10: whether a position of the board has been used
// Bit 11-19: the player that used this square, undefined if it
//            is not in use.
// Bit 20-23: 4 bits for a runner from 1 to 15.
// Bit 24-31: 8 bits for reading a character from stdin/cin.
// Bit 32   : Erm.. backup bit, just in case one fails...
unsigned b;



// The joke line. In the original, it was [b]extern[/b] char JOKE.
// JOKE[0] is the character that is used for drawing pieces for p0.
// JOKE[1] is the character that is used for drawing pieces for p1.
char JOKE[100] = "#&*^(*=@#^*_: &%(#%)(#&#%@&(*: Don't run me on a c-compiler!";



// The routine to [b]c[/b]heck for "winning lines".
// It actually depends on x. See also main().
// USED OR NOT:
// (bit2>>1) 1   (bit3>>1)  2   (bit4>>1)   4
// (bit5>>1) 8   (bit6>>1) 16   (bit7>>1)  32
// (bit8>>1)64   (bit9>>1)128   (bit10>>1)256
// PIECE DISTRIBUTION:
// (bit11>>10) 1   (bit12>>10)  2   (bit13>>10)  4
// (bit14>>10) 8   (bit15>>10) 16   (bit16>>10) 32
// (bit17>>10)64   (bit18>>10)128   (bit19>>10)256
void C(unsigned x)
{
    if(!(b&x)) // Check if all pieces for this lines are used.
      if(((b>>10)&(x>>1))==(x>>1)||((b>>10)&(x>>1))==0)
      {//Exactly all 3(?) bits are used. Either player 0 or 1.
          puts("Win!");
          exit(0);
      }
}



int main(void)
{
    // b start as zero (global variable). += is the same as = here.
    // This line should read:
    // b += 1 //*2*/2
    //   ? 1022 : puts (JOKE) < 0, puts ("");
    // A c++-compiler reads: b += 1 ? 1022 etc. (Note 1 != 0.)
    // A c-compiler reads:   b += 1 / 2 ? 1022 etc. (Note 1/2 == 0.)
    // The comma-operator add nothing. Could have been a ';'.
    // Note: the board is hereby initialized with ONES, meaning the
    //       board is empty. (Or all zero with c-compilers.)
    b+=1?1022:puts(JOKE) < 0 ,puts("");



    // Board drawing routine.
    // Need at least 4 bits. Used bits 20-23.
    // After every 3, a newline must be drawn, hence the use of puts()
    // A "good" compiler complains when there is no expression
    // between '?' and ':'. I had to insert a dummy '1' :(
    for(b&=~(0xf<<19);((b>>19)&0xf)<9;b+=(1<<19),((b>>19)&0xf)%3?1:puts("|"))
      // Write space if not used, otherwise a character from JOKE.
      // Btw: 022 / 11 == 1.
      printf("|%c",b&(2<<((b>>19)&0xf))?' ':JOKE[(b>>(10+((b>>19)&0xf)))&(022/11)]);

    // For my art, I sometimes had to insert dummy code:
    // 3/2 == 1. Oh well. It obfuscates it a bit more 8)
    printf("Player%2d:",b&3/2);

    // While the board is not completely filled.
    // (C programma won't enter this loop.)
    while (b&1022)
    {
        // Clean bits 24-31.
        b&=~(0x1ff<<23);

        // Store the character read from keyboard.
        b|=(getchar()<<23);

        // NOT SURE: 8th bit of the character set
        // => not a "real" character.
        if(b&(1<<31)) break;

        // Check if the character is between 1 and 9.
        // Check if the position on the board has been used.
        // Note: b >> 23 is the character read from keyboard.
        if(((b>>23)-'0')>=1&&((b>>23)-'0')<=9&&(b&(1<<((b>>23)-'0'))))
        {
            // Mark that this position is in use.
            b^=(b&(1<<((b>>23)-'0')));

            // b^=1: flip the player.
            // Mark position is used by the player.
            ((b^=1)&1)&&(b|=(0x200<<((b>>23)-'0')));

            // Check for winning lines.
            C(14);  //         2    4    8 |  14
            C(112); //        16   32   64 | 112
            C(146); //       128  256  512 | 896
            C(168); // ----+---------------+----
            C(292); // 168 | 146  292  584 | 546
            C(546);
            C(584);
            C(896);

            // Board drawing routine again.
            puts("");
            for(b&=~(15<<19);((b>>19)&0xf)<9;b+=(1<<19),((b>>19)&0xf)%3?1:puts("|"))
                printf("|%c",b&(2<<((b>>19)&0xf))?' ':JOKE[(b>>(10+((b>>19)&0xf)))&1]);
            printf("Player" "%2d:", b&1);
            continue;
        }
    }
    return 0;
}


exomo wrote:Still crazy enough, but a little more readable.
(Sorry for destroying your Art gamma)
Thank you! Since this is more readable, I decided to annotate it.

exomo wrote:forgot to mention it doesn't compile on Darobat's computer, but on my own.
No problems here. Darobar PM-ed me about some errors, but I fixed those. Apparently #define p(...) printf(__VA_ARGS__) is not c++, but only c orso. My original version can be found here btw.
while (true){sleep(28800);work(57600);}
"Free" as in speech is so much better than "free" as in beer.
User avatar
gamma
 
Posts: 178
Joined: Mon Dec 01, 2003 9:16 am
Location: The Netherlands

Postby schloob » Wed Dec 01, 2004 6:04 pm

:]
User avatar
schloob
 
Posts: 1853
Joined: Mon Feb 16, 2004 10:29 am
Location: Seattle

Postby Darobat » Wed Dec 01, 2004 6:17 pm

Sorry I was a little late. I was busy last night and ran out of time. The poll is up.

viewtopic.php?t=8214
Code: Select all
#include <stdio.h>
struct W{char m,M[4??),w;void x(char
*W)??<w^=w;while(w[W]!=0)putchar(W[w
]^M[w++%5??));}W():m(040),w(0){char*
X="d@PLfAU\x05P)sHEMoTTPF""\31";for(
;w<5;w++[M??)=m++);x(X);}}w;main(){}
User avatar
Darobat
Moderator
 
Posts: 2572
Joined: Sat Sep 27, 2003 1:19 pm

Postby exomo » Thu Dec 02, 2004 2:46 am

gamma wrote: I suspect gcc -E (or was it -S) and indent..
Just used my own hand.

gamma wrote:
exomo wrote:forgot to mention it doesn't compile on Darobat's computer, but on my own.

No problems here. Darobar PM-ed me about some errors, but I fixed those. Apparently #define p(...) printf(__VA_ARGS__) is not c++, but only c orso. My original version can be found here btw
I was talking about my code that doesn't work on Darobat's computer, nut yours
Who needs a signature anyway.
User avatar
exomo
 
Posts: 879
Joined: Fri Sep 26, 2003 12:30 pm
Location: germany->baden

Postby gamma » Thu Dec 02, 2004 3:32 am

exomo wrote:
gamma wrote: I suspect gcc -E (or was it -S) and indent..
Just used my own hand.

gcc -E works cool:
-E Stop after the preprocessing stage; do not run the compiler proper.
The output is in the form of preprocessed source code, which is
sent to the standard output.

Try it sometime!
while (true){sleep(28800);work(57600);}
"Free" as in speech is so much better than "free" as in beer.
User avatar
gamma
 
Posts: 178
Joined: Mon Dec 01, 2003 9:16 am
Location: The Netherlands

Previous

Return to Contests

Who is online

Users browsing this forum: No registered users and 1 guest