One interest of mine is obfuscated programming. It’s not something I practise in real life, of course, but is one of those intellectual challenges, that really highlights how much you don’t know about a programming language. In reviewing some old emails, this code snippet came to light, and I thought I should share. It produces the entire lyrics to “The 12 days of Christmas” in just 388 bytes.
i,j;char*p,*t="
12 drummers drumming
11 pipers piping
10 lords a leaping
9 ladies dancing
8 maids a milking
7 swans a swimming
6 geese a laying
5 golden rings
4 calling birds
3 french hens
2 turtle doves and
a partridge in a pear tree
";main(){while(i++<12){printf("On the %d%.2s day of christmas my true love sent to me
",j=i,&"thstndrd"[i<4?i*2:0]);for(p=t;*p++>31||++j<13;);puts(p);}}
Obtuse! If you’re geek enough, you may be interested in the use of %.2s, and the snippet:
&"thstndrd"[i<4?i*2:0]
to produce the correct ordinal. Own up. Who else know this :)
However, this is not the shortest! The colleague of a friend of a friend came up with this:Most of the same ideas are used, but this uses tabs to break the song into the appropriate lines (invisible here, alas), and
main(i){char*q,p[]="thstndrd 12 drummers drumming 11 pipers piping
10 lords a leaping 9 ladies dancing 8 maids a milking 7
swans a swimming 6 geese a laying 5 golden rings 4 calling
birds 3 french hens 2 turtle doves and a partridge in a pear
tree";for(;q=strrchr(p,9);printf("\nOn the %d%.2s day of christmas my true
love sent to me%s\n",i,p+(i<4)*i*2,q),++i)++*q;}strrchr instead of my manual for loop.