97
\$\begingroup\$

Write a program that outputs the lyrics to 99 Bottles of Beer, in as few bytes as possible.

Lyrics:

99 bottles of beer on the wall, 99 bottles of beer.
Take one down and pass it around, 98 bottles of beer on the wall.

98 bottles of beer on the wall, 98 bottles of beer.
Take one down and pass it around, 97 bottles of beer on the wall.

97 bottles of beer on the wall, 97 bottles of beer.
Take one down and pass it around, 96 bottles of beer on the wall.

96 bottles of beer on the wall, 96 bottles of beer.
Take one down and pass it around, 95 bottles of beer on the wall.

95 bottles of beer on the wall, 95 bottles of beer.
Take one down and pass it around, 94 bottles of beer on the wall.

....

3 bottles of beer on the wall, 3 bottles of beer.
Take one down and pass it around, 2 bottles of beer on the wall.

2 bottles of beer on the wall, 2 bottles of beer.
Take one down and pass it around, 1 bottle of beer on the wall.

1 bottle of beer on the wall, 1 bottle of beer.
Go to the store and buy some more, 99 bottles of beer on the wall.

Rules:

  • Your program must log to STDOUT or an acceptable alternative, or be returned from a function (with or without a trailing newline).
  • Your program must be a full, runnable program or function.
  • Languages specifically written to submit a 0-byte answer to this challenge are allowed, just not particularly interesting.

    Note that there must be an interpreter so the submission can be tested. It is allowed (and even encouraged) to write this interpreter yourself for a previously unimplemented language.

  • This is different from the output by HQ9+ or 99. Any answers written in these languages will be deleted.

As this is a catalog challenge, this is not about finding the language with the shortest solution for this (there are some where the empty program does the trick) - this is about finding the shortest solution in every language. Therefore, no answer will be marked as accepted.

Catalogue

The Stack Snippet at the bottom of this post generates the catalogue from the answers a) as a list of shortest solution per language and b) as an overall leaderboard.

To make sure that your answer shows up, please start your answer with a headline, using the following Markdown template:

## Language Name, N bytes

where N is the size of your submission. If you improve your score, you can keep old scores in the headline, by striking them through. For instance:

## Ruby, <s>104</s> <s>101</s> 96 bytes

If there you want to include multiple numbers in your header (e.g. because your score is the sum of two files or you want to list interpreter flag penalties separately), make sure that the actual score is the last number in the header:

## Perl, 43 + 2 (-p flag) = 45 bytes

You can also make the language name a link which will then show up in the snippet:

## [><>](http://esolangs.org/wiki/Fish), 121 bytes

<style>body { text-align: left !important} #answer-list { padding: 10px; width: 290px; float: left; } #language-list { padding: 10px; width: 290px; float: left; } table thead { font-weight: bold; } table td { padding: 5px; }</style><script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <link rel="stylesheet" type="text/css" href="//cdn.sstatic.net/codegolf/all.css?v=83c949450c8b"> <div id="language-list"> <h2>Shortest Solution by Language</h2> <table class="language-list"> <thead> <tr><td>Language</td><td>User</td><td>Score</td></tr> </thead> <tbody id="languages"> </tbody> </table> </div> <div id="answer-list"> <h2>Leaderboard</h2> <table class="answer-list"> <thead> <tr><td></td><td>Author</td><td>Language</td><td>Size</td></tr> </thead> <tbody id="answers"> </tbody> </table> </div> <table style="display: none"> <tbody id="answer-template"> <tr><td>{{PLACE}}</td><td>{{NAME}}</td><td>{{LANGUAGE}}</td><td>{{SIZE}}</td><td><a href="{{LINK}}">Link</a></td></tr> </tbody> </table> <table style="display: none"> <tbody id="language-template"> <tr><td>{{LANGUAGE}}</td><td>{{NAME}}</td><td>{{SIZE}}</td><td><a href="{{LINK}}">Link</a></td></tr> </tbody> </table><script>var QUESTION_ID = 64198; var ANSWER_FILTER = "!t)IWYnsLAZle2tQ3KqrVveCRJfxcRLe"; var COMMENT_FILTER = "!)Q2B_A2kjfAiU78X(md6BoYk"; var OVERRIDE_USER = 36670; var answers = [], answers_hash, answer_ids, answer_page = 1, more_answers = true, comment_page; function answersUrl(index) { return "//api.stackexchange.com/2.2/questions/" + QUESTION_ID + "/answers?page=" + index + "&pagesize=100&order=desc&sort=creation&site=codegolf&filter=" + ANSWER_FILTER; } function commentUrl(index, answers) { return "//api.stackexchange.com/2.2/answers/" + answers.join(';') + "/comments?page=" + index + "&pagesize=100&order=desc&sort=creation&site=codegolf&filter=" + COMMENT_FILTER; } function getAnswers() { jQuery.ajax({ url: answersUrl(answer_page++), method: "get", dataType: "jsonp", crossDomain: true, success: function (data) { answers.push.apply(answers, data.items); answers_hash = []; answer_ids = []; data.items.forEach(function(a) { a.comments = []; var id = +a.share_link.match(/\d+/); answer_ids.push(id); answers_hash[id] = a; }); if (!data.has_more) more_answers = false; comment_page = 1; getComments(); } }); } function getComments() { jQuery.ajax({ url: commentUrl(comment_page++, answer_ids), method: "get", dataType: "jsonp", crossDomain: true, success: function (data) { data.items.forEach(function(c) { if (c.owner.user_id === OVERRIDE_USER) answers_hash[c.post_id].comments.push(c); }); if (data.has_more) getComments(); else if (more_answers) getAnswers(); else process(); } }); } getAnswers(); var SCORE_REG = /<h\d>\s*([^\n,<]*(?:<(?:[^\n>]*>[^\n<]*<\/[^\n>]*>)[^\n,<]*)*),.*?(\d+)(?=[^\n\d<>]*(?:<(?:s>[^\n<>]*<\/s>|[^\n<>]+>)[^\n\d<>]*)*<\/h\d>)/; var OVERRIDE_REG = /^Override\s*header:\s*/i; function getAuthorName(a) { return a.owner.display_name; } function process() { var valid = []; answers.forEach(function(a) { var body = a.body; a.comments.forEach(function(c) { if(OVERRIDE_REG.test(c.body)) body = '<h1>' + c.body.replace(OVERRIDE_REG, '') + '</h1>'; }); var match = body.match(SCORE_REG); if (match) valid.push({ user: getAuthorName(a), size: +match[2], language: match[1], link: a.share_link, }); else console.log(body); }); valid.sort(function (a, b) { var aB = a.size, bB = b.size; return aB - bB }); var languages = {}; var place = 1; var lastSize = null; var lastPlace = 1; valid.forEach(function (a) { if (a.size != lastSize) lastPlace = place; lastSize = a.size; ++place; var answer = jQuery("#answer-template").html(); answer = answer.replace("{{PLACE}}", lastPlace + ".") .replace("{{NAME}}", a.user) .replace("{{LANGUAGE}}", a.language) .replace("{{SIZE}}", a.size) .replace("{{LINK}}", a.link); answer = jQuery(answer); jQuery("#answers").append(answer); var lang = a.language; lang = jQuery('<a>'+lang+'</a>').text(); languages[lang] = languages[lang] || {lang: a.language, lang_raw: lang.toLowerCase(42), user: a.user, size: a.size, link: a.link}; }); var langs = []; for (var lang in languages) if (languages.hasOwnProperty(lang)) langs.push(languages[lang]); langs.sort(function (a, b) { if (a.lang_raw > b.lang_raw) return 1; if (a.lang_raw < b.lang_raw) return -1; return 0; }); for (var i = 0; i < langs.length; ++i) { var language = jQuery("#language-template").html(); var lang = langs[i]; language = language.replace("{{LANGUAGE}}", lang.lang) .replace("{{NAME}}", lang.user) .replace("{{SIZE}}", lang.size) .replace("{{LINK}}", lang.link); language = jQuery(language); jQuery("#languages").append(language); } }</script>

\$\endgroup\$
25
  • 17
    \$\begingroup\$ Whoever closevoted this, the other one is a popularity contest... \$\endgroup\$
    – TheDoctor
    Commented Nov 18, 2015 at 15:19
  • 26
    \$\begingroup\$ This is a subtask of the Create an HQ9+ Interpreter golf. \$\endgroup\$
    – Geobits
    Commented Nov 18, 2015 at 15:25
  • 15
    \$\begingroup\$ Note that quartata had already sandboxed a 99BB catalog several weeks ago and was planning to post it. \$\endgroup\$
    – Alex A.
    Commented Nov 18, 2015 at 17:07
  • 16
    \$\begingroup\$ @GamrCorps Please do not unnecessarily change the rules after the challenge has started. None of the characters in the output should be optional. \$\endgroup\$
    – feersum
    Commented Nov 18, 2015 at 18:42
  • 8
    \$\begingroup\$ To try and head off any newcomers who can't see deleted answers, if you're thinking of using a language with a 99 Bottles song built-in, double check the output matches the desired output of the question. There have already been half a dozen deleted HQ9+ answers, as well as other languages with incorrect outputs. \$\endgroup\$
    – Jo King
    Commented Oct 22, 2018 at 4:05

181 Answers 181

1 2 3
4
5
7
1
\$\begingroup\$

BitShift, 342372 bytes

Pre-note: This is probably sub-optimal...

BitShift is a language created by me, which only supports bit-shifting operations on a single value. It is not great for :')

Since the program is too long to include in this post, it is provided here.
It can be tested here. Be aware that it will take a while to finish.

I am hoping for this challenge to provide a better solution to this problem.

\$\endgroup\$
1
  • 1
    \$\begingroup\$ if you click "make readable" it gets mad because newlines aren't 1 or 0 haha \$\endgroup\$ Commented Nov 22, 2015 at 4:50
1
\$\begingroup\$

PHP, 256 bytes

<?php $b=' bottle';$o=' of beer';$t=' on the wall';$i=99;while($i>=1){$s=$i!=1?'s':'';echo "$i$b$s$o$t, $i$b$s$o.\n".($s?"Take one down and pass it around":"Go to the store and buy some more").", ".($i==1&&--$i>-3?99:--$i)."$b".($i!=1?'s':'')."$o$t.\n\n";}
\$\endgroup\$
1
\$\begingroup\$

R, 233 Bytes

f=function(n){b="bottles of beer";p=paste0;w="on the wall";cat(n,b,p(w,","),n,p(b,"."),"\n");if(n>1){cat("Take one down and pass it around,",n-1,b,p(w,".\n"));f(n-1)}else cat("Go to the store and buy some more, 99",b,p(w,"."))}
f(99)

Ideone as a proof!

longer (non-golf) version

Same functions, same syntax; added only breaks and brackets

f <- function(n) { 
  b <- "bottles of beer"
  p <- paste0
  w <- "on the wall"
  cat(n, b, p(w, ","), n, p(b, "."), "\n")
  
  if (n > 1) {
    cat("Take one down and pass it around,", n - 1, b, p(w, ".\n"))
    f(n - 1)
  } else {
    cat("Go to the store and buy some more, 99", b, p(w, "."))
  }
}

f(99)
\$\endgroup\$
1
  • 4
    \$\begingroup\$ It writes 1 bottles of beer during the last two rounds. \$\endgroup\$
    – plannapus
    Commented Feb 24, 2016 at 8:30
1
\$\begingroup\$

golflua, 206 bytes

\b(t)~i.." bottle"..(i>1&"s"|"").." of beer"..t$\c(t)~b" on the wall"..t$i=99@i>0w(c", "..b".")?i<2 ~~$i=i-1w("Take one down and pass it around, "..c".\n")$i=99w("Go to the store and buy some more, "..c".")

Sample run:

bash-4.3$ golflua 99.golflua | tail
Take one down and pass it around, 3 bottles of beer on the wall.

3 bottles of beer on the wall, 3 bottles of beer.
Take one down and pass it around, 2 bottles of beer on the wall.

2 bottles of beer on the wall, 2 bottles of beer.
Take one down and pass it around, 1 bottle of beer on the wall.

1 bottle of beer on the wall, 1 bottle of beer.
Go to the store and buy some more, 99 bottles of beer on the wall.
\$\endgroup\$
1
\$\begingroup\$

O 1.2, 169 bytes

{.o" bottle"o.1>{'so}{}?" of beer"o}:b{" on the wall"o}:w9B*..{bw", "o b "."p(.{"Take one down and pass it around, "}{;"Go to the store and buy some more, "}?obw'.p""p}d

(Works with the old Java interpreter. No idea whether is matter of version of interpreter, but the online O Language IDE throws error.)

Sample run:

bash-4.3$ java xyz.jadonfowler.o.O 99.o | tail

3 bottles of beer on the wall, 3 bottles of beer.
Take one down and pass it around, 2 bottles of beer on the wall.

2 bottles of beer on the wall, 2 bottles of beer.
Take one down and pass it around, 1 bottle of beer on the wall.

1 bottle of beer on the wall, 1 bottle of beer.
Go to the store and buy some more, 99 bottles of beer on the wall.
\$\endgroup\$
1
\$\begingroup\$

Python3, 258 237 232 bytes

Being strict about spaces and newlines.

i=98;b="bottle";w="on the wall";o="of beer"
while i+1:y=bool(i);s="s"*y;print(i+1,b+s,o,w+",",i+1,b+s,o+".\n"+("Go to the store and buy some more,","Take one down and pass it around,")[y],i or 99,b+"s"*bool(i-1),o,w+"."+"\n"*y);i-=1

Readable (sort of):

i=98
b="bottle"
w="on the wall"
o="of beer"
while i + 1:
    y = bool(i)
    s = "s" * y
    print(
        i + 1,
        b + s,
        o,
        w + ",",
        i + 1,
        b + s,
        o + ".\n" 
        + (
            "Go to the store and buy some more,",
            "Take one down and pass it around,"
        )[y],
        i or 99,
        b + "s" * bool(i - 1),
        o,
        w + "." 
        + "\n" * y
    )
    i -= 1

Default unspecified arguments for print(): sep=' ' and end='\n'.

\$\endgroup\$
0
1
\$\begingroup\$

Bubblegum, 474 bytes

00000000: e02de601d25d001ce07cc46f3a26b205c864c02a139d  .-...]...|.o:&...d.*..
00000016: 166e795c307826823b392997b956fbc8b6c350ce248e  .ny\0x&.;9)..V....P.$.
0000002c: 1071a7cb6e24ab42a254bbe0a8be43ad25d657dd967a  .q..n$.B.T....C.%.W..z
00000042: 258b807b20b4ceba00a9918ae3e3eed4e23bb85856ef  %..{ ............;.XV.
00000058: e1267f9a415e5ca27213fcf0adf089c56057787ec1fe  .&..A^\.r.......`Wx~..
0000006e: 3620203a8af3c18b3cc36c43f04307eed88938663114  6  :....<.lC.C....8f1.
00000084: 4de9bb22d200d500dfaae3b7c6eda8da60c7ad0e3496  M.."............`...4.
0000009a: d5df645de0ba1a202c6094f9103fc864a170d4c001d3  ..d]... ,`...?.d.p....
000000b0: d862374c5d4918d17d84d30c1234bb7861a1b3545599  .b7L]I..}....4.xa..TU.
000000c6: 4daaab8a872177daef7ee025d5ec84c4ba56f0840d8f  M....!w..~.%.....V....
000000dc: c49023d4506350828b8bfa6287c90b9cdac45b11283f  ..#.PcP....b......[.(?
000000f2: 9dd5926301a37d1817468471f0e5f5c38999e5cd9d9b  ...c..}..F.q..........
00000108: 3429c1755fd57139c9c66126f899217c3b09395dd205  4).u_.q9..a&..!|;.9]..
0000011e: 6a0332c73adbe8015922be7407dd21f813b1d90a389b  j.2.:...Y".t..!.....8.
00000134: d0bc6aac22cc65bdb8dbdd03d863654f06538750c04b  ..j.".e......ceO.S.P.K
0000014a: e437a1e0f19dbb0d5d598f3122a31f400245f39c156a  .7......]Y.1"[email protected]
00000160: 06082aaa40ce6f0c822f585b6d1053a8144cf5552831  ..*[email protected]../X[m.S..L.U(1
00000176: 3885ffc5d3aa540bc74534253b94406174b4b4e0337d  8.....T..E4%;[email protected]}
0000018c: 607f28b1614429ee7238ecba8f8682446140cbc507f9  `.(.aD).r8.....Da@....
000001a2: 54e2cbed802442331930e963822df3527911b0f42895  T....$B3.0.c.-.Ry...(.
000001b8: e0a1a806829eca36fe4bb623dbc6b87c45c26843f770  .......6.K.#...|E.hC.p
000001ce: ede402da278009d9a19a0000                      ....'.......

The above hexdump can be reversed with xxd -r -c 22.

\$\endgroup\$
1
\$\begingroup\$

CJam, 252 227 220 bytes

I can guarantee that this is not the shortest solution. Oh well, I tried.

" bottles of beer on the wall.

":D;99,W%:){_"%d bottles of"D28/0=+", %d bottles of beer."+e%W$e%{'s=1$1=&!},\_(:M{"
Take one down, pass it around, %d"\(e%D{'s=M1=&!},}{"
Go to the store and buy some more, 99"D+}?+1-+o}%

Try it out here. Any golfing tips are greatly appreciated.

Thanks to @Dennis for helping me shorten this!

\$\endgroup\$
1
\$\begingroup\$

JavaScript ES5, 233 bytes

for(i=100,a=' bottle',b=' of beer',c=' on the wall';i>1;i--,alert(i+a+(i!=1?'s':'')+b+c+', '+i+a+(i!=1?'s':'')+b+'.'+(i>1?'Take one down and pass it around, '+(i-1):'Go to the store and buy some more, '+99)+a+(i==2?'':'s')+b+c+'.'));
\$\endgroup\$
1
\$\begingroup\$

Mouse-2002, 275 bytes

Mouse hates strings. Actually, it doesn't even know anything about them, because the closest thing to a string you can put on the stack is a char. Anything between " " is printed, but you can't put strings in variables or manipulate them.1

99I:(I.0>^I.1=["1 bottle of beer on the wall, 1 bottle of beer.!Go to the store and buy some more, 99 bottles of beer on the wall.!"|I.!" bottles of beer on the wall, "I.!" bottles of beer.!Take one down and pass it around, "I.1-!" bottles of beer on the wall.!!"]I.1-I:)$

That's shamefully long, for a stack-based language.

Explained:

99I:     ~ i = 99
(        ~ while(true)
  I.0>^  ~ if i > 0 == false then break
  I.1=[  ~ if i == 1 print this string
    "1 bottle of beer on the wall, 1 bottle of beer.!Go to the store and buy some more, 99 bottles of beer on the wall.!"
  | I.!" bottles of beer on the wall, "I.!" bottles of beer.!Take one down and pass it around, "I.1-!" bottles of beer on the wall.!!" ~ else print this interpolated one
  ] ~ end if
  I.1-I: ~ i = i-1
) ~ end while
$ ~ end prog

1That's why I'm planning to reimplement and update Mouse into newmouse!

\$\endgroup\$
1
\$\begingroup\$

Perl 5, 222 bytes

Ungolfed:

$b=" bottles of beer";
$B=" bottle of beer";
$w=" on the wall";
for($i=99;$i>1;$i--){
print"$i$b$w, $i$b.\nTake one down and pass it around, ".($i-1)."$b$w.\n\n";
}
print"1$B$w, 1$B.\nGo to the store and buy some more, 99$b$w.";

Golfed:

$b=" bottles of beer";$B=" bottle of beer";$w=" on the wall";for($i=99;$i>1;$i--){print"$i$b$w, $i$b.\nTake one down and pass it around, ".($i-1)."$b$w.\n\n";}print"1$B$w, 1$B.\nGo to the store and buy some more, 99$b$w.";

I'm convinced there has to be a way to improve this approach with some sort of whitespace collapsing. Is there a special variable in Perl that is printed after alphanumeric characters, that you can set? I wonder...

Either way, I feel pretty content for a 5-minute golf.

\$\endgroup\$
1
\$\begingroup\$

Milky Way 1.0.2, 255 bytes

99&{~:?{1b_" bottle"_" bottles"}" of beer"+;:>;+:" on the wall, "+;".\n"++"Take one down and pass it around, "<1-:>++<?{1b_" bottle"_" bottles"};>+" of beer on the wall."+<?{_""_"\rGo to the store and buy some more, 99 bottles of beer on the wall."};>+!<}

It's really too long to explain in detail here, but check the docs for info.


Usage

./mw <path-to-code>
\$\endgroup\$
1
\$\begingroup\$

Emacs Lisp, 290 Bytes

(defun b (n) (format "%d bottle%s of beer" n (if (eq n 1) "" "s")))
(defun bw (n) (concat (b n) " on the wall"))
(defun l (n) (concat (bw n) ", " (b n) ".\nTake one down, pass it around, " (bw (- n 1)) ".\n" (if (> n 1) (l (- n 1)) "Go to the store and buy some more.\n")))
(message (l 99))
\$\endgroup\$
3
  • 1
    \$\begingroup\$ You forgot to “go to the store and buy some more”. \$\endgroup\$
    – manatwork
    Commented Nov 23, 2015 at 12:19
  • \$\begingroup\$ The first line would be shorter as (defun b (n) (format "%d bottle%s of beer" n (if (> n 1) "s" ""))) \$\endgroup\$
    – manatwork
    Commented Nov 23, 2015 at 12:31
  • \$\begingroup\$ @manatwork Thanks for the help. Updated it. \$\endgroup\$
    – Lord Yuuma
    Commented Nov 23, 2015 at 19:20
1
\$\begingroup\$

Lua, 267 263 bytes

q=" bottle"r=q.."s"s=" of beer"t=" on the wall"u="\nTake one down and pass it around, "v="\nGo to the store and buy some more, "w=r..s for n=99,1,-1 do io.write(n,w,t,", ",n,w,".",n>1 and u..n-1 ..(n>2 and w or q..s)or v.."99"..r..s,t,".\n\n")w=(n>2 and w or q..s)end

I didn't see lua under the answers yet, only golflua, correct me if im wrong.

Explatation:

q=" bottle"     -- assigning parts of the text to variables in as
r=q.."s"        -- long pices as it's practical
s=" of beer"
t=" on the wall"
u="\nTake one down and pass it around, "
v="\nGo to the store and buy some more, "
w=r..s          -- bracause r..s/r,s repeats 4 times its shorter to assign it to w

for n=99,1,-1 do
    io.write(n,w,t,", ",n,w,".",    --putting it together

        n>1 and u..n-1              -- "take one down..." or "go to the store..."
            ..(n>2 and w or q..s)   -- "bottels or bottle..."
        or v.."99"..r..s,t,".\n\n"  -- "n-1" or "99"
    )

    w=(n>2 and w or q..s)           -- this condition repeats 2 times, it changes "bottles" to "bottle"
end

Tryout link: Execute Lua Online.

Type lua in the comandline and then dofile("99bob.lua").

Testrun:

3 bottles of beer on the wall, 3 bottles of beer.                                                                                                                   
Take one down and pass it around, 2 bottles of beer on the wall.                                                                                                    

2 bottles of beer on the wall, 2 bottles of beer.                                                                                                                   
Take one down and pass it around, 1 bottle of beer on the wall.                                                                                                     

1 bottle of beer on the wall, 1 bottle of beer.                                                                                                                     
Go to the store and buy some more, 99 bottles of beer on the wall.

I tried to write the program golfed from the start, that probably wasn't a good idia.

Edit: thanks to manatwork for r=q.."s".

\$\endgroup\$
1
  • \$\begingroup\$ Even if a Lua solution would already exist, you are welcome to add another one if the algorithm is significantly different. r=" bottles"r=q.."s". \$\endgroup\$
    – manatwork
    Commented Dec 23, 2015 at 15:43
1
\$\begingroup\$

JavaScript ES6, 298 bytes

for(var a=[" bottles of beer","on the wall","take one down and pass it around","go to the store and buy some more",99];0<a[4];)a[0]=1===a[4]?a[0].replace("s",""):a[0],console.log(a[4]+a[0]+" "+a[1]+", "+a[4]+a[0]+". "+(1===a[4]?a[3]:a[2])+", "+(0===a[4]-1?"99"+a[0]+"s":a[4]+a[0])+" "+a[1]),a[4]--;
\$\endgroup\$
1
\$\begingroup\$

C, 256 bytes

Nice round number.

*a=" bottles of beer",*b=" on the wall",*c="Take one down and pass it around, ",d=100;main(){while(--d){printf("%d%s%s, %d%s, \n",d,a,b,d,a);d-1&&printf("%s%d%s%s. \n",c,d,a,b);}puts("Go to the store and buy some more, 99 bottles of beer on the wall.");}

With whitespace:

*a=" bottles of beer",
*b=" on the wall",
*c="Take one down and pass it around, ",
d=100;main(){
    while(--d){
        printf("%d%s%s, %d%s, \n",d,a,b,d,a);
        d-1&&printf("%s%d%s%s. \n",c,d,a,b);
    }
    puts("Go to the store and buy some more, 99 bottles of beer on the wall.");
}

Fun fact: I accidentally posted this on stack exchange and then realised a few minutes later that it was on the wrong post. Ooops.

\$\endgroup\$
1
\$\begingroup\$

SELECT., 7313057 bytes

This is not a SELECT. program that prints the 99 bottles lyrics. It is a link to a pastebin of a python program that prints a SELECT. program that prints the 99 bottles lyrics. The latter program is over 7 megabytes long, and unsuitable for posting here.

http://pastebin.ca/3372405

The program that generated this program looks like this:

######GENERATION CODE######
init(185,49,1)
makenum(10)
square()
dec()
dec()
var("99")
loop("main",computei=True,savelist=["99"])
var("i")
go(1)
makeneg1()
multiply(-1)
go(1)
fetch("99")
add(-1)
var("num")
go(1)
makeone()
add(-1)
var("curnum")
go(1)
makenum(10)
go(-1)
intdiv()
go(-5)
var("mod")
go(5)
var("div")
ifnonpositive("tens")
els("tens",True)
digitprintXY(0,0)
endif("tens")
go(1)
fetch("mod")
var("mod")
digitprintXY(6,0)
go(1)
drawstringliteral("bottle",16,0)
go(1)
fetch("curnum")
go(1)
makeneg1()
add(-1)
var("onetest")
ifzero("plural")
go(1)
drawstringliteral("of beer on the wall,",51,0)
els("plural")
go(1)
drawstringliteral("s of beer on the wall,",46,0)
endif("plural")
go(1)
fetch("div")
ifnonpositive("tens")
els("tens",True)
digitprintXY(0,13)
endif("tens")
go(1)
fetch("mod")
digitprintXY(6,13)
go(1)
drawstringliteral("bottle",16,13)
go(1)
fetch("onetest")
ifzero("plural")
go(1)
drawstringliteral("of beer.",51,13)
els("plural")
go(1)
drawstringliteral("s of beer.",46,13)
endif("plural")
go(1)
drawstringliteral("Take one down and pass it around,",0,26)
go(1)
fetch("num")
ifzero("nomore")
go(1)
drawstringliteral("No more bottles ofbeer on the wall.",0,39)
els("nomore",True)
go(1)
makenum(10)
go(-1)
intdiv()
go(-5)
var("mod")
go(5)
ifnonpositive("tens")
els("tens",True)
digitprintXY(0,39)
endif("tens")
go(1)
fetch("mod")
digitprintXY(6,39)
go(1)
drawstringliteral("bottle",16,39)
go(1)
fetch("num")
go(1)
makeneg1()
add(-1)
ifzero("plural")
go(1)
drawstringliteral("of beer on the wall.",51,39)
els("plural")
go(1)
drawstringliteral("s of beer on the wall.",46,39)
endif("plural")
input()
clear()
endif("nomore")
go(1)
endloop("main",savelist=["99"])
go(1)
drawstringliteral("No more bottles of beer on the wall,",0,0)
go(1)
drawstringliteral("no morebottles of beer.",0,13)
go(1)
drawstringliteral("Go to thestore and buy some more,",0,26)
go(1)
drawstringliteral("99 bottles of beeron the wall.",0,39)
writetofile("99bottles.sel")
\$\endgroup\$
1
\$\begingroup\$

Groovy, 213 bytes

Quite simple, probably much more golfing could be done.

b={" $it bottles of beer.\n"}
print ((99..1).collect{"$it bottle${it>1?'s':''} of beer on the wall,"+b(it)+(it>1?"Take one down and pass it around,"+b(it-1):"Go to the store and buy some more,"+b(99))}.join('\n'))
\$\endgroup\$
1
\$\begingroup\$

Jolf, 193 bytes

Try it here! I can still probably golf a byte or two...

Ώ³γhHΖp{" Ξ\x000k"γΆΆγ"% of beer on the wall, "ζγ"% of ΞΓi
"ζ?wγΆ"Ξ    ψone Ξp«Ξit ΞpDΖ"wγ"% of ΞΙon the Ξy
"p{" Ξ\x000k"wγ"Go to the Ξrand buy ΞzδΞB\x00Ε 99 Ξώof ΞΙon the Ξy"1Μw_z~1dΏH

Here's the main concept: this defines a function Ώ that returns the bottle line of an input, calls it 1 for a default, and:

Μw_z~1dΏH
Μ           maps
 w_z~1      0..99 reversed
      dΏH   with that function

Ugh that hurt my head.

\$\endgroup\$
1
\$\begingroup\$

XSLT 1.0 (no EXSLT), 845 bytes

<transform xmlns="http://www.w3.org/1999/XSL/Transform" version="1.0"><output method="text"/><template match="/"><call-template name="b"/></template><template name="b"><param name="n" select="99"/><if test="$n>0"><variable name="o"><if test="$n>1"><value-of select="$n -1"/></if><if test="$n=1">99</if></variable><variable name="p"><if test="$n>1">s</if></variable><variable name="q"><if test="$n!=2">s</if></variable><variable name="z"><if test="$n=1">Go to the store and buy some more</if><if test="$n>1">Take one down and pass it around</if></variable><variable name="w" select="concat($n,' bottle',$p,' of beer')"/><value-of select="concat($w,' on the wall, ',$w,'.&#10;',$z,', ',number($o),' bottle',$q,' of beer on the wall.&#10;')"/><call-template name="b"><with-param name="n" select="$n -1"/></call-template></if></template></transform>

With a bit less golf in it:

<transform xmlns="http://www.w3.org/1999/XSL/Transform" version="1.0">
    <output method="text"/>
    <template match="/">
        <call-template name="b"/>
    </template>
    <template name="b">
        <param name="n" select="99"/>
        <if test="$n>0">
            <variable name="o">
                <if test="$n>1">
                    <value-of select="$n -1"/>
                </if>
                <if test="$n=1">99</if>
            </variable>
            <variable name="p">
                <if test="$n>1">s</if>
            </variable>
            <variable name="q">
                <if test="$n!=2">s</if>
            </variable>
            <variable name="z">
                <if test="$n=1">Go to the store and buy some more</if>
                <if test="$n>1">Take one down and pass it around</if>
            </variable>
            <variable name="w" select="concat($n,' bottle',$p,' of beer')"/>
            <value-of select="concat($w,' on the wall, ',$w,'.&#10;',$z,', ',number($o),' bottle',$q,' of beer on the wall.&#10;')"/>
            <call-template name="b">
                <with-param name="n" select="$n -1"/>
            </call-template>
        </if>
    </template>
</transform>

Notes

Text compression

The use of variables as abbreviations isn't as extensive as in other languages; for example, note the repetition of ' bottle' as a constant. In XSLT, the binding of an XPath expression or some arbitrary text to a variable name requires at least 29 characters of overhead, excluding the name and content itself:

<variable name="name" select="expr"/>
<variable name="name">text</variable>

Say, for example, that one would like to replace all instances of the XPath string literal ' bottle' (8 chars) with $b (2 chars). Each substitution saves 6 characters. However, given the definition <variable name="b"> bottle</variable> (38 chars), there is no net profit unless there are 7 or more such sequences to replace.

The use of the <variable/> element is limited to evaluating conditions. The version of XPath used with XSLT 1.0 does not have its own if/else constructs, but XSLT itself does, and <variable/> is necessary to retrieve the results in a useful form.

$n -1

The expression $n -1 appears twice. The space after the $n is required since - is a valid name character in XPath.

Inequalities

Wherever practical, any instance where != originally appeared was changed to use > instead. If the program counted down to 0 instead of resetting to 99 after 1, the plural check for $n would be incorrect at 0.

Tail recursion

The template named b recurses, counting the variable n down by one each time. XSLT provides looping constructs, but they are for traversing structures found in the input document. A task like this one would be substantially easier if we had an input doc containing the countdown (<doc><value>99</value><value>98</value><value>97</value>...</doc>) but at that point we're cheating (or we at least have to count the input doc in our byte count).

Run

Run using xsltproc:

xsltproc 99.xsl 99.xsl

Yes, 99.xsl is passed twice: Once as the XSLT document and then as the XML document to be transformed. An input document must be present because an XSLT processor generally requires one to begin running. (XSLT is designed to define a transformation from an input document to an output document; running a transform solely with command-line parameters as I've done here is atypical.) For this program, any well-formed XML document will suffice as input, and, XSLT being an application of XML, any well-formed XSLT transform is by definition a well-formed XML document.

\$\endgroup\$
1
  • 1
    \$\begingroup\$ Bad memories. At least it's good from something. \$\endgroup\$
    – primo
    Commented Jul 4, 2016 at 18:04
1
\$\begingroup\$

C#, 267 bytes

()=>{for(int x=99;x>0;){string e="",s="s",t=x>1?s:e,b=" bottle",o=" of beer",w=$"{o} on the wall",a=x+b+t;System.Console.Write($"{a+w}, {a+o}.\n"+(x-->1?$"Take one down and pass it around, {x+b+(x>1?s:e)+w}.\n\n":$"Go to the store and buy some more, 99{b}s{w}."));}};

C# lambda where the output is stdout and without output. You can try it on .NetFiddle.

Code :

()=>{
    for (int x=99;x>0;) {
        string e="",s="s",t=x>1?s:e,b=" bottle",o=" of beer",w=$"{o} on the wall",a=x+b+t;
        System.Console.Write($"{a+w}, {a+o}.\n"+(x-->1
            ?$"Take one down and pass it around, {x+b+(x>1?s:e)+w}.\n\n"
            :$"Go to the store and buy some more, 99{b}s{w}."));
}};

Kudos to Igby Largeman for his solution. I think it is better than mine (shorter if it had use a lambda too).

\$\endgroup\$
1
\$\begingroup\$

Common Lisp (Lispworks), 431 bytes

(defun f()(do((i 99(- i 1)))((= i 2))(format t"~S bottles of beer on the wall, ~S bottles of beer.~%Take one down and pass it around, ~S bottles of beer on the wall.~%~%" i i(- i 1)))(format t"2 bottles of beer on the wall, 2 bottles of beer.~%Take one down and pass it around, 1 bottle of beer on the wall.~%~%1 bottle of beer on the wall, 1 bottle of beer.~%Go to the store and buy some more, 99 bottles of beer on the wall.~%"))

Ungolfed:

(defun 99-Bottles-of-Beer ()
  (do ((i 99 (- i 1))) ((= i 2))
    (format t "~S bottles of beer on the wall, ~S bottles of beer.~%Take one down and pass it around, ~S bottles of beer on the wall.~%~%" i i (- i 1)))
  (format t "2 bottles of beer on the wall, 2 bottles of beer.~%Take one down and pass it around, 1 bottle of beer on the wall.~%~%1 bottle of beer on the wall, 1 bottle of beer.~%Go to the store and buy some more, 99 bottles of beer on the wall.~%"))
\$\endgroup\$
1
\$\begingroup\$

Maple, 233 bytes

Added (unnecessary) newlines for code readability:

seq(printf("%d %s of beer on the wall,\n%d %s of beer.\nTake one down, pass it around,\n%d %s of beer on the wall.\n\n",
         i,`if`(i<>1,"bottles","bottle"),
         i,`if`(i<>1,"bottles","bottle"),
         i-1,`if`(i-1<>1,"bottles","bottle")),
i=99..1,-1);
\$\endgroup\$
1
\$\begingroup\$

R, 138 225 bytes

a='bottles of beer on the';for(i in 99:1)cat("",i,a,"wall,",i,"bottles of beer.\n","Take one down and pass it around,",i-1,a,"wall.\n\n")

a='bottles of beer on the'
for(i in 99:2)
    cat("",i,a,"wall,",i,"bottles of beer.\n","Take one down and pass it around,",i-1,a,"wall.\n\n")
cat(1,a,"wall,","1 bottle of beer.\nGo to the store and buy some more,",99,a,"wall.")

EDIT-- missed the change in the last verse.

\$\endgroup\$
1
  • 2
    \$\begingroup\$ There's still a little bit to fix: it prints 1 bottles (<- with s) in the end of the second last verse and at the beginning of the last verse. \$\endgroup\$
    – nimi
    Commented Aug 5, 2016 at 18:45
1
\$\begingroup\$

dc, 269 237 bytes

299si[lid1-dsi3/li3>vdn[ bottle]P1<n]sm[[s]P]sn[ of beer]so[ on the wall]sp[, ]sq46sr10ss[[Take one down and pass it around]Pq]st[d4<t[Go to the store and buy some more]P]su[99+]sv[lmxloPlpPlqPlmxloPlrPlsPluxlqPlmxloPlpPlrPlsdPP3<z]dszx

Shaved off about 30 bytes by replacing two counters w/ 1 299/3 counter; couple of unnecessary variable loads replaced w/ stack duplications; code point 46 instead of [.].

\$\endgroup\$
0
1
\$\begingroup\$

Windows Batch, 279 bytes

some optimization later

@echo off
set A=99
set B= bottle
set C=s of beer
set D= on the wall
:l
echo %A%%B%%C%%D%, %A%%B%%C%.
set/a A-=1
if %A%==1 set C=%C:~1%
if %A%==0 (echo Go to the store and buy some more, 99%B%s%C%%D%.
exit)else (echo Take one down and pass it around, %A%%B%%C%%D%.
echo.)
goto l

(my first post here, code actually from 2016)

\$\endgroup\$
2
  • \$\begingroup\$ Works, but you're not handling dropping the 's' when the bottles are singular \$\endgroup\$
    – Chirishman
    Commented Jun 14, 2017 at 22:32
  • \$\begingroup\$ @Chirishman if %A%==1 set C=%C:~1% drops the s when bottles are singular \$\endgroup\$
    – loadingnow
    Commented Jun 22, 2017 at 3:23
1
\$\begingroup\$

Ohm, 166 164 131 bytes (CP437)

Ohm has been updated a lot since I first posted this! Now I'm using some new features.

98@R≥î:_Θ▀a▀_Ωü▀5≡axÿx}iöܪ├┬ï°∙╟╕Ö√²┬zτα█▀_≤îΘ..ÑÑ;1Θ▀a▀1Ω▀â"190└δ*6²H"╥╨;√⌡ÄwîFδh#|a≡Ω█≡5▓Θ╕Ç≈└hÑqûI▀J
D≤y.sa·▀╖+╫Ä▀a
Θ▀Ö$k·}Qy╦▀

Try it online!

Explanation

The main wire contains a lot of compressed strings, which are delimited by .

Main wire:

98@R    Push the range 98...1.
≥î      Increment each.
:       Map each number n:
 _Θ      Call the second helper wire on n.
 ▀a▀     Push ", ".
 _Ω      Call the first helper wire on n.
 ü       Push a space.
 ▀...▀   Push "of beer.\nTake one down and pass it around, "
 _≤îΘ    Call the second helper wire on n-1.
 ..ÑÑ    Push a period and two newlines.
;       (end map)
1Θ      Call the second helper wire on 1.
▀a▀     Push ", "
1Ω      Call the first helper wire on 1.
▀...▀   Push " of beer.\nGo to the store and buy some more, 99 bottles of beer on the wall."
J       Join the entire stack together. Implicitly output.

First helper wire:

D       Duplicate TOS.
≤       Decrement.
y       Get sign (0 or 1).
.sa·    Repeat 's' that many times.
▀╖+╫Ä▀  Push " bottle"
a       Swap top two elements.

Second helper wire:

Θ           Call the first helper wire.
▀Ö$k·}Qy╦▀  Push " of beer on the wall"
\$\endgroup\$
1
\$\begingroup\$

tcl, 243

proc B {} {set x " bottle[expr $::i>1?{s}:{}] of beer"}
set i 99
time {puts "$i[B][set w " on the wall"], $i[B].
Take one down and pass it around, [incr i -1][B]$w."} 98
puts "1[B]$w, 1[B].
Go to the store and buy some more, [incr i 98][B]$w."

demo


tcl, 255

proc B i {set x " bottle[expr $i>1?{s}:{}] of beer"}
set w " on the wall"
set i 99
while \$i>1 {puts "$i[B $i]$w, $i[B $i].\nTake one down and pass it around, [incr i -1][B $i]$w."}
puts "1[B $i]$w, 1[B $i].\nGo to the store and buy some more, 99[B 9]$w."

demo

\$\endgroup\$
1
\$\begingroup\$

Ly, 272 bytes

(99)1[psu" bottles of beer on the wall, "&olu" bottles of beer.\n"&ol1-sp"Take one down and pass it around, "&olu" bottles of beer on the wall.\n"&ol1G]pu" bottle of beer on the wall, 1 bottle of beer.\nGo to the store and buy some more, 99 bottles of beer on the wall."&o

Try it online!

\$\endgroup\$
1
\$\begingroup\$

PowerShell (204 bytes)

$b='bottles of beer';$w="on the wall";$p="Take one down and pass it around";99..2|%{"$_ $b $w, $_ $b.`n$p, $($_-1) $w.`n"};$b=$b.remove(6,1);"1 $b $w, 1 $b.`nGo to the store and buy some more, 99 $b $w."

Edit (now 198 bytes as per @BradC comments, thx buddy!):

$b=' bottles of beer';$w=" on the wall";$p="Take one down and pass it around";99..2|%{"$_$b $w, $_$b.`n$p, $($_-1)$w.`n"};$b=$b.remove(6,1);"1$b$w, 1$b.`nGo to the store and buy some more, 99$b$w."
\$\endgroup\$
1
  • 1
    \$\begingroup\$ Nice. I think you can save a couple of bytes by including a space at the beginning of both @b and @w. \$\endgroup\$
    – BradC
    Commented Aug 9, 2017 at 21:10
1 2 3
4
5
7

Not the answer you're looking for? Browse other questions tagged or ask your own question.