\$\begingroup\$

C, Twelve Days of Xmas Style

New version:

main(Z,_){Z?(_=Z[" $X,X3Y<X@Z@[<XHZHX," "` \\(Z(X0Z0Z8[@X@^8ZHZHX(Z(`#Y(Z(X3[8" "\\@_8ZHXHXHX(Z(` \\(Z(X0Z0Z8\\@_8ZIXI" "X(Z(` \\,X0Z0Z8\\@_8ZHZHX,"])?main(0,_ -32),main(Z+1,_):0:(putchar((_>>3)["kt" "wy~|tE/42"]-37),(_&7)?main(0,_-1):0);}

Output:

FFFFF OOOOO RRRR TTTTT Y Y TTTTT W W OOOOO F O O R R T Y Y T W W O O FFFF O O RRRR T Y T W W W O O F O O R R T Y T WW WW O O F OOOOO R R T Y T W W OOOOO

By the way, also check out my text-to-speech answer.

Original Version:

main(c,z,_){c==1?main(c+1,0,c^c):c==2? z=_["##$#%&#%#x'%%()&(%%x$%$((&(*%x'%" "%((&(+%x'#%((&(%#x"],z?z=='x'?main(4, _,c*5):main(c+1,z,0),main(c,z,_+1):0:c ==3?(_-2)==3?main(_-1,_,32):(main(c+1, c,((2+c)*(z-35)+_)["six*nine= { } " " ; _ ( ) [ 3 ]do {;=0xDA"]== 32?32:043),main(c,z,_+1)):putchar(_);}

The output is:

##### ##### #### ##### # # ##### # # ##### # # # # # # # # # # # # # #### # # #### # # # # # # # # # # # # # # # # ## ## # # # ##### # # # # # # # #####

Alternate spacing, if you're feeling tacky:

main(c ,z,_){c==01? main(c+ 1,0,c^c):c==2 ?z=_["#" "#$#%&#%#x'%%" "()&(%%x" "$%$(" "(&(""*%x" "'%%(" "(&(" "+%x" "'#%(" "(&(" "%#x" ],z ?z =='x'?main(4,_ ,c*5):main(c +1,z,0),main(c ,z,_+1):00:c ==3?(_+-2)==3? main(_-1,_, 32):( main( c+1,c ,((2+ c)*(z -35)+ _)["" "six" "*ni" "ne= { } " " ;" " _ ( " ") [" " 3 ]do {;"]== 32?32 :043),main(c,z ,_+1) ):putchar(_);}

The program is a single recursive statement. I made it in the style of my favorite obfuscated C program ever, Twelve Days of Christmas (compile, prepare mind to be blown, run).

HOW TO

Also, since this seems as good a place as any, here is a guide describing how to make this type of program. This guide uses the original version above as an example. Aside from the first bit with the block letters, they are general steps:

INITIAL: First, I started by making the block letters:

##### ##### #### ##### # # ##### # # ##### # # # # # # # # # # # # # #### # # #### # # # # # # # # # # # # # # # # ## ## # # # ##### # # # # # # # #####

I then made a numbered list of the unique patterns in each 5-column character row:

0: ***** 1: **** 2: * * 3: 4: * 5: * 6: * * 7: * * * 8: ** **

And so each of the 5 pixel rows of text becomes a series of 9 numbers:

00000 00000 11111 00000 22222 33333 00000 22222 00000 44444 22222 22222 55555 66666 33333 55555 22222 22222 11111 22222 11111 55555 55555 33333 55555 77777 22222 44444 22222 22222 55555 55555 33333 55555 88888 22222 44444 00000 22222 55555 55555 33333 55555 22222 00000

For obfuscation (and ease of programming) we add the '#' character to the numbers. In the program below, patterns is the array of pixel patterns, and lines is the obfuscated array of pattern codes for each line, terminated by an 'x'. For further obfuscation we define "on" pixels in patterns to be any character that isn't a space; this lets us put more misleading text in pattern :

#include <stdio.h> char pattern[] = "six*n" "ine= " "{ }" " " "; " " _ " " ( ) " "[ 3 ]" "do {;"; char lines[] = "##$#%&#%#x" "'%%()&(%%x" "$%$((&(*%x" "'%%((&(+%x" "'#%((&(%#x"; void printpattern (char c) { int n; for (n = 0; n < 5; ++ n) putchar(pattern[5*(c-'#') + n]==32?32:'#'); putchar(' '); } int main () { char *ptr = lines; while (*ptr) { while (*ptr != 'x') printpattern(*(ptr++)); putchar('

'); ++ ptr; } }

STEP 1: The next step involves a few tasks:

Remove all loops and use recursion.

Change all functions (except main) to the form int function (int, int) and use the same parameter names for each. The reasons will become clear later.

and use the same parameter names for each. The reasons will become clear later. Change main to the form int main (int, int, int) and name the last two parameters the same as your function parameter names.

to the form and name the last two parameters the same as your function parameter names. Replace all references to string constants with the strings themselves; and use each string only once if possible.

The include can be removed; it's unnecessary for int putchar (int) .

We can also take advantage of the weird C feature where a[b] is equivalent to b[a] to obfuscate further.

int printpattern (int z, int _) { if (_==5) putchar(' '); else{ putchar((5*(z-'#') + _)["six*nine= { } ; _ ( ) [ 3 ]do {;"]==32?32:'#'); printpattern(z, _+1); } return 0; } // z ignored, _ is index int printtext (int z, int _) { z = _["##$#%&#%#x'%%()&(%%x$%$((&(*%x'%%((&(+%x'#%((&(%#x"]; if (z) { if (z == 'x') putchar('

'); else printpattern(z, 0); printtext(z, _ + 1); // first parameter arbitrary } return 0; } int main (int c, int z, int _) { printtext(0, 0); }

STEP 2: Next, make use of the ?: and , operators to transform each function into a single return statement. I'm illustrating this separately from the above because this is where things start getting confusing to look at. Remember that putchar() returns an int , and ?: takes precedence over , :

int printpattern (int z, int _) { return _==5 ? putchar(' ') : (putchar((5*(z-'#') + _)["six*nine= { } ; _ ( ) [ 3 ]do {;"]==32?32:'#'), printpattern(z, _+1)); } // z ignored, _ is index int printtext (int z, int _) { return z = _["##$#%&#%#x'%%()&(%%x$%$((&(*%x'%%((&(+%x'#%((&(%#x"], z ? z == 'x' ? putchar('

') : printpattern(z, 0) , printtext(z, _ + 1) : 0; } int main (int c, int z, int _) { printtext(0, 0); }

STEP 3: Ok. The next step is a big one. All of the functions are now a single statement of the same form. We can now combine them all into a single function, identifying each one by a number -- essentially turning the entire program into a single recursive function. Note that the first parameter to main will be 1 when the program is run with no arguments, so that should be our initial state.

Also, since our parameter c to main is our state variable, we know its value at all times, and we can obfuscate a little further by replacing integer constants with their values in terms of c (for example, when we know c is 2, we can replace 5 with c+3 ). Other little obfuscations can be done too (e.g. I replaced '#' with 35 and 043 ):

int main (int c, int z, int _) { switch (c) { case 1: // main return main(c+1, 0, c^c); // (2, 0, 0) case 2: // printtext return z = _["##$#%&#%#x'%%()&(%%x$%$((&(*%x'%%((&(+%x'#%((&(%#x"], z ? z == 'x' ? putchar('

') : main(c+1, z, 0) // c+1==3 , main(c, z, _ + 1) : 0; case 3: // printpattern return (_-2)==3 ? // _==5 putchar(' ') : (putchar(((2+c)*(z-35) + _)["six*nine= { } ; _ ( ) [ 3 ]do {;"]==32?32:043), main(c, z, _+1)); } }

STEP 4: Finally, remove the switch block by using a series of ?: operators. We can also remove the int declarations, since C will use them by default, as well as the return itself (which will generate a warning at worst). After this, our program is a single, recursive function with one statement. Pretty cool, right?

Edit: I replaced putchar() with a main and c==4 below; because I just thought of it at the last minute:

main (c, z, _) { c == 1 ? main(c+1, 0, c^c) : c == 2 ? z = _["##$#%&#%#x'%%()&(%%x$%$((&(*%x'%%((&(+%x'#%((&(%#x"], z ? z == 'x' ? main(4,_,c*5) : main(c+1, z, 0) , main(c, z, _ + 1) : 0 : c==3 ? (_-2)==3 ? main(_-1,_,32) : (main(c+1,c,((2+c)*(z-35) + _)["six*nine= { } ; _ ( ) [ 3 ]do {;"]==32?32:043), main(c, z, _+1)) : // c==4 putchar(_); }

If you want to add a little flair, you can use more interesting numbers for c and even base the checks off of other numbers (e.g. for the c==2 case, z is ignored and available, so instead of calling main(2,z,_) you could call main(-97,_,_) and replace c==2 with c<-z ). Be creative; the possibilities are endless.