12 Days of Christmas Şarkı Sözü


17

Bunun herkes için eğlenceli bir meydan okuma olacağını düşündüm ve insanların getirdiği çözümleri görmeyi merak ediyorum.

"12 Days Of Christmas" şarkı sözlerini yazdır

On the first day of Christmas, 
my true love gave to me,
A partridge in a pear tree. 

On the second day of Christmas, 
my true love gave to me,
Two turtle doves, 
And a partridge in a pear tree. 

...

On the twelfth day of Christmas,
My true love gave to me,
Twelve drummers drumming,
Eleven pipers piping,
Ten lords-a-leaping,
Nine ladies dancing,
Eight maids-a-milking,
Seven swans-a-swimming,
Six geese-a-laying,
Five golden rings,
Four calling birds,
Three french hens,
Two turtle doves,
And a partridge in a pear tree.

kurallar

  • Büyük harf kullanımı konusunda endişelenmenize gerek yok; metnin tamamı büyük / küçük harfe duyarlı olmayabilir
  • Noktalama işaretlerini göz ardı edebilirsiniz: tireler boşluk olabilir, virgül ve nokta yok sayılabilir
  • Her ayet arasında boş bir çizgi olmalıdır
  • Numaralarınızı sıraya koymalısınız: " Noel'in ilk günü", " Dört çağıran kuş", vb.

3
Her satırın tam sürümünü sağlayabilir misiniz? "Gerçek aşkım bana verdi " ye alışkınım ve farklı versiyonların kullanılması çözümleri etkileyebilir.
Matthew

Tam şarkı sözleri güncellendi.
macek

Bu, "cümle başkentlerini bırakabilirsiniz" mi yoksa "metnin tamamı büyük / küçük harfe duyarsız" mıdır?
JB

Ayrıca, noktalama işaretlerini göz ardı etme doğrultusunda, boşluk (ve karşılıklı olarak) için noktalama işaretlerini değiştirebilir miyiz?
JB

1
@macek: daha iyi, ama sorumun gizli tarafı şuydu: boşluklar yerine tire yazdırabilir miyim?
JB

Yanıtlar:


23

Brainfuck - 2.974

Bununla gurur duyuyorum. Bu oldukça büyük bir sayı gibi görünüyor, ancak unutmayın, herhangi bir harici sıkıştırma kütüphanesi kullanmadım ve orijinal metnin hiçbiri programımda hiçbir yerde yok. Diğer gönderilerin hiçbiri bunu söyleyemez. Bunların hepsi elle kodlanmış. Daha saf metin üreteçleri bu metin için 39k'den fazla verdi, bu yüzden bunun önemli bir gelişme olduğunu söyleyebilirim.

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

Ne yazık ki, bu kendi çıktısından yaklaşık 600 karakter daha uzun, ama her neyse. Bir dizide c, h, m, r, w karakterlerini tutar ve bunu tüm metni yazdırmak için kullanır. On iki boşluğun sağındaki iki dizi, sayı için hangi gün açık olduğumuzu ve hangi öğeler için çıktı alabileceğimizi takip eder. Ben böyle uzun zincirleri önlemek için iki sayım diziler arasına baskı karakterleri getirmek için hafıza haritası yeniden düzenleyerek bunu biraz optimize etmek mümkün olabilir <<<<<<<ve >>>>>>ancak bu noktada çok iş olacaktır. Ayrıca, artan / azalmayı en aza indirmek için frekans analiziyle daha iyi tohum karakterleri seçebilirim, ama her neyse.

Bu, düzgün çalışması için 8 bitlik sarma hücrelerine bağlıdır.

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

1
Bu boyutta (golf skoru için sayılmamış) gönderimleri hakkında satır içi yorumları tolere ediyorum. Yine de + 1'ledi.
JB

Tabi ki. Çözülmemiş sürüm eklendi. Gün numaraları için on ikiden yalnızca bir bit ayarlanır ve çıktı alındıktan sonraki gün bitini ayarlar. Öğeler için, son satırın yanındaki satır üstündeki n biti ve tüm etkin get çıktısını ayarlar.
captncraig

10

Perl, 438291 karakter

Jeff Burdges'in DEFLATE sıkıştırmasını , Ventero'nun sıkıştırılmış Ruby kodunu ve JB'nin Lingua :: EN :: Numbers'ı kullanmasından esinlenerek , girişimi dekompresyon kodu dahil 291 karaktere (kuyu, bayt) kadar sıkıştırmayı başardım. Program yazdırılamayan bazı karakterler içerdiğinden, MIME Base64 biçiminde sağladım :

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

Programın kodunu çözmek için aşağıdaki yardımcı Perl betiğini kullanabilirsiniz:

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

Çıkışı adlı bir dosyaya kaydedin 12days.plve ile çalıştırın perl -M5.01 12days.pl. Belirtildiği gibi, kodun çalışması için Lingua :: EN :: Numbers modülünün kurulu olması gerekir.

Merak ediyorsanız, kodun okunabilir kısmı şöyle görünür:

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

burada ...254 bayt RFC 1950 sıkıştırılmış Perl kodu anlamına gelir. Sıkıştırılmamış, kod 361 karakter uzunluğundadır ve şöyle görünür:

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

Bu kodu yazmak garip bir golf egzersizi türüydü: tekrarlamayı en üst düzeye çıkarmak ve kullanılan farklı karakter sayısını en aza indirmek, ilgili metrik sıkıştırmadan sonra boyut olduğunda ham karakter sayısını en aza indirmekten çok daha önemlidir .

Son birkaç karakteri sıkıştırmak için, en iyi sıkıştırılanı bulmak için bu kodun küçük varyasyonlarını denemek için basit bir program yazdım. Sıkıştırma için, maksimum sıkıştırma ayarlarında bile standart Zlib'den daha iyi sıkıştırma oranları (hız maliyetiyle) veren Ken Silverman'ın KZIP yardımcı programını kullandım . Tabii ki, KZIP sadece ZIP arşivleri oluşturduğundan, ham DEFLATE akışını arşivden çıkarmak ve bir RFC 1950 üstbilgisi ve sağlama toplamına sarmak zorunda kaldım. İşte bunun için kullandığım kod:

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";
}}}}}}}

Bu korkunç bir kluge gibi görünüyorsa, bunun nedeni tam olarak budur.


Tarihsel ilgi için, satır sonları ve noktalama işaretleri de dahil olmak üzere daha güzel çıktı üreten orijinal 438 karakterlik çözümüm:

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

Bu sürümün s/e?t? .*/th/,s/vt/ft/, hediye hatlarının başlangıcında kardinallerden 4 ila 12 arasında sıra sıralarını oluşturan regexps çiftini vurgular .

Bu kod, elbette, yukarıda açıklanan Zlib hilesi kullanılarak da sıkıştırılabilir, ancak çıktıyı sıkıştırmanın daha verimli olduğu ve aşağıdaki 338 baytlık programı (yine Base64 formatında) sağladığı ortaya çıkar :

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

Aynı DEFLATE akışından oluşturulan 312 baytlık bir gzip şarkı sözleri arşivim var. Sanırım buna "zcat betiği" diyebilirsiniz. :)


Görünüşe göre 2 karakter kaydetmek için ringsile değiştirebilirsinizrGs
macek

@macek: benim orijinal versiyonunda ben yerine çünkü ben, yapamadım yılında Gbirlikte ing,, ancak virgül ekleyerek daha sonra gerçekten de daha kısa olduğu ortaya çıktı. Teşekkürler!
Ilmari Karonen

'Salt okunur bir değerin değiştirilmesi' hatasını nasıl önlersiniz?
Jeff Burdges

@JeffBurdges: Orijinal versiyonda mı? Önce dizeleri bir diziye atayarak.
Ilmari Karonen

"/Opt/local/lib/perl5/5.12.3/Compress/Zlib.pm satır 357'de salt okunur bir değerin değiştirilmesi denendi." $_Aşağıdaki güncellememde atayarak bu sorundan kaçındım .
Jeff Burdges

10

Yaygın Lisp, 333363

(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))

Ordinalleri biçimlendirmek için yerleşik özellikler yararlıdır, ancak sıkıştırmanın çoğu aynı argüman listesini tekrar tekrar kullanabilmek ve her çalışmada daha az sayıda argümanı atlamaktan gelir.

Yorumlarda coredump tarafından kanıtlandığı gibi , yerleşik tesisler kardinaller için hala iyi bir şekilde kullanılabilir.


Belki "oniki", "onbir", "on", ... ile başlayan satırları biraz daha sıkıştırabilirsiniz
coredump

Belki de öyledir. Önerin nedir?
JB

1
İlk önce kalan sayıda argümanın kullanılabileceğini umuyordum, ancak spesifikasyonu okudum ve yapamam. Sahip olduğum kısalar 333 karakter:(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))
coredump

Şimdi konuşuyoruz. Benim hayalim atlama sayacını sıra ile paylaşmak olurdu, ama bunu yapmanın kısa bir yolunu bulamadım.
JB

7

JavaScript 570

Bu benim ilk golf oyunum. JavaScript 570

var l=["first","second","third","fourth","fifth","sixth","seventh","eigth","nineth","tenth","eleventh","twelth","Two turtle doves","Three french hens","Four calling birds","Five golden rings","Six geese-a-laying","Seven swans-a-swimming","Eight maids-a-milking","Nine ladies dancing","Ten lords-a-leaping","Eleven pipers piping","Twelve drummers drumming"];var b = "<br/>";for(var i=0;i<12;i++){var p="On the "+l[i]+"day of Christmas"+b+"My true love gave to me"+b;for(var x=i;x>0;x--)p+=l[13+x]+b;if(i>0)p+="and ";p+="a partridge in a pear tree"+b+b;document.write(p);}

6

Python 2.7 (465)

for k in range(12):
 print'On the %s day of Christmas\nMy true love gave to me'%'first^second^third^fourth^fifth^sixth^seventh^eighth^ninth^tenth^eleventh^twelfth'.split('^')[k]
 print'\n'.join('Twelve drummers drumm*Eleven pipers pip*Ten lords-a-leap*Nine ladies danc*Eight maids-a-milk*Seven swans-a-swimm*Six geese-a-lay*Five golden rings^Four calling birds^Three french hens^Two turtle doves and^a partridge in a pear tree^'.replace('*','ing^').split('^')[11-k:])

Ancak, keklik yerine güvercinlerle aynı satıra 've' koydum.


1
Benimle aynı yazım hatasını yaptın: "onikinci"
Andrew Shepherd

Bu bana başka bir karakter kazandırıyor ... Teşekkürler!
Daan

5

Yakut (474)

(0..11).each{|i|puts "On the #{"first^second^third^fourth^fifth^sixth^seventh^eighth^ninth^tenth^eleventh^twelfth".split("^")[i]} day of Christmas\nMy true love gave to me";puts "a partridge in a pear tree\n\n^two turtle doves and^three french hens^four calling birds^five golden rings^six geese-a-lay*Seven swans-a-swimm*Eight maids-a-milk*Nine ladies danc*Ten lords-a-leap*Eleven pipers pip*Twelve drummers drumming".gsub('*','ing^').split('^')[0..i].reverse.join("\n")}

veya daha okunabilir bir biçimde (486):

(0..11).each do |i|
    puts "On the #{"first^second^third^fourth^fifth^sixth^seventh^eighth^ninth^tenth^eleventh^twelfth".split("^")[i]} day of Christmas\nMy true love gave to me"
    puts "a partridge in a pear tree\n\n^two turtle doves and^three french hens^four calling birds^five golden rings^six geese-a-lay*Seven swans-a-swimm*Eight maids-a-milk*Nine ladies danc*Ten lords-a-leap*Eleven pipers pip*Twelve drummers drumming".gsub('*','ing^').split('^')[0..i].reverse.join("\n")
end

Herkes nasıl .reverse atlatmak için bir fikrim var? bir çözüm bulamadım


* İle hilenizi "ing" i temsil etmeyi seviyorum. Bunu yaklaşık 440'a kadar indirebilmelisiniz: use 12.timesyerine (0..11).each; tek bir argüman ile iki argüman yerine iki argüman ile tek bir açıklama yapın; Noel günleri dizisi için% w () gösterimini kullanın. Son olarak, listeyi tersine çevirerek, dizenin sonuna fazladan bir ^ ekleyerek ve sonra [-i..-1][0..i] yerine kullanarak tersinden kurtulabilirsiniz.
Wayne Conrad

"davul çalmayı" "davul *" olarak değiştiremez misiniz?
yeraltı

5

Perl, 500 485

@s=(first,second,third,fourth,fifth,sixth,seventh,eighth,ninth,tenth,eleventh,twelfth);
$;=ing;
@a=(
"Twelve drummers drumm$;",
"Eleven pipers pip$;",
"Ten lords-a-leap$;",
"Nine ladies danc$;",
"Eight maids-a-milk$;",
"Seven swans-a-swimm$;",
"Six geese-a-lay$;",
"Five golden r$;s",
"Four call$; birds",
"Three french hens",
"Two turtle doves\nAnd "
);
for(0..11){
print"\n\nOn the $s[$_] day of Christmas\nMy true love gave to me\n";
$"=$/;
print"@b";
unshift@b,pop@a;
print"A partridge in a pear tree"
}

Bu benim ilk denemem ve eminim ki çok daha kısa yapılabilir. Satır kesmeleri okunabilirlik içindir. Biri her gün için isim tutan @s, biri tüm hediyeleri (birincisi hariç) @alisteleyen ve birincisi verilen hediyeleri listeleyen üç önemli diziye sahiptir @b. Ana mekanizma her gün, o yazdırır olmasıdır @bve o andan itibaren bir ek hediye aktarır @aiçin @b.

Andrew'a 500-> 485 için teşekkürler


Eğer yerini alabilir ringsile r$1s1'e daha kömürü kaydetmek için
Macek

@macek Bunu yapamam çünkü perl s, değişken isminin bir parçası olarak yorumlanacak ve değişken $ismevcut değil. (Onlar aslında olanlar yerine btw)
PhiNotPi

eigth->eighth
Matthew

Sen yerini alabilir $i, diyelim ki, birlikte $;o çevrede alır. Hiç kimse $;amaçlanan amaç için kullanmaz .
Ilmari Karonen

@IlmariKaronen Tavsiyeni aldım, ama bir yazım hatası düzelttim, böylece karakter sayısı aynı kaldı.
PhiNotPi

5

Vim - 578 tuş vuruşları

Ben denemek ve vim-golf bu karar verdim, çünkü bu vim-golfed olabilir bir şeydir.

Çerçeveyi ekleyerek başlayın - "Noel'in X günü" satırı toplam 12 kez (89 tuş vuruşu):

                                         KS   TL   GT
12iOn the X day of Christmas,<Enter>     30   30   30
my true love gave to me,<Enter>          25   55   55
and a partridge in a pear tree.<Enter>   32   87   87
<Enter><Esc>                              2   89   89

Ardından, şarkı sözleri için olması gereken yerlere 2 ila 12 arasındaki sayıları ekleyecek bir dizi makro gerçekleştirin (172 tuş vuruşu):

                                      KS   TL   GT
42kqmO2<Esc>9/a<Enter>q10@m           17   17  106
dw                                     2   19  108
6jqm/2<Enter>O3<Esc>jq9@m             14   33  122
/3<Enter>qm/3<Enter>O4<Esc>jq8@m      16   49  138
/4<Enter>qm/4<Enter>O5<Esc>jq7@m      16   65  154
/5<Enter>qm/5<Enter>O6<Esc>jq6@m      16   81  170
/6<Enter>qm/6<Enter>O7<Esc>jq5@m      16   97  186
/7<Enter>qm/7<Enter>O8<Esc>jq4@m      16  113  202
/8<Enter>qm/8<Enter>O9<Esc>jq3@m      16  129  218
/9<Enter>qm/9<Enter>O10<Esc>jq2@m     17  146  235
/10<Enter>qm/10<Enter>O11<Esc>jq@m    18  164  253
?11<Enter>O12<Esc>                     8  172  261

İkinci satırdaki "dw" ilk "ve" 'den kurtulmaktır, çünkü oraya gitmez.

Ardından, gerçek sevginin verdiği sayıların yerine bir dizi ikame gerçekleştirin (319 tuş vuruşu):

                                       KS   TL   GT
:%s/12/twelve drummers drumming,/g     34   34  295
:%s/11/eleven pipers piping,/g         30   64  325
:%s/10/ten lords-a-leaping,/g          29   93  354
:%s/9/nine ladies dancing,/g           28  117  382
:%s/8/eight maids-a-milking,/g         30  147  412
:%s/7/seven swans-a-swimming,/g        31  178  443
:%s/6/six geese-a-laying,/g            27  205  366
:%s/5/five golden rings,/g             26  231  392
:%s/4/four calling birds,/g            27  268  419
:%s/3/three french hens,/g             26  294  445
:%s/2/two turtle doves,/g              25  319  470

Ve son olarak, her oluşumunu Xbir sıra numarasıyla değiştirmek:

                         KS   TL   GT
/X<Enter>sfirst<Esc>     10   10  480
nssecond<Esc>             9   18  488
nsthird<Esc>              8   27  497
nsfourth<Esc>             9   36  506
nsfifth<Esc>              8   44  514
nssixth<Esc>              8   52  522
nsseventh<Esc>           10   62  532
nseighth<Esc>             9   71  541
nsninth<Esc>              8   79  549
nstenth<Esc>              8   87  557
nseleventh<Esc>          11   98  568
nstwelfth<Esc>           10  108  578

Ve işimiz bitti!


Eminim kaçırdığım başka optimizasyonlar var, ama bence bu oldukça iyi.


Ah, kaçışları unuttum.
Joe Z.

/ :%s/2/two turtle doves,
G'yi

4

C (644)

Count, sunum için kullanılan boşluk içermez.

#include <stdio.h>

void main() {
    char *e = "On the \0 day of Christmas my true love gave to me\0 Twelve drummers drumming Eleven pipers piping Ten lords-a-leaping Nine ladies dancing Eight maids-a-milking Seven swans-a-swimming Six geese-a-laying Five golden rings Four calling birds Three french hens Two turtle doves and A partridge in a pear tree\n\n";
    printf("%sfirst%s%s%ssecond%s%s%sthird%s%s%sfourth%s%s%sfifth%s%s%ssixth%s%s%sseventh%s%s%seighth%s%s%sninth%s%s%stenth%s%s%seleventh%s%s%stwelfth%s%s",
           e, e+8, e+276, e, e+8, e+255, e, e+8, e+237, e, e+8, e+218, e, e+8, e+200, e, e+8, e+181, e, e+8, e+158, e, e+8, e+136, e, e+8, e+116, e, e+8, e+96, e, e+8, e+75, e, e+8, e+50);
}

Çıktı şöyle:

On the first day of Christmas my true love gave to me A partridge in a pear tree

...

On the twelfth day of Christmas my true love gave to me Twelve drummers drumming Eleven pipers piping Ten lords-a-leaping Nine ladies dancing Eight maids-a-milking Seven swans-a-swimming Six geese-a-laying Five golden rings Four calling birds Three french hens Two turtle doves and A partridge in a pear tree

4

PowerShell, 487 453

0..11 | % {
   'On the {0} day of christmas my true love gave to me {1}`n'-f
   (
        'first^second^third^fourth^fifth^sixth^seventh^eighth^ninth^tenth^eleventh^twelfth'.Split('^')[$_],
        (
            'a partridge in a pear tree^two turtle doves and^three french hens^four calling birds^five golden rings^six geese-a-laying^Seven swans-a-swimming^Eight maids-a-milking^Nine ladies dancing^Ten lords-a-leaping^Eleven pipers piping^Twelve drummers drumming'.Split('^')[$_..0]-join' '
        )
    )
 }

Birleştirilmiş bir dizeyi bölme fikri için Daan'a teşekkür ederiz .

Başlangıçta ilk ayet dışında herkes için keklik "ve" almak için bir anahtarlama ifadesi dahil. Ancak, soru bizi noktalama işaretlerini ortadan kaldırdığı için, güvercinlere "ve" yi ekleyebiliriz.

Bu, satır beslemelerine şu şekilde sonuçlanır:

On the first day of christmas my true love gave to me a partridge in a pear tree

On the second day of christmas my true love gave to me two turtle doves and a partridge in a pear tree

On the third day of christmas my true love gave to me three french hens two turtle doves and a partridge in a pear tree

Onikinci, oniki değil.
Joey Adams

@Joey Adams - Beni düzelttiğiniz ve bana bir karakter daha verdiğiniz için teşekkür ederim :-)
Andrew Shepherd

4

Perl, 368 389 (bir Unicode / kompresyon)

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

Koşumlar Lingua :: EN :: Sayılar ,% 100 olmadığım halde, modülü ve tanımlayıcı adlarının uzunluklarını gördüğümde iyi bir fikir olduğuna ikna oldum. Perl 5.10 veya üst sürümünü gerektirir, komut satırından bir -Eanahtarla çalıştırın.

Düzenleme: küçük iyileştirmeler: bir dizi kullanmayı bırakın, daha iyi kullanımı $_, gereksiz boşluk.


+1, Harika! Birisi standart dışı bir modül kullanmaktan şikayet edebilir, ancak herhangi bir dilin kullanılmasına izin veriyorsak (GolfScript gibi özel amaçlı olanlar dahil), neden "Perl + Lingua :: EN :: Sayılar" t bir çözüm için geçerli bir dil olmalıdır. Yine de bir "Acme :: 12Days" modülü yazmak ve CPAN'a göndermek muhtemelen aldatıyor. :)
Ilmari Karonen

@Ilmari Karonen insanlar şikayet ettiğinde, genellikle dili "CPAN" olarak yeniden adlandırıyorum. Sık sık olmaz.
JB

2
Ilmari, Golfscript özel amaçlı bir dil değil.
Joey

2
More like an "accidentally general"-purpose language :D
J B

1
Dear lord, why can't people golf their own code anymore?
J B

4

PowerShell, 440

-join('On the 1 day of Christmas
my true love gave to me
1a partridge in a pear tree

1first1second1third1fourth1fifth1sixth1seventh1eighth1ninth1tenth1eleventh1twelfth1Twelve drummers drumm7Eleven pipers pip7Ten lords-a-leap7Nine ladies danc7Eight maids-a-milk7Seven swans-a-swimm7Six geese-a-lay7Five golden rings
1Four calling birds
1Three french hens
1Two turtle doves
And '-replace7,'ing
1'-split1)[(26..15|%{0
29-$_
1
$_..26-le25
2})]

This prints the lyrics as given in the question with multiple lines per verse. We can save a few characters if that requirement isn't there.


+1 Holey crap. You got this working with line feeds as well as the "And" appearing on the last line.
Andrew Shepherd

Well, printing a different text was never an option anyway, and the “And” in the last line uses the same trick like everyone else. Still, I wanted to preserve line breaks which the other solution does not (while being longer, too).
Joey

3

C# (528)

class P{static void Main(){for(int i=1;i<12;i++){Console.WriteLine("On the "+"^first^second^third^fourth^fifth^sixth^seventh^eighth^ninth^tenth^eleventh^twelfth".Split('^')[i]+" day of christmas my true love gave to me "+"a partridge in a pear tree^two turtle doves and^three french hens^four calling birds^five golden rings^six geese-a-laying^Seven swans-a-swimming^Eight maids-a-milking^Nine ladies dancing^Ten lords-a-leaping^Eleven pipers piping^Twelve drummers drumming".Split('^').Take(i).Aggregate("",(t,s)=>s+' '+t));}}}

2

Java, 2062

I know this was posted a while ago, but I thought I would try. I'm a student and still new to this but its seems to work.

public class TwelveDaysOfChristmas 
{

    public static void main(String[] args) 
    {
        String[] days = new String[12];
        days[0] = "and a partriage in a pear tree.";
        days[1] = "Two turtle doves, ";
        days[2] = "Three french hens, ";
        days[3] = "Four callings birds, ";
        days[4] = "Five diamond rings, ";
        days[5] = "Six geese a-laying, ";
        days[6] = "Seven swans a-swimming, ";
        days[7] = "Eight maids a-milking, ";
        days[8] = "Nine ladies dancing, ";
        days[9] = "Ten lords a-leaping, ";
        days[10] = "Eleven pipers piping, ";
        days[11] = "Twelve twelve drummers drumming, ";


        System.out.println(chorus(0));
        System.out.println("a partriage in a pear tree");

        for(int i = 1; i<days.length; i++)
        {
            System.out.println(chorus(i));

            for(int x = i; x>=0; x--)
            {
                System.out.println(days[x]);
            }
            System.out.println();
        }
    }

    public static String chorus(int line)
    {
        String chorus = "On the " + getLine(line) + " day of Christmas my true " +
                "love gave to me, ";

        return chorus;
    }

    public static String getLine(int line)
    {
        int num = line;
        String result = "first";
        switch (num)
        {
        case 1:  result = "second";
                 break;
        case 2:  result = "third";
                 break;
        case 3:  result = "fourth";
                 break;
        case 4:  result = "fifth";
                 break;
        case 5:  result = "sixth";
                 break;
        case 6:  result = "seventh";
                 break;
        case 7:  result = "eighth";
                 break;
        case 8:  result = "ninth";
                 break;
        case 9: result = "tenth";
                 break;
        case 10: result = "eleventh";
                 break;
        case 11: result = "twelfth";
                 break;
        }

        return result;
    }

}

Hi Gina, welcome to the site!
Tynam

3
Congrats on a first solution; works fine. The contest with code golf is 'shortest code possible', so it's standard on this site to do a few things you'd never do in normal code: cut variable and function names to one character, cut whitespace, and a few other things. (It's OK, and expected, to also include the full-length version as you did here to make the approach clearer.) Having done that, you can then ask yourself: "how can I make this shorter?"
Tynam

2

Swift, 577

import UIKit
let f=NSNumberFormatter()
f.numberStyle = .SpellOutStyle
for i in 0...11{
let w = split("first-second-third-four-fif-six-seven-eigh-nin-ten-eleven-twelf"){$0=="-"}[i]+(i<3 ?"":"th")
println("On the \(w) day of Christmas\nmy true love gave to me")
for m in reverse(0...i){
if m==0{break}
let v = split("turtle doves and*french hens*calling birds*golden rings*geese-a-lay*swans-a-swimm*maids-a-milk*ladies danc*lords-a-leap*pipers pip*drummers drumm"){$0=="*"}[m-1]+(m<5 ?"":"ing")
println("\(f.stringFromNumber(m+1)!) \(v)")}
println("a partridge in a pear tree.")}

You can paste this in a playground.

I tried moving the v into the print command and got:

Playground execution failed: <EXPR>:20:1: error: expression was too complex to be solved in reasonable time; consider breaking up the expression into distinct sub-expressions

1

Ruby 1.9.3, compressed, 321 characters

Since the code contains non-printable characters, I'll post a hexdump of the code instead:

0000000: 2363 6f64 696e 673a 6269 6e61 7279 0a72  #coding:binary.r
0000010: 6571 7569 7265 277a 6c69 6227 3b65 7661  equire'zlib';eva
0000020: 6c20 5a6c 6962 2e69 6e66 6c61 7465 2778  l Zlib.inflate'x
0000030: da2d 90db 6ac3 300c 86ef f714 a163 b042  .-..j.0......c.B
0000040: 15e8 5ea7 f442 8be5 58cc 8720 39cd 42db  ..^..B..X.. 9.B.
0000050: 3dfb e4a4 3792 f559 c7ff fcd5 574e a4f7  =...7..Y....WN..
0000060: 073f a6b9 eaa1 64a8 81e0 fdfe b17c 7a16  .?....d......|z.
0000070: ad9d d250 b2eb 6a60 719d 2fb3 d4d0 79f6  ...P..j`q./...y.
0000080: 6695 7f9b a51b 65f3 c463 3097 b905 7547  f.....e..c0...uG
0000090: f1f5 5717 8a56 71bc f0f5 090e 5728 1e86  ..W..Vq.....W(..
00000a0: 20ac 35a1 bea5 15aa cc04 b1dc 0846 3453   .5..........F4S
00000b0: 0b24 3a9c 6c87 5669 c0c9 9c12 89ee 0fce  .$:.l.Vi........
00000c0: e3ab 374c 3c35 6cae 411b 6b5d c429 2044  ..7L<5l.A.k].) D
00000d0: c28d d942 d61a 1d93 5563 1eb6 e2b6 2b24  ...B....Uc....+$
00000e0: e42d 3371 fc69 74bb 0474 c1dc a82e bc4f  .-3q.it..t.....O
00000f0: b233 6124 526a 4d71 6dc8 73db b444 67f9  .3a$RjMqm.s..Dg.
0000100: 6240 3761 60c0 182d 826f 934a 4d31 2102  b@7a`..-.o.JM1!.
0000110: 2f94 8700 81b2 91a5 4035 01a3 1d64 b7da  /.......@5...d..
0000120: 1413 1661 42a9 c26e 24e0 6c33 2642 3141  ...aB..n$.l3&B1A
0000130: 888e 973f ee7b 385f 4fd3 f31f be35 9d6f  ...?.{8_O....5.o
0000140: 27                                       '

To create the actual code from the hexdump, put it in a file and run xxd -r hexdump > 12days.rb. Then executing ruby1.9.3 12.days.rb will run the code and print the lyrics. Note that this code requires Ruby 1.9.3 (because it uses Zlib.inflate) , so it won't work with Ruby 1.8.x, 1.9.1 and 1.9.2.

The uncompressed code is 425 characters long:

12.times{|i|puts"on-the-#{%w(first second third fourth fifth sixth seventh eighth ninth tenth eleventh twelfth)[i]}-day-of-christmas
my-true-love-gave-to-me",%w(twelve-drummers-drumming eleven-pipers-piping ten-lords-a-leaping nine-ladies-dancing eight-maids-a-milking seven-swans-a-swimming six-geese-a-laying five-golden-rings four-calling-birds three-french-hens two-turtle-doves-and a-partridge-in-a-pear-tree)[~i..-1],p}

1

Perl, 319/313

Idea : Uncompress and evaluate J B's Lingua::EN::Numbers solution.

First, paste this text block into the command perl -e 'use MIME::Base64; print decode_base64 $_ while <>;' >12days.pl. Next, run the command perl -M5.01 12days.pl.

dXNlIENvbXByZXNzOjpabGliOyRfPSd4nCWOwW7CMAyG730K
q8oBNIUOjq2QxmG3iT3AhJBpTBsRu12cgqpp776UXWx/v63/96QEH166Cev6/VjXx4kvFLWspCqb
N91/P1YHO2JM0buOrBeLdiSMNkUiSFNMgawb7qRwjSRtb3sShRZDyK724qNT6IbgSGzMSgYipewS
cM4M+kDRjPrwzIvA6N0isA+3hQM6T2odSvvEIT7XgXBcePRj/tfmtpCLE/PCzyEr68ac90a/Xk/N
dYiGV9vNZrtb/xjZy8Q7knP284LBcKM4l58CqVwnMAIOZxiu0PbRa2LUgmdIcaL8wZ2gw1zSAEyF
ORdlo9WhQnGA1RL4b70y/LJdb0rI+YZP+bD8Lf4A5ut+sic7ZXZhbCB1bmNvbXByZXNzJF87Cg==

The script itself takes the form use Compress::Zlib;$_='...';eval uncompress$_; where ... is J B's 368 char solution after being compressed with this command and escaping a '.

perl -M5.01 -e 'use Compress::Zlib; $_ .= <> while !eof; say compress($_);' <12days_JB.pl

Ilmari's script complains about modifying a read only value without the extra $_=...; characters, but presumably he'd make this 313. You could save several more bytes by tweaking the compression manually like Ilmari did before, maybe achieving 310 or so, but I didn't bother.


Perl, 376 (cheating off another submission) [my original submission]

First, create a perl script called 12days.pl containing :

use IO::Uncompress::Inflate qw(inflate);inflate\*DATA=>'-';
__DATA__

Next, pipe the output from any other submission into 12days.txt and execute the command :

perl -e 'use IO::Compress::Deflate qw(deflate); deflate "-" => "-";' <12days.txt >>12days.pl

Vola 12days.pl is around 376 bytes and prints the song. ;) Amusingly using rawinflate moves precisely six bytes from the data doc into the code starting from Ilmari's output.

I'd originally set about looking for a Huffman coding module directly, which isn't nearly so dishonest. Yet, sadly CPAN has no modules with English's letter entropy table, which is what you really want when compressing very short strings.

I found that fortune -m Days\ of\ Christmas does not work either, regrettably.


1

PHP, 548

$n=explode('|','first|second|third|fourth|fifth|sixth|seventh|eighth|ninth|tenth|eleventh|twelfth');$w=explode('|','and a partridge in a pear tree.|two turtle doves|three french hens|four calling birds|five golden rings|six geese a-laying|seven swans a-swimming|eight maids a-milking|nine ladies dancing|ten lords a-leaping|eleven pipers piping|twelve drummers drumming');foreach($n as $i=>$j)echo "on the $n[$i] day of christmas,\nmy true love sent to me\n".str_replace($i?'':'and ','',implode(",\n",array_reverse(array_slice($w,0,$i+1))))."\n\n";

Reduced length with compression, 502

eval(gzuncompress(base64_decode('eNpVkE1u3DAMhfc+BWEI8AzqDtJtg7QHCYKCsWiLrX4MUjPOAD58KE829UIyP5Hge8/lF/pYY/F0GvZhHGYWrbvSVLLfa2Dx+1yuUsM+82yn8kc76UbZbuIl2JW5FfWB4tdb3SjaxHB+dtv/OzB7QFhRqrBfCDi3klCgCtHFJgtU2xkJfLmRmg7jMAvlKUCgrIcmmDBGzgu8m0pDfCNYSvSUQQxr0woLkRLg94h3Yw/hoBtmNagbp9Tw4QMSsm84cfzXqNkiiOiZFDzmqTEzCbHI0RcJ12P6sAwrryTarqPR7JsgL9eUGj5+7MHymIsQTuHkzLeC45df7u+ZplCgLxlqIHD51fGbLb1DmWEKwloT6tilu2V0NVWWC6jlDLVAoq6/aJU/QmvEiU6Ofw/DzyNni3sYOT3S78euH1EE79Z6M1V0elQauY1t49Po+NuPs32Xvuv650+BSMT/')));

Not the kind of solution I like to read (yet another base-64/gzip, just great), but I really see no reason why you alone would deserve a negative answer score with that. Upvoted to bring some balance; whoever downvoted is requested to inform us of why.
J B

Many have provided a compressed solution so just for the fun of it I decided to put one also. But my original code is posted too, so I don't see what the problem is. Just ignore the gzipped one. I would also like to know why I was being downvoted.
Vladimir

1

VALA, 584, 574

void main(){int i=0;string[] d={"first","second","thrid","fourth","fifth","sixth","seventh","eighth","ninth","tenth","eleventh","twelfth"};string[] p={"Twelve drummers drumming","Eleven pipers piping","Ten lords-a-leaping","Nine ladies dancing","Eight maids-a-milking","Seven swans-a-swimming","Six geese-a-laying","Five golden rings","Four calling birds","Three french hens","Two turtle doves","A"};while(i<12){stdout.printf("On the %s day of Christmas,\nmy true love gave to me,\n%s partridge in a pear tree.\n\n",d[i],string.joinv(",\n",p[11-i:12]));p[11]="And a";i++;}}

No more warning at compilation.


1

Java, 608

First post on Stack Exchange, second attempt at this problem.

class T{public static void main(String[]args){String Z=" partridge in a pear tree.\n";String[] B=(" day of Christmas,\nMy true love gave to me0first0second0third0fourth0fifth0sixth0seventh0eighth0ninth0tenth0eleventh0twelfth0A"+Z+"0Two turtle doves,0Three french hens,0Four calling birds,0Five golden rings,0Six geese-a-laying,0Seven swans-a-swimming,0Eight maids-a-milking,0Nine ladies dancing,0Ten lords-a-leaping,0Eleven pipers piping,0Twelve drummers drumming,").split("0");for(int i=1;i<13;i++){System.out.println("On the "+B[i]+B[0]);for(int j=i+12;j>12;j--)System.out.println(B[j]);B[13]="And a"+Z;}}}

Java is a little cumbersome for tasks like this, but using split helped reduce the String overhead.


1

///, 439 bytes

/|/\/\///^/on the |%/ day of christmas
my true love gave to me
|=/-a-|&/ing|*/even|+/th%|:/

^|A/el* pipers pip&
B|B/ten lords=leap&
C|C/nine ladies danc&
D|D/eight maids=milk&
E|E/s* swans=swimm&
F|F/six geese=lay&
G|G/five golden r&s
H|H/four call& birds
I|I/three french hens
J|J/two turtle doves
and K|K/a partridge in a pear tree/^first%K:second%J:+ird%I:four+H:fif+G:six+F:s*+E:eigh+D:nin+C:ten+B:el*+A:twelf+twelve drummers drumm&
A

Try it online!

If trailing newlines are allowed, you can save four bytes:

/|/\/\///^/on the |%/ day of christmas
my true love gave to me
|=/-a-|&/ing|*/even|+/th%|A/el* pipers pip&
B|B/ten lords=leap&
C|C/nine ladies danc&
D|D/eight maids=milk&
E|E/s* swans=swimm&
F|F/six geese=lay&
G|G/five golden r&s
H|H/four call& birds
I|I/three french hens
J|J/two turtle doves
and K|K/a partridge in a pear tree

/^first%K^second%J^+ird%I^four+H^fif+G^six+F^s*+E^eigh+D^nin+C^ten+B^el*+A^twelf+twelve drummers drumm&
A

Try it online!

Explanation

/// is a language where the only operation is a self-modifying substitution. In particular, the instruction /abc/xyz/ replaces all instances of abc with xyz in the rest of the source code, including other substitutions.. Any other characters are simply output to STDOUT.

While this is enough for Turing-completeness, golfing in /// generally consists of starting with the intended output and identifying repeated substrings that can be replaced with single-character shortcuts.

\ can be used as an escape character in patterns, replacements, and literal characters to mean a literal / or \.


The first instruction encountered is /|/\/\//. This means "replace all | with // in the rest of the program." This saves a byte for every subsequent substitution in the program.


After this, a set of replacements are made to compress the text itself:

  • on the becomes ^.
  • day of christmas \n my true love gave to me \n becomes %.
  • -a- becomes =.
  • ing becomes &.
  • even becomes *.
  • th% becomes +.
  • ^ preceded by two newlines (which appears in every verse but the first) becomes :.

Following this, we write the lyrics themselves. This is done using replacements A through K. Each letter replacement adds a line to the replacement after it. For example, K represents a partridge in a pear tree and J represents two turtle doves \n and K.

In this way, each verse of the song is composed of:

  • ^ or :
  • A string representing the correct ordinal (say, el*th)
  • %
  • A letter A through K that represents the correct lyrics.

However, because most of the ordinals end in th, we use the substitution th%+ to save some bytes.


0

There are times when the most obvious solution is also the shortest, i.e. I could no longer resist this urge.

Bash on Mac OS X, 26

open http://tiny.cc/kavxf

Perl, 111

use LWP::Simple;get("http://tiny.cc/n230t")=~/On.*tree!/;
$_=$&;s/<br>/\n/g;s/(<.+?>)|(&\w+;)/ /g;print "$_\n";

One newline added for readability.


3
If this is a valid solution, then I have an even shorter one written in HQ9+C. (That's HQ9+ with one extra command. You can guess what it does.)
Ilmari Karonen

1
I considered obfuscating the perl one using the eval compress trick to claim I found a regex that compresses really well, but that bloated up around 200 characters. lol
Jeff Burdges

The video doesn't work... also, use x.co, it's the shortest url shortener I'm aware of

0

Java - 1329 chars

class x{public static void main(String[] args){for(int i=1;i<=12;i++){System.out.print("On the ");switch(i){case 1:System.out.print("first");break;case 2:System.out.print("second");break;case 3:System.out.print("third");break;case 4:System.out.print("fourth");break;case 5:System.out.print("fifth");break;case 6:System.out.print("sixth");break;case 7:System.out.print("seventh");break;case 8:System.out.print("eighth");break;case 9:System.out.print("ninth");break;case 10:System.out.print("tenth");break;case 11:System.out.print("eleventh");break;case 12:System.out.print("twelfth");break;}System.out.println(" day of Christmas\nmy true love gave to me");switch(i){case 12:System.out.println("Twelve drummers drumming");case 11:System.out.println("Eleven pipers piping");case 10:System.out.println("Ten lords-a-leaping");case 9:System.out.println("Nine ladies dancing");case 8:System.out.println("Eight maids-a-milking");case 7:System.out.println("Seven swans-a-swimming");case 6:System.out.println("Six geese-a-laying");case 5:System.out.println("Five golden rings");case 4:System.out.println("Four calling birds");case 3:System.out.println("Three french hens");default:if(i>=2)System.out.print("Two turtle doves\nAnd a");else System.out.print("A");System.out.println(" partridge in a pear tree");break;}System.out.println();}}}

I'm too lazy to ungolf it, but it is here: http://ideone.com/MU9IcP.


-2

SIMPLE, 1 byte

a

Note :

The language was designed after the challenge and is still a WIP.

How :

Any char will output the 12 days of Christmas.

Sitemizi kullandığınızda şunları okuyup anladığınızı kabul etmiş olursunuz: Çerez Politikası ve Gizlilik Politikası.
Licensed under cc by-sa 3.0 with attribution required.