12 Days of Christmas Lyrics

Brainfuck - 2,974

I am rather proud of this one. That sounds like a pretty big number, but keep in mind, I did not use any external compression libraries, and none of the original text is in my program anywhere. None of the other submissions can say that. This is all hand coded. More naive text generators give over 39k for this text, so I would say this is a significant improvement.

>--[<->+++++]<---[>+>+>+>+>+<<<<<-]++++++++++>>[+++++>]<<<[+++++>]<<[+
++++>]<+++++>>++++[<++++++++>-]++++++++++++>+>+>>>>>>>>>>>>>+<<<<<<<<<
<<<<<<[<<<<++.-.->>>.<<++.--<<.<++.-->>>>>.>>>>>>>>>>>>>>[<<<<<<<<<<<<
<<<<++.>.<<<<++.-->>-.+<--.++>>.--<<.>>>>.>>>>>>>>>>>>>>-]<[<<<<<<<<<<
<<<<<<<<++.>>-.<<.>>>>-.+<<<<.-->>++.>++.--<<.>->>>.>>>>>>>>>>>>>>+<-]
<[<<<<<<<<<<<<<<++.<<<++.-->>+.->.--<<.>>>>.>>>>>>>>>>>>>+<-]<[<<<<<<<
<<<<<<<+.<+.>.->++.--<<-.>>>>.>>>>>>>>>>>>+<-]<[<<<<<<<<<<<<<<<++.-->+
.--.+.>>++.--<<.>>>>.>>>>>>>>>>>+<-]<[<<<<<<<<<<<+.<<<++.>>>>-.+<<<<.-
->>+.->+.--<<.>>>>.>>>>>>>>>>+<-]<[<<<<<<<<<<+.<<+.>>>+.-<+.--<<-.>>>>
.>>>>>>>>>+<-]<[<<<<<<<<<<<--.+++.---.>>++.--<<++.>>>>.>>>>>>>>+<-]<[<
<<<<<<<<<--.>++.-->>--.++<.++.--<<++.>>>>.>>>>>>>+<-]<[<<<<<<<++.<<.+.
->>--.<<<+.->>>>>.>>>>>>+<-]<[<<<<<<+.-<<<++.--.>>++.-.-<<+.->>>>>.>>>
>>+<-]<[<<<<<<<--.+++.->>.+.+.-->>.>>>>+<-]<<<<<<<<+.---.>>>>++.>.<<<+
+.<--.>>>>.<<<<<++.>++.>>.<<+.>>+.+.<--.<<--.>>>-.>>.<<<.>>.>.<<+.--.>
----.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--.<++..>>>.-<<<.>>>>>
.<<<<<----.>>>>>.<<<-.<<.++>>>>+.--<<<++.>++.-<--.+>>>>.>>>>>>>>>>>>>>
>>>>>>>-]<[<<<<<<<<<<<<<<<<<<<<<<<<--.+++.>>>-.+<<<<++.>>>>>.<<<<--.>+
+.---.<<-.+.-->>++.>>>.<<.<<++.>.-<--.+>>+.->>.>>>>>>>>>>>>>>>>>>>>-]<
[<<<<<<<<<<<<<<<<<<<<<<<--.>++.>>--.++<.>>.<<<<<.--.>>---..<+++.>++.-<
--.>>>>.<<<<<+.>++.->>.<<<++.->>>+.->>.>>>>>>>>>>>>>>>>>>>-]<[<<<<<<<<
<<<<<<<<<<<<++.<<.>>--.<<<++..>>>>>.<<<<--.>>.<<<.>>+.<<--.>++.>>>>.<<
<<.<++.-->>.->+.->>.>>>>>>>>>>>>>>>>>>-]<[<<<<<<<<<<<<<<<<<<<++.>.<<++
.>>>.<<.>--.<--.++.<---.<<++.>>>>>.<<<<<-.>>+++.>>+.<<<<+.>>>-.>>.<<<<
<----.>>-.-<<+++.->>>->+>.>>>>>>>>>>>>>>>>>-]<[<<<<<<<<<<<<<<<<<<<<<--
.>>>>>.<<--.<<<.>>>++.++.--.<<+.<+++.>--.<+.>>>>>.<<<<++.>+.>>>.<<<<<-
---.>>>>>.<<<++.<<++++.----.>>>.>>.<<++.--.<<<++++..>>>>>.<<<<<-->->--
->>>>>>>>>>>>>>>>>>>-]<<<<<<<<<<<<<<<<<<<<<<..>>>>>>>->+[>+>>>>>>>>>>>
>>[>]+[<]<<<<<<<<<<<<<-]>[<+>-]<<]

Unfortunately, this is about 600 characters longer than its own output, but whatever. It keeps the characters c,h,m,r,w in an array, and uses that to print all text. Two arrays to the right of twelve spaces each keep track of which day we are on for the count, and for which items we can output. I may be able to optimize it a bit by reorganizing the memory map to bring the printing characters in between the two counting arrays to avoid such long chains of <<<<<<< and >>>>>>, but that would be a lot of work at this point. I could also probably choose some better seed characters with frequency analysis to minimize incrementing / decrementing, but whatever.

This does depend on 8-bit wrapping cells to work properly.

Ungolfed:

>--[<->+++++]<---

[>+>+>+>+>+<<<<<-]++++++++++
>>[+++++>]<<<[+++++>]<<[+++++>]<+++++>>
++++[<++++++++>-]
++++++++++++>+>+>>>>>>>>>>>>>+<<<<<<<<<<<<<<<
[ 
   <<<<++o.-n.->>>.<<++t.--<<h.<++e.-->>>>>.    
   >>>>>>>>>>>>>>
   12[<<<<<<<<<<<<<<<<++t.>w.<<<<++e.-->>-l.+<--f.++>>t.--<<h.>>>>.>>>>>>>>>>>>>>-]
   11<[<<<<<<<<<<<<<<<<<<++e.>>-l.<<e.>>>>-v.+<<<<e.-->>++n.>++t.--<<h.>->>>.>>>>>>>>>>>>>>+<-]
   10<[<<<<<<<<<<<<<<++t.<<<++e.-->>+n.->t.--<<h.>>>>.>>>>>>>>>>>>>+<-]
   9<[<<<<<<<<<<<< <<+n.<+i.>n.->++t.--<<-h.>>>> . >>>>>>>>>>>>+<-]
   8<[<<<<<<<<<<< <<<<++e.-->+i.--g.+h.>>++t.--<<h.>>>> . >>>>>>>>>>>+<-]
   7<[<<<<<<<<<< <+s.<<<++e.>>>>-v.+<<<<e.-->>+n.->+t.--<<h.>>>> . >>>>>>>>>>+<-]
   6<[<<<<<<<<< <+s.<<+i.>>>+x.-<+t.--<<-h.>>>> . >>>>>>>>>+<-]
   5<[<<<<<<<< <<<--f.+++i.---f.>>++t.--<<++h.>>>>. >>>>>>>>+<-]
   4<[<<<<<<< <<<--f.>++o.-->>--u.++<r.++t.--<<++h.>>>> . >>>>>>>+<-]
   3<[<<<<<< <++t.<<h.+i.->>--r.<<<+d.->>>>>.>>>>>>+<-]
   2<[<<<<<<+s.-<<<++e.--c.>>++o.-n.-<<+d.->>>>>.>>>>>+<-]
   1<[<<<<<<<--f.+++i.->>r.+s.+t.-->>.>>>>+<-]
   <<<<<<<<+d.---a.>>>>++y.>_.<<<++o.<--f.>>>>_.<<<<<++c.>++h.>>r.<<+i.>>+s.+t.
   <--m.<<--a.>>>-s.>>_.<<<m.>>y.>_.<<+t.--r.>----u.<<<<++++e.>>>>>_.
   <<<-l.+++o.>>+v.<<<<e.>>>>>_.   
   <<<<--g.<----a.>>>>v.<<<<++++e.>>>>>.
   <<++t.<o.>>>.
   <<<--m.<<e.>>>>>.
   <<<<<-->+>>-->+
   >>>>>>>>>>>>>>>>>>>>>>>>>>>>
   12[<<<<<<<<<<<<<<<<<<<<<<<<<<<<<++t.>w.<<<<++e.>>-l.>>-v.<<<<e.->>>>>.
   <<<<<d.>>>--r.>-u.<<+m.m.<<+e.>>>r.+s.>>.<<<<<-d.->>>-r.>u.<<m.m.<+i.>+n.-<--g.+>>>++>.
   >>>>>>>>>>>>>>>>>>>>>>>>>>>-]
   11<[<<<<<<<<<<<<<<<<<<<<<<<<<<< <<<<++e.>>-l.<<e.>>>>-v.+<<<<e.>>++n.>>>.
   <<--p.<<+i.>>p.<<<e.-->>>++r.+s.>>.<<---p.<<i.>>p.++<<i.>n.-<--g.+>>>>.>>>>>>>>>>>>>>>>>>>>>>>>>>-]
   10<[<<<<<<<<<<<<<<<<<<<<<<<<<< <++t.<<<++e.>>+n.>>> .<<<--
   .+++o.>--r.<<<-d.>>>+s.>>.<<<<<---a.>>>>>.<<<---l.<<++++e.----a.++>>>---p.++<<+
   i.>++n.-<--g.+>>>> .    >>>>>>>>>>>>>>>>>>>>>>>>>-]
   9< [<<<<<<<<<<<<<<<<<<<<<<<<< <<+n.<+i.>n.<<++e.>>>>> .<<<--l.<<----a.+++d.>
   i.<+e.>>>+s.->> .<<<<<-d.---a.>>++n.<<++c.>i.>n.-<--g.+>>>> . >>>>>>>>>>>>>>>>>>>>>>>>-]
   8< [<<<<<<<<<<<<<<<<<<<<<<<< <<<<++e.>+i.--g.+h.>>++t.>> .<<<m.<<----a.>+
   i.<+++d.>>>-s.->> .<<<<<---a.++>>>>> .<<<m.<i.>-l.-k.<i.>+++n.-<--g.+>>>> . >>>>>>>>>>>>>>>>>>>>>>>-]
   7< [<<<<<<<<<<<<<<<<<<<<<<< <+s.<<<++e.>>>>-v.<<<<e.>>+n.>>> .<<s.>+w.<<<<
   ----a.>>n.>s.>> .<<<<<a.++>>>>> .<<s.->w.<<<+i.>-m.m.<i.>+n.-<--g.+>>>>.  >>>>>>>>>>>>>>>>>>>>>>-]
   6< [<<<<<<<<<<<<<<<<<<<<<< <+s.<<+i.>>>+x.> .<<<<--g.<++e.e.>>>s.-<<<e.>>>>> 
   .<<<<<----a.>>>>> .<<<-l.<<a.++>>>>+y.--<<<++i.>++n.-<--g.+>>>> .  >>>>>>>>>>>>>>>>>>>>>-]
   5< [<<<<<<<<<<<<<<<<<<<<< <<<--f.+++i.>>>-v.+<<<<++e.>>>>> .<<<<--g.>++o.---
   l.<<-d.+e.-->>++n.>>> .<<r.<<++i.>n.-<--g.+>>+s.->> .   >>>>>>>>>>>>>>>>>>>>-]
   4< [<<<<<<<<<<<<<<<<<<<< <<<--f.>++o.>>--u.++<r.>> .<<<<<c.--a.>>---l.l.<+++
   i.>++n.-<--g.>>>> .<<<<<+b.>++i.->>r.<<<++d.->>>+s.->> .  >>>>>>>>>>>>>>>>>>>-]
   3< [<<<<<<<<<<<<<<<<<<< <++t.<<h.>>--r.<<<++e.e.>>>>> .<<<<--f.>>r.<<<e.>>+
   n.<<--c.>++h.>>>> .<<<<h.<++e.-->>n.->+s.->> .    >>>>>>>>>>>>>>>>>>-]
   2< [<<<<<<<<<<<<<<<<<<<++t.>w.<<++o.>>>.<<t.>--u.<--r.++t.<---l.<<++e.>>>>>.
   <<<<<-d.>>+++0.>>+v.<<<<+e.>>>-s.>>.<<<<<----a.>>-n.-<<+++d.->>>->+>.
   >>>>>>>>>>>>>>>>>-] 
   1<[<<<<<<<<<<<<<<<<<<<<<--a.>>>>>.<<p--.<<<a.>>>++r.++t.--r.<<+i.<+++d.>--g.<+
   e.>>>>>.<<<<++i.>+n.>>>.<<<<<----a.>>>>>.<<<++p.<<++++e.----a.>>>r.>>.<<++
   t.--r.<<<++++e..>>>>>.<<<<<-->->--->>>>>>>>>>>>>>>>>>>-]
   <<<<<<<<<<<<<<<<<<<<<<..>>>>>>>->+
   [>+>>>>>>>>>>>>>[>]+[<]<<<<<<<<<<<<<-]>[<+>-]<<
]

Common Lisp, 333 363

(dotimes(n 12)(format t"on-the-~:R-day-of-christmas
my-true-love-gave-to-me
~v*~@{~R-~A
~#[and-~]~}a-PARTRIDGE-IN-A-PEAR-TREE

"(1+ n)(- 22 n n)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))

The builtin facilities to format ordinals are helpful, but most of the compression comes from being able to use the same argument list over and over, skipping over fewer and fewer arguments at each run.

As proven by coredump in the comments, the builtin facilities can still be put to good use for the cardinals.


Perl, 438 291 chars

Inspired by Jeff Burdges's use of DEFLATE compression, Ventero's compressed Ruby code and J B's use of Lingua::EN::Numbers, I managed to compress my entry down to 291 chars (well, bytes) including decompression code. Since the program contains some non-printable characters, I've provided it in MIME Base64 format:

dXNlIENvbXByZXNzOjpabGliO2V2YWwgdW5jb21wcmVzcyAneNolkMFqAkEMhu8+RVgELdaIXmXB
S2/FFyhF4k7cHTqTsclMZd++M3pJvo+QH5JiDJ9exkKrj/PqXOKV1bod77qj9b2UeGBZ7w/bpd9s
3rCDruf3uWtwS3qS/vfROy0xsho+oWbB3d+b19YsJHWGhIHp5eQ8GzqSoWkk/xxHH36a24OkuT38
K21kNm77ND81BceCWtlgoBAq4NWrM7gpyzDhxGKQi+bA6NIfG5K4/mg0d0kgTwwdvi67JHVeKKyX
l3acoxnSDYZJveVIBnGGrIUh1BQYqZacIDKc5Gvpt1vEk3wT3EmzejcyeIGqTApZmRftR7BH3B8W
/5Aze7In

To unencode the program, you can use the following helper Perl script:

use MIME::Base64;
print decode_base64 $_ while <>;

Save the output in a file named 12days.pl and run it with perl -M5.01 12days.pl. As noted, you need to have the Lingua::EN::Numbers module installed for the code to work.

In case you're wondering, the readable part of the code simply looks like this:

use Compress::Zlib;eval uncompress '...'

where the ... stands for 254 bytes of RFC 1950 compressed Perl code. Uncompressed, the code is 361 chars long and looks like this:

use Lingua'EN'Numbers"/e/";s==num2en(12-$i++)." "=e,y"." "for@n=qw=drummers.drumming pipers.piping lords.a.leaping ladies.dancing maids.a.milking swans.a.swimming geese.a.laying golden.rings calling.birds french.hens turtle.doves.and=;say"on the ".num2en_ordinal($_)." day of christmas my true love gave to me @n[$i--..@n]a partridge in a pear tree
"for 1..12

Writing this code was a weird kind of golfing exercise: it turns out the maximizing repetition and minimizing the number of distinct characters used are much more important than minimizing raw character count when the relevant metric is size after compression.

To squeeze out the last few chars, I wrote a simple program to try small variations of this code to find the one that compresses best. For compression, I used Ken Silverman's KZIP utility, which usually yield better compression rations (at the cost of speed) than standard Zlib even at the maximum compression settings. Of course, since KZIP only creates ZIP archives, I then had to extract the raw DEFLATE stream from the archive and wrap it in a RFC 1950 header and checksum. Here's the code I used for that:

use Compress::Zlib;
use 5.010;

@c = qw(e i n s);
@q = qw( " );
@p = qw( = @ ; , );
@n = ('\n',"\n");

$best = 999;

for$A(qw(e n .)){ for$B(@q){ for$C(@q,@p){ for$D(@p){ for$E(@q,@p){ for$F(qw(- _ . N E)){ for$G("-","-"eq$F?():$F){ for$H(@c){ for$I(@c,@p){ for$N(@n){ for$X(11,"\@$I"){ for$Y('$"','" "',$F=~/\w/?$F:()){ for$Z('".num2en_ordinal($_)."'){
    $M="Lingua'EN'Numbers";
    $code = q!use MB/A/B;sDDnum2en(12-$H++).YDe,yCFC Cfor@I=qwEdrummersFdrumming pipersFpiping lordsGaGleaping ladiesFdancing maidsGaGmilking swansGaGswimming geeseGaGlaying goldenFrings callingFbirds frenchFhens turtleFdovesFandE;say"on the Z day of christmas my true love gave to me @I[$H--..X]a partridge in a pear treeN"for 1..12!.$/;
    $code =~ s/[A-Z]/${$&}/g;

    open PL, ">12days.pl" and print PL $code and close PL or die $!;
    $output = `kzipmix-20091108-linux/kzip -b0 -y 12days.pl.zip 12days.pl`;
    ($len) = ($output =~ /KSflating\s+(\d\d\d)/) or die $output;

    open ZIP, "<12days.pl.zip" and $zip = join("", <ZIP>) and close ZIP or die $!;
    ($dfl) = ($zip =~ /12days\.pl(.{$len})/s) or die "Z $len: $code";

    $dfl = "x\xDA$dfl" . pack N, adler32($code);
    $dfl =~ s/\\(?=[\\'])|'/\\$&/g;

    next if $best <= length $dfl;
    $best = length $dfl;
    $bestcode = $code;
    warn "$A$B$C$D$E$F$G$H$I $X $Y $best: $bestcode\n";

    open PL, ">12days_best.pl" and print PL "use Compress::Zlib;eval uncompress '$dfl'" and close PL or die $!;

}}}}}}
    print STDERR "$A$B$C$D$E$F\r";
}}}}}}}

If this looks like a horrible kluge, it's because that's exactly what it is.


For historical interest, here's my original 438-char solution, which generates nicer output, including line breaks and punctuation:

y/_/ /,s/G/ing/for@l=qw(twelve_drummers_drummG eleven_pipers_pipG ten_lords-a-leapG nine_ladies_dancG eight_maids-a-milkG seven_swans-a-swimmG six_geese-a-layG five_golden_rGs four_callG_birds three_french_hens two_turtle_doves);s/e?t? .*/th/,s/vt/ft/for@n=@l;@n[9..11]=qw(third second first);say map("\u$_,\n","\nOn the $n[11-$_] day of Christmas,\nMy true love gave to me",@l[-$_..-1]),$_?"And a":A," partridge in a pear tree."for 0..11

Highlights of this version the pair of regexps s/e?t? .*/th/,s/vt/ft/, which construct the ordinals for 4 to 12 from the cardinals at the beginning of the gift lines.

This code can, of course, also be compressed using the Zlib trick described above, but it turns out that simply compressing the output is more efficient, yielding the following 338-byte program (in Base64 format, again):

dXNlIENvbXByZXNzOjpabGliO3NheSB1bmNvbXByZXNzICd42uWTwU7DMAyG730KP8DGOyA0bsCB
vYBp3MYicSo7W9e3xx3ijCIQDHZIUjn683+/k3ZPAjUSDKxWIeACZYC7qGw1o226hwWqHghSORKM
6FMtkGnT3cKEWpXDSMACCBOhQlWim+7jUKO+SGg5dT8XqAetiSD4nrmPBMDPvXywtllF18OgJH2E
SGJfcR+Ky2KL/b0roMeUWEZ4cXb7biQeGol4LZQUSECdyn4A0vjUBvnMXCcYiYy2uE24ONcvgdOR
pBF9lYDNKObwNnPOTnc5kYjH2JZotyogI4c1Ueb06myXH1S48eYeWbyKgclcJr2D/dnwtfXZ7km8
qOeUiXBysP/VEUrt//LurIGJXCdSWxeHu4JW1ZnS0Ph8XOKloIecSe39w/murYdvbRU+Qyc=

I also have a 312-byte gzip archive of the lyrics, constructed from the same DEFLATE stream. I suppose you could call it a "zcat script". :)