Friday, 17 December 2010

#carolsincode - Christmas for Programmers

#carolsincode
#carolsincode are small pseudo-programs which contain a Christmas song. Some examples display the lyrics while others use the program's control structures to define the song.

Here are five examples in C, CSS and JavaScript. Can you do better? ;-)

var kings=new Array(3);
for (x in kings)
{
  kings[x].origin='orient';
  kings[x].bearingGift=true;
  kings[x].travelled='afar';
}

main() {
  int a;
  for (a=1;a<5;a++) printf("Noel, ");
  printf("Born is the King of Israel.");
}

#rudolf .nose {
  color: red;
  background: url('very_shiny.jpg');
}

while ( shepherds.watch() == 'flocks' &&
  date.getHours() in night )
{
  lord.angel--;
  glory.shone_around();
}

for ( c=1, c<=4, c++)
{
  noel()
};
king = new Object();
king.kingdom = 'Israel';

5 comments:

  1. The Rudolf one is pretty funny...

    ReplyDelete
  2. Great idea :) It inspired me to do some of my own:

    http://www.weedoorsbanging.com/archives/carols-in-code.html

    ReplyDelete
  3. for(i=0;i++<12;)alert("On day "+i+" of xmas: "+"drummers|pipers|lords|ladies|maids|swans|geese|rings|birds|hens|doves|partridge".split("|").map(function(x,n){return 12-n+" "+x}).slice(-i))

    ReplyDelete
  4. bells.jingle(); bells.jingle(); while( all > 0 ) { way[all--].jingle(); } sleigh.horses=1; sleigh.ride();

    do {sleigh_bells.ring();} while (you.listening==true); for (snow in lane) {snow.glistening=true}; winter.wonderland='walking';

    ReplyDelete
  5. #!/usr/bin/env python
    # -*- coding: utf-8 -*-

    import sys

    data = [
    [ 2, 4, 2, 2, 8, 2, 8, 2, 8, 2, 2, 4, 2 ],
    [ 2, 4, 2, 2, 8, 2, 8, 2, 8, 2, 2, 4, 2 ],
    [ 2, 4, 2, 2, 2, 4, 2, 2, 2, 4, 2, 2, 2, 4, 2, 2, 2, 4, 2 ],
    [ 2, 4, 2, 2, 2, 4, 2, 2, 2, 4, 2, 2, 2, 4, 2, 2, 2, 4, 2 ],
    [ 8, 2, 8, 2, 8, 2, 8, 2, 8 ],
    [ 8, 2, 8, 2, 8, 2, 8, 2, 8 ],
    [ 2, 4, 2, 2, 2, 4, 2, 2, 2, 8, 2, 11, 2 ],
    [ 2, 4, 2, 2, 2, 4, 2, 2, 2, 8, 2, 11, 2 ],
    [ 0 ],
    [ 2, 4, 2, 2, 8, 2, 2, 8, 2, 2, 8, 3, 8, 2, 2, 4, 2, 2, 8 ],
    [ 2, 4, 2, 2, 8, 2, 2, 8, 2, 2, 8, 3, 8, 2, 2, 4, 2, 2, 8 ],
    [ 2, 4, 2, 2, 2, 4, 2, 2, 2, 8, 2, 2, 2, 5, 2, 2, 2, 4, 2, 2, 2, 4, 2, 2, 2 ],
    [ 2, 4, 2, 2, 2, 4, 2, 2, 2, 8, 2, 2, 2, 5, 2, 2, 2, 4, 2, 2, 2, 4, 2, 2, 8 ],
    [ 8, 2, 2, 4, 2, 2, 2, 8, 2, 2, 2, 5, 2, 2, 8, 2, 8, 2, 8 ],
    [ 8, 2, 2, 4, 2, 2, 2, 8, 2, 2, 2, 5, 2, 2, 8, 2, 8, 8, 2 ],
    [ 2, 4, 2, 2, 8, 2, 8, 2, 2, 2, 8, 3, 2, 4, 2, 5, 2, 5, 8 ],
    [ 2, 4, 2, 2, 8, 2, 8, 2, 2, 2, 8, 3, 2, 4, 2, 5, 2, 5, 8 ]
    ]

    print

    for line in data:
    [ sys.stdout.write( ('*' if i % 2 == 0 else ' ') * line[i]) for i in range(len(line)) ]
    print
    print

    ReplyDelete