Ryan IRL

Christmas Code

A friend at work put together our 2nd annual Christmas coding contest. The goal was to print out a tree that would scale by n, and was sort of inspired by another well known holiday coding challenge, only we made it a little more fun by adding some considerable complexity by adding slashes on either side of the tree to fill out the shape, and a pipe and ^ for the bottom and top.

Turns out I won despite some other brilliant entries. My secret sauce was the use of nested ternary operators, and pretty much makes the code unreadable. I’m posting the example in case anyone is interested in running it and seeing if they can improve/learn from it. Might also be worth noting that you should turn php notices off, and give $n an unsigned int value (for best results).

The code:
<?$a=str_repeat;for($t=$n+1;$t--;)echo$a(' ',$t?$t-1:$n-1).($t?$t==$n?"^":"/".$a('*',($p+=1*2)-1)."\\":'|')."\n";

This entry was written by Ryan Leland, posted on December 23, 2009 at 10:20 pm, filed under Uncategorized. Bookmark the permalink. Follow any comments here with the RSS feed for this post.

Timeline

1 Comment