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

#21 SamLowry   Members   -  Reputation: 1674

Like
2Likes
Like

Posted 06 July 2013 - 06:03 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.

 

If you would perhaps extend it to produce Pascal's triangle when feeding it a non-empty source file, it might become a serious contender to PacTri as language to be used in these kinds of contests.



Sponsor:

#22 Code Fox   Members   -  Reputation: 1809

Like
3Likes
Like

Posted 06 July 2013 - 06:32 AM

Theoretically any one who could write this program in Whitespace would win ...


Does Anyone Actually Read This ?
 


#23 swiftcoder   Senior Moderators   -  Reputation: 10369

Like
2Likes
Like

Posted 06 July 2013 - 08:34 AM


215 non white space characters - thanks Ruby

You are missing the singular case of bottles/bottle, and you have a comma instead of a period at the end of every second line.

 

The "canonical" output has 89 characters in unique runs, so entropy is the limiting factor in program size. One can reduce the size of the program pretty substantially by cutting corners in the output...


Tristam MacDonald - Software Engineer @Amazon - [swiftcoding]


#24 Aurioch   Crossbones+   -  Reputation: 1304

Like
0Likes
Like

Posted 06 July 2013 - 09:41 AM

C# version:

string b, w, t, n, c, g;
b = " bottles of beer";
c = b.Remove(7, 1);
w = " on the wall";
t = "Take one down and pass it around, ";
n = "o more";
for (int a = 99; a > 0; a--)
{
   g = a.ToString();
   Console.WriteLine(g + (a > 2 ? b : c) + w + ", " + g + (a > 2 ? b : c) + ((a > 1) ? (".\n" + t + (a - 1).ToString() + (a > 2 ? b : c) + w + ".\n") : "."));
}
Console.WriteLine(t + "n" + n + b + w + ".\n\nN" + n + b + w + ", n" + n + b + ".\nGo to the store and buy some more, 99" + b + w + ".");

349 non-whitespace characters. I have no idea how to reduce it more without cutting corners in output format...

 

Full solution is here, as is the result. I didn't count anything besides pure solution.



#25 Code Fox   Members   -  Reputation: 1809

Like
0Likes
Like

Posted 06 July 2013 - 09:54 AM

Here is the corrected version ... 250 characters . I'm going to see if I can slim that down a bit later today.

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

Edited by Shippou, 06 July 2013 - 10:24 AM.

Does Anyone Actually Read This ?
 


#26 swiftcoder   Senior Moderators   -  Reputation: 10369

Like
0Likes
Like

Posted 06 July 2013 - 11:06 AM


I have no idea how to reduce it more without cutting corners in output format...

I guess verbose languages like C# and Java pretty much screwed by the criteria themselves.

 

The variable declarations cost you 20 characters, and the length of Console.WriteLine and ToString cost even more.


Tristam MacDonald - Software Engineer @Amazon - [swiftcoding]


#27 Cornstalks   Crossbones+   -  Reputation: 6991

Like
0Likes
Like

Posted 06 July 2013 - 11:22 AM

I have no idea how to reduce it more without cutting corners in output format...

One common way of doing ToString() in "shorthand" is just doing +"" (in both C# and Java), which will convert the object to a string using ToString().

You could also save more by doing Console.Write() (and adding '\n' to the end) instead of Console.WriteLine(). Also, no need to use g. And some parens aren't needed. Here's a smaller version (296 non-whitespace characters):
 
string
b = " bottles of beer",
c = b.Remove(7, 1),
w = " on the wall",
t = "Take one down and pass it around, ",
n = "o more", x;
for (int a = 99; a>0;)
{
    x = a > 2 ? b : c;
    Console.WriteLine(a + x + w + ", " + a + x + (a-- > 1 ? ".\n" + t + a + (a > 1 ? b : c) + w + ".\n" : "."));
}
Console.Write(t + "n" + n + b + w + ".\n\nN" + n + b + w + ", n" + n + b + ".\nGo to the store and buy some more, 99" + b + w + ".\n");
output

I still think we should be counting whitespace, because it's actually significant. Try running a program with no whitespace... Also, I think C# and Java should include their using/import and class definitions.

Edited by Cornstalks, 06 July 2013 - 11:50 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 ]

#28 Aurioch   Crossbones+   -  Reputation: 1304

Like
0Likes
Like

Posted 06 July 2013 - 12:22 PM

Wow, thanks Cornstalks for showing me those tricks (mainly string with comma, which I didn't know) :D



#29 TheDespite   Members   -  Reputation: 104

Like
0Likes
Like

Posted 06 July 2013 - 12:32 PM

Do \n etc. Count as one character?



#30 ByteTroll   Crossbones+   -  Reputation: 1506

Like
0Likes
Like

Posted 06 July 2013 - 12:53 PM

  Closest I can get using "C" and no sleep!
 

#include <stdio.h>


void r(int i)
{
  if(i <= 0)
  {
    printf("No more bottles of beer");
    return;
  }
  printf("%d bottles of beer on the wall, %d bottles of beer\nTake one down and pass it around, %d bottles of beer on the wall\n\n", i, i, i);
  r(i - 1);
}


void main()
{
  r(99);
}

Edited by ByteTroll, 06 July 2013 - 12:59 PM.

▬▬▬▬▬▬▬▬▬▬▬ஜ۩۞۩ஜ▬▬▬▬▬▬▬▬▬▬▬▬▬
I see the future in 1's and 0's
▬▬▬▬▬▬▬▬▬▬▬ஜ۩۞۩ஜ▬▬▬▬▬▬▬▬▬▬▬▬▬

"This is called programming. The art of typing shit into an editor/IDE is not programming, it's basically data entry. The part that makes a programmer a programmer is their problem solving skills." - Serapth

#31 Tournicoti   Prime Members   -  Reputation: 684

Like
0Likes
Like

Posted 06 July 2013 - 01:02 PM

I can't anwer this, sorry, because I've drunk all the beers



#32 slicer4ever   Crossbones+   -  Reputation: 3983

Like
0Likes
Like

Posted 06 July 2013 - 01:04 PM

Also, I think C# and Java should include their using/import and class definitions.

 

I did, they add ~20-30 characters to your character count.


Edited by slicer4ever, 06 July 2013 - 01:05 PM.

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

#33 Cornstalks   Crossbones+   -  Reputation: 6991

Like
0Likes
Like

Posted 06 July 2013 - 01:13 PM

Closest I can get using "C" and no sleep!

*snip*

That doesn't print all the right words. The last verse is wrong (you're not going to the store to buy some more), and when you have 1 bottle left you still say bottles. And when you have 1 bottle of beer on the wall and you take it down, you mysteriously still have 1 bottle of beer on the wall. You're also missing periods.

Do \n etc. Count as one character?

Nope, they count as two.
[ 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 ]

#34 ByteTroll   Crossbones+   -  Reputation: 1506

Like
0Likes
Like

Posted 06 July 2013 - 01:18 PM

 

Closest I can get using "C" and no sleep!

*snip*

That doesn't print all the right words. The last verse is wrong (you're not going to the store to buy some more), and when you have 1 bottle left you still say bottles. And when you have 1 bottle of beer on the wall and you take it down, you mysteriously still have 1 bottle of beer on the wall. You're also missing periods.

Do \n etc. Count as one character?

Nope, they count as two.

 

 

Ah snap! I didn't even notice that ;P  I shall fix it.


▬▬▬▬▬▬▬▬▬▬▬ஜ۩۞۩ஜ▬▬▬▬▬▬▬▬▬▬▬▬▬
I see the future in 1's and 0's
▬▬▬▬▬▬▬▬▬▬▬ஜ۩۞۩ஜ▬▬▬▬▬▬▬▬▬▬▬▬▬

"This is called programming. The art of typing shit into an editor/IDE is not programming, it's basically data entry. The part that makes a programmer a programmer is their problem solving skills." - Serapth

#35 Mike.Popoloski   Crossbones+   -  Reputation: 2931

Like
10Likes
Like

Posted 06 July 2013 - 02:51 PM

 

Also, I think C# and Java should include their using/import and class definitions.

 

I did, they add ~20-30 characters to your character count.

 

 

Actually, with the Roslyn shell you no longer need the surrounding class / Main function cruft, just as if you were using the python shell. Granted, you should still include the using statements, since they're required.

 

Also, I see your 300+ C# entry and raise you 250 (even beats some of the other languages)  biggrin.png

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

It doesn't require the conditional operator either, which is nifty.


Edited by Mike.Popoloski, 06 July 2013 - 02:55 PM.

Mike Popoloski | Journal | SlimDX

#36 slicer4ever   Crossbones+   -  Reputation: 3983

Like
0Likes
Like

Posted 06 July 2013 - 02:59 PM



 



 

Also, I think C# and Java should include their using/import and class definitions.

 

I did, they add ~20-30 characters to your character count.

 

 

Actually, with the Roslyn shell you no longer need the surrounding class / Main function cruft, just as if you were using the python shell. Granted, you should still include the using statements, since they're required.

 

Also, I see your 300+ C# entry and raise you 250 (even beats some of the other languages)  biggrin.png

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

It doesn't require the conditional operator either, which is nifty.

 

impressive, i wrapped it up to be compilable with ideone: http://ideone.com/MX82jD adding the extra fluff for ideone's compiler bumps it up to 288, but still, quite impressive mate.


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

#37 swiftcoder   Senior Moderators   -  Reputation: 10369

Like
0Likes
Like

Posted 06 July 2013 - 03:14 PM

Also, I see your 300+ C# entry and raise you 250 (even beats some of the other languages)  biggrin.png

string f="{0:#;;",e=".\n",o="s;s;''} of beer",w=" on the wall",n="o more} bottle",x=n+"{1:"+o;
for(int a=99;a>=0;)System.Console.WriteLine(f+'N'+x+w+", "+f+'n'+x+e+
"{1:Take one down and pass it around;Go to the store and buy some more}, {1:#;99;n"+n+"{2:"+o+w+e,a,a-1,--a-1);
It doesn't require the conditional operator either, which is nifty.

That is a thing of beauty.

Tristam MacDonald - Software Engineer @Amazon - [swiftcoding]


#38 Cornstalks   Crossbones+   -  Reputation: 6991

Like
2Likes
Like

Posted 06 July 2013 - 05:53 PM

Also, I see your 300+ C# entry and raise you 250 (even beats some of the other languages)  biggrin.png

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

It doesn't require the conditional operator either, which is nifty.

You, sir, just blew my mind

Mind-Blown.jpg

 

Edit: this thread needs to be somewhere where I can upvote... I'm learning all sorts of tricks in this wide variety of languages.


Edited by Cornstalks, 06 July 2013 - 05:54 PM.

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

#39 swiftcoder   Senior Moderators   -  Reputation: 10369

Like
0Likes
Like

Posted 06 July 2013 - 06:05 PM


Edit: this thread needs to be somewhere where I can upvote... I'm learning all sorts of tricks in this wide variety of languages.

Your wish is my command.

 

Plus, this thread seemed to qualify from the beginning.


Tristam MacDonald - Software Engineer @Amazon - [swiftcoding]


#40 Aurioch   Crossbones+   -  Reputation: 1304

Like
0Likes
Like

Posted 06 July 2013 - 06:27 PM

 

Also, I see your 300+ C# entry and raise you 250 (even beats some of the other languages)  biggrin.png
string f="{0:#;;",e=".\n",o="s;s;''} of beer",w=" on the wall",n="o more} bottle",x=n+"{1:"+o;
for(int a=99;a>=0;)System.Console.WriteLine(f+'N'+x+w+", "+f+'n'+x+e+
"{1:Take one down and pass it around;Go to the store and buy some more}, {1:#;99;n"+n+"{2:"+o+w+e,a,a-1,--a-1);

It doesn't require the conditional operator either, which is nifty.

 

 

What? Wait... what? Can somebody translate that for me, because some things I didn't see yet in C# (or I saw but didn't have the need to use hence forgot it).







PARTNERS