• Welcome to Valhalla Legends Archive.
 

C or C++ Brain Busters

Started by Mephisto, May 07, 2004, 10:10 PM

Previous topic - Next topic

Mephisto

See if you can predict the output of the following program without compiling/running it.


#include <stdio.h>
#define S(s)char x[]=#s;s
#define Q(x)x
#define A(x,y)y##x
#define B(x,y)A(y,x)
#define C(x,y)B(y,x)
#define Z(s,t,u)case s:if(*p!=32){t;}else{u;}break;
S(B( A( a ,m ),A(n ,i))() {B (A(h,c ),A(r ,a ))*p=x ;B(A( n, i),t)t
=0;B(A(n , i),t)s =0;B( f ,A(r, o )) (;*p;Q( p)++){C( B( A(c,t) ,h),B(A(
w, s),i))( s){ Z( 0,t+=8 *8-00 ,s ++)Z( 1,t+= 8 ;,s++ )Z
( 2, t++ ,putchar(t-73);t=s=0)}}})

Mephisto

The answer is: "Hello, world!"
For people's compilers who automatically close the console window you can replace the code with:


#include <stdio.h>
#include <stdlib.h>
#define S(s)char x[]=#s;s
#define Q(x)x
#define A(x,y)y##x
#define B(x,y)A(y,x)
#define C(x,y)B(y,x)
#define Z(s,t,u)case s:if(*p!=32){t;}else{u;}break;
S(B( A( a ,m ),A(n ,i))() {B (A(h,c ),A(r ,a ))*p=x ;B(A( n, i),t)t
=0;B(A(n , i),t)s =0;B( f ,A(r, o )) (;*p;Q( p)++){C( B( A(c,t) ,h),B(A(
w, s),i))( s){ Z( 0,t+=8 *8-00 ,s ++)Z( 1,t+= 8 ;,s++ )Z
( 2, t++ ,putchar(t-73);t=s=0)}}system("pause");})


Forum PM me for spoiler information.

Next question: Figure out why the above program prints that.


Mephisto

You didn't think that I wrote that did you...?  And then imply that I wrote it...?

I thought it would just be interesting if someone could figure it out without actually knowing about it.  :p

Soul Taker

Quote from: Mephisto on May 08, 2004, 08:17 PM
You didn't think that I wrote that did you...?  And then imply that I wrote it...?

I thought it would just be interesting if someone could figure it out without actually knowing about it.  :p
Why are you so defensive?

Adron

#5
Step by step:


#include <stdio.h>
#define S(s)char x[]=#s;s
#define Q(x)x
#define A(x,y)y##x
#define B(x,y)A(y,x)
#define C(x,y)B(y,x)
#define Z(s,t,u)case s:if(*p!=32){t;}else{u;}break;
S(B( A( a ,m ),A(n ,i))() {B (A(h,c ),A(r ,a ))*p=x ;B(A( n, i),t)t
=0;B(A(n , i),t)s =0;B( f ,A(r, o )) (;*p;Q( p)++){C( B( A(c,t) ,h),B(A(
w, s),i))( s){ Z( 0,t+=8 *8-00 ,s ++)Z( 1,t+= 8 ;,s++ )Z
( 2, t++ ,putchar(t-73);t=s=0)}}})




#include <stdio.h>
#define S(s)char x[]=#s;s
#define Q(x)x
#define A(x,y)y##x
#define B(x,y)A(y,x)
#define C(x,y)B(y,x)
#define Z(s,t,u)case s:if(*p!=32){t;}else{u;}break;
char x[]="B( A( a ,m ),A(n ,i))() {B (A(h,c ),A(r ,a ))*p=x ;B(A( n, i),t)t =0;B(A(n , i),t)s =0;B( f ,A(r, o )) (;*p;Q( p)++){C( B( A(c,t) ,h),B(A( w, s),i))( s){ Z( 0,t+=8 *8-00 ,s ++)Z( 1,t+= 8 ;,s++ )Z ( 2, t++ ,putchar(t-73);t=s=0)}}}";
B( A( a ,m ),A(n ,i))() {
 B (A(h,c ),A(r ,a ))*p=x ;
 B(A( n, i),t)t =0;
 B(A(n , i),t)s =0;
 B( f ,A(r, o )) (;*p;Q( p)++){
   C( B( A(c,t) ,h),B(A( w, s),i))( s){
     Z( 0,t+=8 *8-00 ,s ++)
     Z( 1,t+= 8 ;,s++ )
     Z( 2, t++ ,putchar(t-73);t=s=0)
   }
 }
}





#include <stdio.h>
#define B(x,y)x##y
#define C(x,y)y##x
char x[]="B( A( a ,m ),A(n ,i))() {B (A(h,c ),A(r ,a ))*p=x ;B(A( n, i),t)t =0;B(A(n , i),t)s =0;B( f ,A(r, o )) (;*p;Q( p)++){C( B( A(c,t) ,h),B(A( w, s),i))( s){ Z( 0,t+=8 *8-00 ,s ++)Z( 1,t+= 8 ;,s++ )Z ( 2, t++ ,putchar(t-73);t=s=0)}}}";
B( ma,in)() {
 B (ch,ar)*p=x ;
 B(in,t)t =0;
 B(in,t)s =0;
 B( f , or) (;*p; p++){
   C( B( tc ,h),B(sw,i))( s){
     case 0:if(*p!=32){t+=8 *8-00; }else{s ++;}break;
     case 1:if(*p!=32){t+= 8 ;;}else{s++;}break;
     case 2:if(*p!=32){t++ ;}else{putchar(t-73);t=s=0;}break;
   }
 }
}





#include <stdio.h>
#define B(x,y)x##y
#define C(x,y)B(y,x)
char x[]="B( A( a ,m ),A(n ,i))() {B (A(h,c ),A(r ,a ))*p=x ;B(A( n, i),t)t =0;B(A(n , i),t)s =0;B( f ,A(r, o )) (;*p;Q( p)++){C( B( A(c,t) ,h),B(A( w, s),i))( s){ Z( 0,t+=8 *8-00 ,s ++)Z( 1,t+= 8 ;,s++ )Z ( 2, t++ ,putchar(t-73);t=s=0)}}}";
main() {
 char *p=x;
 int t=0;
 int s=0;
 for(;*p; p++){
   switch(s){
     case 0:if(*p!=32){t += 64; }else{s ++;}break;
     case 1:if(*p!=32){t += 8; }else{s++;}break;
     case 2:if(*p!=32){t++; }else{putchar(t-73);t=s=0;}break;
   }
 }
}


The string to be printed is thus encoded in the program itself. The information is carried in the length of the words / non-space strings. Each character is encoded in three words. If the lengths of the three words are a, b and c, the character is a*64 + b*8 + c - 73.

Example #1: "B( A( a "

The word lengths are 2, 2, 1. The character is 2*64 + 2*8 + 1 - 73 = 72 = 'H'.

Example #2: ",m ),A(n ,i))() "

The word lengths are 2, 5, 6. The character is 2*64 + 5*8 + 6 - 73 = 101 = 'e'.