Jump to content

  • Log In with Google      Sign In   
  • Create Account


99 Bottles Of Beer Challenge With Least Amount Of Characters ?

  • You cannot reply to this topic
97 replies to this topic

#1 Shippou   Members   -  Reputation: 1471

Like
0Likes
Like

Posted 05 July 2013 - 07:28 PM

99 Bottles Of Beer Project  is a collection of the "99 Bottles Of Beer" song written in over 1,500 different programming languages.

 

 I was wondering, what is the shortest program you can write, that outputs the whole song "99 Bottles Of Beer" ( total characters ) ?


Edited by Shippou, 05 July 2013 - 07:29 PM.

 Reactions To Technologies:
1. Anything that is in the world when you’re born is normal and ordinary and is just a natural part of the way the world works.
2. Anything that's invented between when you’re fifteen and thirty-five is new and exciting and revolutionary and you can probably get a career in it.
3. Anything invented after you're thirty-five is against the natural order of things.

- Douglas Adams 2002


 


Sponsor:

#2 slicer4ever   Crossbones+   -  Reputation: 3414

Like
0Likes
Like

Posted 05 July 2013 - 07:41 PM

c++ version, smallest that i can come up with: http://codepad.org/dDT47DTr


Check out https://www.facebook.com/LiquidGames for some great games made by me on the Playstation Mobile market.

#3 Servant of the Lord   Crossbones+   -  Reputation: 18242

Like
0Likes
Like

Posted 05 July 2013 - 08:07 PM

I see your 473 non-whitespace characters, and raise you 331.

 

(Yes, it's permitted in the C++ standard to not include the return statement in the main() function)

 

[Edit:] Got it to 314 non-whitespace characters. Still has the flaw @slicer mentions below. =(...


Edited by Servant of the Lord, 05 July 2013 - 08:24 PM.

It's perfectly fine to abbreviate my username to 'Servant' rather than copy+pasting it all the time.

[Fly with me on Twitter] [Google+] [My broken website]

All glory be to the Man at the right hand... On David's throne the King will reign, and the Government will rest upon His shoulders. All the earth will see the salvation of God.                                                                                                                                                            [Need web hosting? I personally like A Small Orange]
Of Stranger Flames - [indie turn-based rpg set in a para-historical French colony] | Indie RPG development journal


#4 slicer4ever   Crossbones+   -  Reputation: 3414

Like
0Likes
Like

Posted 05 July 2013 - 08:12 PM

I see your 473 non-whitespace characters, and raise you 331.

 

(Yes, it's permitted in the C++ standard to not include the return statement in the main() function)

aha! but you missed that instead of "0 bottles of beer on the wall." it's suppose to be: "no more bottles of beer on the wall."

 

=-)


Check out https://www.facebook.com/LiquidGames for some great games made by me on the Playstation Mobile market.

#5 SiCrane   Moderators   -  Reputation: 9498

Like
0Likes
Like

Posted 05 July 2013 - 08:28 PM

I'm disappointed I haven't been seeing more obfuscated C++ tricks like using the preprocessor definitions and implicit string concatenation to shrink the string literals. For instance, "bottles of beer" is repeated in a number of places in both of those code samples so you can use things like
#define B "bottles of beer"
printf("No more " B " on the wall, no more " B ".\n"
You guys are actually producing readable code.

#6 swiftcoder   Senior Moderators   -  Reputation: 9763

Like
0Likes
Like

Posted 05 July 2013 - 08:34 PM

I see your 473 non-whitespace characters, and raise you 331.

 

[Edit:] Got it to 314 non-whitespace characters. Still has the flaw @slicer mentions below. =(...

 

By the powers vested in me by Python, I see your 314 and raise you 274.


Tristam MacDonald - Software Engineer @Amazon - [swiftcoding]


#7 Servant of the Lord   Crossbones+   -  Reputation: 18242

Like
0Likes
Like

Posted 05 July 2013 - 08:40 PM

Ah, but now you have a mistake in your output! You went from 1 bottle of beer to none, without passing it around. tongue.png


It's perfectly fine to abbreviate my username to 'Servant' rather than copy+pasting it all the time.

[Fly with me on Twitter] [Google+] [My broken website]

All glory be to the Man at the right hand... On David's throne the King will reign, and the Government will rest upon His shoulders. All the earth will see the salvation of God.                                                                                                                                                            [Need web hosting? I personally like A Small Orange]
Of Stranger Flames - [indie turn-based rpg set in a para-historical French colony] | Indie RPG development journal


#8 cowsarenotevil   Crossbones+   -  Reputation: 1979

Like
0Likes
Like

Posted 05 July 2013 - 08:52 PM

 

I see your 473 non-whitespace characters, and raise you 331.

 

[Edit:] Got it to 314 non-whitespace characters. Still has the flaw @slicer mentions below. =(...

 

By the powers vested in me by Python, I see your 314 and raise you 274.

 

 

I don't know Python, but following from what SiCrane said, could you perhaps make line 6 stick an 's' into the string a, removing the need for a substantially-identical string b?

 

EDIT: This seems to work with fewer characters, and I'm sure someone who has actually used Python, you know, ever, could do this in a smarter way.

a = 'bottles of beer'
w = 'on the wall.\n'

def D(i):
	return i if i!=0 else 'no more'

def Q(i):
	return a if i!=1 else "".join(a.split("s"))

def C(i):
	return '\nTake one down and pass it around,' if i>0 else '\nGo to the store and buy some more,'

for i in range(99,-1,-1):
    print D(i), Q(i), 'on the wall,', D(i), Q(i)+'.', C(i), D(i-1), Q(i-1), w

#print 'No more', b, 'on the wall, no more', b+'.'+C(i), '99', b, w

EDIT: Oops, I must have stolen swiftcoder's code to make that change at a moment when it was broken, so there's an off-by-one error in mine.


Edited by cowsarenotevil, 05 July 2013 - 09:09 PM.

-~-The Cow of Darkness-~-

#9 swiftcoder   Senior Moderators   -  Reputation: 9763

Like
0Likes
Like

Posted 05 July 2013 - 09:06 PM


I don't know Python, but following from what SiCrane said, could you perhaps make line 6 stick an 's' into the string a, removing the need for a substantially-identical string b?

Yeah, but unfortunately I also had a couple of bugs, so even with that, I had to go back up to 290 to fix them...

 

Edit: that's still 30 characters shorter than the shortest complete example on the site, so I guess I am happy.


Edited by swiftcoder, 05 July 2013 - 09:11 PM.

Tristam MacDonald - Software Engineer @Amazon - [swiftcoding]


#10 slicer4ever   Crossbones+   -  Reputation: 3414

Like
0Likes
Like

Posted 05 July 2013 - 10:02 PM



I'm disappointed I haven't been seeing more obfuscated C++ tricks like using the preprocessor definitions and implicit string concatenation to shrink the string literals. For instance, "bottles of beer" is repeated in a number of places in both of those code samples so you can use things like

#define B "bottles of beer"
printf("No more " B " on the wall, no more " B ".\n"
You guys are actually producing readable code.

 

challanged accepted: http://codepad.org/jHzuL4zd

 

not sure what you guys are using to do non-whitespace counting, but i don't feel like counting them all to give a number.

 

edit: made minor fix.


Edited by slicer4ever, 05 July 2013 - 10:03 PM.

Check out https://www.facebook.com/LiquidGames for some great games made by me on the Playstation Mobile market.

#11 swiftcoder   Senior Moderators   -  Reputation: 9763

Like
1Likes
Like

Posted 05 July 2013 - 10:20 PM


not sure what you guys are using to do non-whitespace counting, but i don't feel like counting them all to give a number.

Character counting a la the internet.

 

You are rocking a 330, and you have a capitalization error on the 2nd-to-last line (I'm quibbling, I know, but that capital cost me 5 characters to get right).


Tristam MacDonald - Software Engineer @Amazon - [swiftcoding]


#12 slicer4ever   Crossbones+   -  Reputation: 3414

Like
0Likes
Like

Posted 05 July 2013 - 10:25 PM

 


not sure what you guys are using to do non-whitespace counting, but i don't feel like counting them all to give a number.

Character counting a la the internet.

 

You are rocking a 330, and you have a capitalization error on the 2nd-to-last line (I'm quibbling, I know, but that capital cost me 5 characters to get right).

 

ah thanks=-).  had to bump it up by 3 characters to fix that, and one other mistake: http://codepad.org/aDEAu5R6

 

edit: here's a c# version: http://ideone.com/S7Hcs5 clocks in at 382 characters.

 

edit2: managed to reduce it down to 338 characters.


Edited by slicer4ever, 05 July 2013 - 11:24 PM.

Check out https://www.facebook.com/LiquidGames for some great games made by me on the Playstation Mobile market.

#13 Endurion   Crossbones+   -  Reputation: 3418

Like
0Likes
Like

Posted 05 July 2013 - 11:50 PM

Silly little change to get the C# to 330 smile.png

 

http://ideone.com/D0iVbE


Edited by Endurion, 05 July 2013 - 11:58 PM.

Fruny: Ftagn! Ia! Ia! std::time_put_byname! Mglui naflftagn std::codecvt eY'ha-nthlei!,char,mbstate_t>

#14 Andy474   Members   -  Reputation: 671

Like
0Likes
Like

Posted 06 July 2013 - 01:35 AM

Ah, but now you have a mistake in your output! You went from 1 bottle of beer to none, without passing it around. tongue.png

 

He stole my beer!



#15 SamLowry   Members   -  Reputation: 1605

Like
0Likes
Like

Posted 06 July 2013 - 02:25 AM

z = concat
a = " bottle"
e = a ++ "s"

y = "o more"
b 0 = "n" ++ y ++ e
b 1 = "1" ++ a
b n = show n ++ e

c = " of beer"
d = c ++ " on the wall"

f 0 = z [ "N", y, e, d, ", ", b 0, c, ".\nGo to the store and buy some more, ", b 99, d, ".\n" ]
f n = z [ b n, d, ", ", b n, c, ".\nTake one down and pass it around, ", b $ n-1, d, ".\n\n" ]

main = mapM_ (putStr . f) [99,98..0]

260 nonwhitespaces using Haskell.



#16 Cornstalks   Crossbones+   -  Reputation: 6974

Like
0Likes
Like

Posted 06 July 2013 - 03:14 AM

C99, 365 characters including whitespace (newlines and all), 314 excluding whitespace (I personally think we should be counting whitespace):
#define T ".\nTake one down and pass it around, "
#define B " bottles of beer"
#define b "1 bottle of beer"
#define W B" on the wall"
#define w b" on the wall"
int main(){for(int j=100;j--;)printf(j?j>1?"%d"W", %d"B:w", "b:"No more"W", no more"B,j,j),printf(j?j>1?j>2?T"%d"W".\n\n":T w".\n\n":T"no more"W".\n\n":".\nGo to the store and buy some more, 99"W".",j-1);}
I'm tired. It's time for bed. I might try some more tomorrow.

Edit: I just learned implicit function declaration was made illegal in C99, so my code is an unholy union of C89 and C99. Here's a C89 version (added 10 characters):
#define T ".\nTake one down and pass it around, "
#define B " bottles of beer"
#define b "1 bottle of beer"
#define W B" on the wall"
#define w b" on the wall"
int main(){for(int j=100;j--;)printf(j?j>1?"%d"W", %d"B:w", "b:"No more"W", no more"B,j,j),printf(j?j>1?j>2?T"%d"W".\n\n":T w".\n\n":T"no more"W".\n\n":".\nGo to the store and buy some more, 99"W".",j-1);return 0;}
Edit edit: I think these both technically invoke undefined behavior. I ended up being curious about the legal-ness of implicitly declared functions and asked on StackOverflow. According to the answer (and the comments), implicitly declared functions in C89 are legal, but only for specific types of functions, and printf isn't one of those. Learned something new!

Edited by Cornstalks, 07 July 2013 - 12:24 AM.

[ I was ninja'd 71 times before I stopped counting a long time ago ] [ f.k.a. MikeTacular ] [ My Blog ] [ SWFer: Gaplessly looped MP3s in your Flash games ]

#17 Shippou   Members   -  Reputation: 1471

Like
0Likes
Like

Posted 06 July 2013 - 04:54 AM

215 non white space characters - thanks Ruby

i = 99
x = " bottles of beer"
y = x + " on the wall, "
z = "Take one down, pass it around, "
a = ".\n"
b = "no more "
while i > 1
    q = i.to_s
    r = (i - 1).to_s
    puts q + y + q + x + a  + z + r + y
    i -= 1
end
q = i.to_s
puts q + y + q + x + a + z + b + y,"No more" + y + b + x + a + "Go to the store and buy some more, 99" + y

Edited by Shippou, 06 July 2013 - 04:55 AM.

 Reactions To Technologies:
1. Anything that is in the world when you’re born is normal and ordinary and is just a natural part of the way the world works.
2. Anything that's invented between when you’re fifteen and thirty-five is new and exciting and revolutionary and you can probably get a career in it.
3. Anything invented after you're thirty-five is against the natural order of things.

- Douglas Adams 2002


 


#18 Juliean   GDNet+   -  Reputation: 2366

Like
0Likes
Like

Posted 06 July 2013 - 05:50 AM

#include <fstream>
#include <iostream>

void main()
{
std::fstream s;
s.open("b", std::ios::in);

while(s)
{
char c;
s.get(c);
std::cout << c;
}
}

160 characters. Yet I've got the feeling that this one doesn't count...


Edited by Juliean, 06 July 2013 - 05:52 AM.


#19 Bacterius   Crossbones+   -  Reputation: 8319

Like
10Likes
Like

Posted 06 July 2013 - 05:55 AM

Nobody said we had to use a real language. Therefore, zero characters, using the newly created "bottles of beer" language..


The specification of the "bottles of beer" language states that:

 

An empty source file shall produce a program which prints out the lyrics of the "99 bottles of beer" song. The behaviour of non-empty source files is unspecified.

 

 

Thank you.


The slowsort algorithm is a perfect illustration of the multiply and surrender paradigm, which is perhaps the single most important paradigm in the development of reluctant algorithms. The basic multiply and surrender strategy consists in replacing the problem at hand by two or more subproblems, each slightly simpler than the original, and continue multiplying subproblems and subsubproblems recursively in this fashion as long as possible. At some point the subproblems will all become so simple that their solution can no longer be postponed, and we will have to surrender. Experience shows that, in most cases, by the time this point is reached the total work will be substantially higher than what could have been wasted by a more direct approach.

 

- Pessimal Algorithms and Simplexity Analysis


#20 Shippou   Members   -  Reputation: 1471

Like
0Likes
Like

Posted 06 July 2013 - 06:02 AM

The characters from your imported files also count .... tongue.png

import BoB

 Reactions To Technologies:
1. Anything that is in the world when you’re born is normal and ordinary and is just a natural part of the way the world works.
2. Anything that's invented between when you’re fifteen and thirty-five is new and exciting and revolutionary and you can probably get a career in it.
3. Anything invented after you're thirty-five is against the natural order of things.

- Douglas Adams 2002


 






PARTNERS