99 Shortened Bottles of Beer
Disclaimer
This challenge also exists in CGCC, but if you want to compete here (too), then hop in!
Challenge
Recreate "99 Bottles of Beer on the Wall", using the least bytes 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
- 0-byte answers aren't allowed. Although 1-byte answers are allowed while still abiding to not using loopholes, they won't be very interesting (unless you DO make it interesting).
- Use any form of output in the program. Yes, the whole thing must be outputted up until you reach $n=1$.
- Built-in functions and/or code for this specific challenge is pretty much allowed, only if you explain (even a little) how the code works (or not if you can't at all).
- Shortest program of each language used is the winner.
[Java (JDK)], 311 301 296 byte …
3y ago
[Python 3], 202 bytes …
3y ago
FALSE, 198 bytes ``` [" on t …
3y ago
[C (gcc)], 244 bytes …
3y ago
[C (gcc)], 232 bytes …
3y ago
[Haskell], 221 215 bytes …
3y ago
[Scala], 242 bytes …
3y ago
[shortC], 223 bytes AOC …
3y ago
JavaScript (V8), 185 bytes …
3y ago
Japt, 117 116 115 bytes Nee …
3y ago
[JavaScript (Node.js)], 228 22 …
3y ago
Sclipting, (UTF-16) 238 bytes …
3y ago
[Seriously], 1 byte N …
3y ago
13 answers
Scala, 242 bytes
_=>99 to(1,-1)map{b=>s"${w(b)} on the wall, ${w(b)}. \n${if(b>1)"Take one down and pass it around"else
"Go to the store and buy some more"}, ${w((b+97)%99+1)} on the wall."}mkString "\n\n"
def w(b:Int)=s"$b bottle${if(b>1)"s"else ""} of beer"
0 comment threads
Python 3, 202 bytes
a,i='bottle%s of beer on the wall',99
while i:i-=1;x=a%'s'[:i];print(i+1,x+',',i+1,x[:-12]+'.\n'+'GToa kteo otnhee dsotwonr ea nadn dp absusy isto maer omuonrde,,'[i>0::2],i or 99,a%'s'[:i!=1]+'.\n')
0 comment threads
FALSE, 198 bytes
[" on the wall"]c:[$." bottle"$1=~["s"]?" of beer"]b:99[$1>][b;!c;!", "b;!".
Take one down, pass it around, "1-b;!c;!".
"]#1b;!" on the wall, "1b;!".
Go to the store and buy some more, "99b;!c;!"."
0 comment threads
C (gcc), 244 bytes
main(){for(char*w=" bottles of beer on the wall",i=100;--i;)printf("%d%.*s%s, %d%.*s%.8s.\n%s, %d%.*s%s.\n\n",i,i-1?8:7,w,w+8,i,i-1?8:7,w,w+8,i^1?"Take one down and pass it around":"Go to the store and buy some more",i^1?i-1:99,i^2?8:7,w,w+8);}
Full program, gcc extensions.
0 comment threads
Haskell, 221 215 bytes
f=a 99
(#)=(++)
a n=d n#", "#c".\n"n#b(n-1)
b 0="Go to the store and buy some more, "#d 99#"."
b n="Take one down and pass it around, "#d n#".\n\n"#a n
c m n=show n#" bottle"#['s'|n>1]#" of beer"#m
d=c" on the wall"
0 comment threads
Java (JDK), 311 301 296 bytes
interface A{static void main(String[]x){String a=" bottle",b=" of beer",c=" on the wall. ",e;for(int i=99;i>0;i--){e=i>1?"s":"";System.out.println(i+a+e+b+c+(i)+a+e+b+(i>1?".\nTake one down and pass it around. ":".\nGo to the store and buy some more. ")+(i>1?i-1:99)+a+(i!=2?"s":"")+b+c+"\n");}}}
This shortened if-else
condition thingy is actually pretty good.
0 comment threads
C (gcc), 232 bytes
f(n,w,p){printf("%i bottle%s of beer%s%s",n,"s"+(n<2)," on the wall"+w,p);}main(i){for(i=99;i;){f(i,0,", ");f(i,12,".\n");printf(i>1?"Take one down and pass it around, ":"Go to the store and buy some more, ");f(--i?:99,0,".\n\n");}}
Main golfing techniques:
-
The repetitive part has been put into a function
-
conditional strings are sometimes implemented using pointer arithmetic, using the fact that C strings are zero terminated character arrays
-
The code uses the implicit int rule
-
I'm passing a pointer through an int argument which then in turn is interpreted as a pointer by
printf
(not portable and definitely not clean; allows me to use the implicit int rule for that argument) -
The gcc extension
condition?:value
(givecondition
unless that is zero, in which case givevalue
) is used to save one character -
The printf function is declared implicitly (which isn't guaranteed to work by any C standard, but works just fine in practice by any compiler that allows implicit declarations), saving the 18 characters that would otherwise be needed for
#include<stdio.h>
(including the mandatory line break following that directive)
JavaScript (V8), 185 bytes
for(c=99;c;)print((f=(s=` on the wall`)=>`${c||99} bottle${~-c?`s`:``} of beer`+s)()+`, ${f``}.
${--c?`Take one down and pass it around`:`Go to the store and buy some more`}, ${f()}.
`)
0 comment threads
JavaScript (Node.js), 228 224 bytes
-4 bytes thanks to Hakerh400
(b=n=>`${n} bottle${n-1?'s':''} of beer`,w=' on the wall')=>[...Array(99)].map((_,i)=>`${b(i=99-i)}${w}, ${b(i)}.
`+(--i?`Take one down and pass it around, `:(i=99,`Go to the store and buy some more, `))+b(i)+w+'.').join`
`
Japt, 117 116 115 bytes
Needs more golfing or, perhaps, a completely different approach. Borrows a couple of tricks trick from ETHProductions' original version.
Includes 2 trailing newlines.
´LÇ=L+` Þ¤{´LÎçs} Þ8`".
{=Z+` e Ø!`}, {Z}.
{LÎg`Go e ÐJe d ¿y Ñ Ú
Take e ܵ d ps ÂÐ`·}, "iUìé~H
´LÇ=L+`...{´LÎçs}...`"...{=Z+`...`}...{Z}...{LÎg`...`·}..."iUìé~H
´L :Prefix decrement L (initially 100)
Ç :Map each Z in the range [0,L)
= : Reassign to Z
L+ : L concatenated with
` : Compressed string
... : " bottle"
{ : Interpolate
´LÎ : Prefix decrement L and get its sign
çs : Repeat "s" that many times
} : End interpolate
... : " of beer"
` : End compressed string
"... : Literal string (the return value)
{ : Interpolate
= : Assign to variable U
X+`...` : X & the compressed string " on the wall"
} : End interpolate
... : Literals
{X} : Interpolate D
... : Literals
{ : Interpolate
LÎg : Get the sign of L and index (0-based) into
`...` : Compressed string "Go to the store and buy some more\nTake one down and pass it around"
· : Split on newlines
} : End interpolate
..." : End of string
iU : Prepend U
à :End map
¬ :Join
é :Rotate right by
~H : The bitwise NOT of 32 (i.e., -33)
0 comment threads
Sclipting, (UTF-16) 238 bytes
눰 감下❶貶下標⓶긆깯덇끬뉗꼠닶눠눦녥댠❷감侔是검摧終併終❷긆뭮긇끨뉒걷눖롬 껂밀⓹껠뙔눖띥긆뭮뉒걤닷덮긆굮뉂거눗꽳긆땴긆굲닷녮뉂렠⓺❺껠똊終棄棄껠뙇닲건닲건늆넠댷끯댦넠눖멤긆깵뎒걳닶륥긆륯댦넬긃딹긆깯덇끬뉗꼠닶눠눦녥댢걯닢건늆넠덶구닂밎
Explanation
눰 감下
For n from 99 down to 1
❶貶下
For k from n to n - 1
標⓶긆깯덇끬뉗꼠닶눠눦녥댠
Push " bottles of beer"
❷감侔是검摧終
If k == 1, delete the 8th character from the end (the s)
併
Combine into a string -> "k bottle(s) of beer"
終
End loop
❷
Copy "n bottles of beer" from the stack
긆뭮긇끨뉒걷눖롬 껂밀
" on the wall" ", "
⓹
Move "n bottles of beer" from the stack
껠뙔눖띥긆뭮뉒걤닷덮긆굮뉂거눗꽳긆땴긆굲닷녮뉂렠
".\nTake one down and pass it around, "
⓺❺
Move "<n-1> bottles of beer". Copy " on the wall".
껠똊
".\n\n"
終
End loop
棄棄
Discard the top four items (which are the zero bottles text)
껠뙇닲건닲건늆넠댷끯댦넠눖멤긆깵뎒걳닶륥긆륯댦넬긃딹긆깯덇끬뉗꼠닶눠눦녥댢걯닢건늆넠덶구닂밎
".\nGo to the store and buy some more, 99 bottles of beer on the wall."
2 comment threads