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

#81 samoth   Crossbones+   -  Reputation: 4684

Like
1Likes
Like

Posted 25 July 2013 - 02:23 AM

Yup, that was a great suggestion, thank you.

 

Modifying the raw source as follows, I managed to bring it down to 181 characters:

for($r=99;$r;--$r){$s=$r==1?:"s";echo "$r bottle$s of beer on the wall, $r bottle$s of beer.
Take one and pass it around, ",$r-1," bottle$s of beer on the wall.

";}echo "
No more bottles of beer on the wall, no more bottles of beer. 
Go to the store and buy some more, 99 bottles of beer on the wall.";

Basically, this tries to give the dictionary compressor a few more opportunities to match. That's harder to get right (and less intuitive, longer text sometimes compresses to smaller output!) than it sounds. Not only is the compression result a bit hard to predict from the editor, but also better compression may be larger in the end due to escaping.



Sponsor:

#82 Sik_the_hedgehog   Crossbones+   -  Reputation: 1607

Like
0Likes
Like

Posted 25 July 2013 - 05:09 AM

Well, you don't need to escape all those characters (you can probably store tabs as-is, and \" shouldn't need to be escaped since you're using the other kind of quote). Also remember gzdeflate can take a level parameter which determines how much it will try to compress (you may want to push it to the maximum, which is 9).


Don't pay much attention to "the hedgehog" in my nick, it's just because "Sik" was already taken =/ By the way, Sik is pronounced like seek, not like sick.

#83 samoth   Crossbones+   -  Reputation: 4684

Like
3Likes
Like

Posted 25 July 2013 - 05:44 AM

The level parameter won't change anything, the to-be-compressed string is far too short for that to have any effect (not that I didn't initially try a brute-force packer that chose the smallest of every possible level anyway, but that turned out a complete waste of time rolleyes.gif ).

 

As for not escaping all those characters, this was my first thought, too. Actually I thought that escaping \0 as well as single and double quotation marks should be enough. Then it occurred to me that backslash probably needed to be escaped, too. Turned out that still isn't enough, gzinflate will choke on tab and $ as well.

Further, there is indeed at least one character that I'm not escaping although I should to be "formally correct" (I didn't bother to find out which, as the result was longer than the best match anyway -- probably it's the backspace character or some other weird control code?).

 

I discovered this when trying to rename the loop variable to something that gives more matches than "$n". My first idea was "$e", seeing how e is the most frequent character in English. That was too naive and didn't work out (first, in this particular text, "o" is the most common character, and second it doesn't suffice to be frequent, you also need surrounding characters to match -- which made "r" being the most frequent end-of-word character the best candidate), But what's worst, it broke the decompressor.

 

Now "r" saved 2 bytes, which is nice, so I stuck with that. And incidentially, it works fine with the small subset of escaped characters, too.



#84 swiftcoder   Senior Moderators   -  Reputation: 9857

Like
4Likes
Like

Posted 25 July 2013 - 06:30 AM

Now "r" saved 2 bytes, which is nice, so I stuck with that. And incidentially, it works fine with the small subset of escaped characters, too.

Micro-optimizing data pre-compression.

Folks, that is a true coding horror.

Tristam MacDonald - Software Engineer @Amazon - [swiftcoding]


#85 RobTheBloke   Crossbones+   -  Reputation: 2334

Like
0Likes
Like

Posted 01 August 2013 - 01:57 PM

The brackets are need around the ternary operator since it's in a macro.

#include <iostream>
#define a(i) << " bottle" << (i-1?"s":"") << " of beer" << 
#define w " on the wall" <<

int main()
{   
    for(int i=100;i-->1;)
      std::cout << i a(i) w ", " << i a(i) ".\nTake one down and pass it around, " << i-1 a(i-1) w ".\n\n";
    std::cout << "No more" a() w ", no more" a() ".\nGo to the store and buy some more, 99" a() w ".";
}

After some tweaking 273 now

 

Sadly, it fails for 1 bottle of beer.



#86 Paragon123   Members   -  Reputation: 435

Like
0Likes
Like

Posted 02 October 2013 - 04:11 PM

C# 230 characters

 
class B
{
static void Main()
{
for(int i=100;i-->1;)
System.Console.WriteLine("{0} {6}{1} {2} {5}, {0} {6}{1} {2}.\nTake 1 down, pass it around {3} {6}{4} {2} {5}.",i,i>1?"s":"","of beer",i==1?"no more":(i-1).ToString(),i==2?"":"s","on the wall","bottle");
}
}

 

 



#87 Cornstalks   Crossbones+   -  Reputation: 6974

Like
0Likes
Like

Posted 02 October 2013 - 05:47 PM

 

C# 230 characters

 
class B
{
static void Main()
{
for(int i=100;i-->1;)
System.Console.WriteLine("{0} {6}{1} {2} {5}, {0} {6}{1} {2}.\nTake 1 down, pass it around {3} {6}{4} {2} {5}.",i,i>1?"s":"","of beer",i==1?"no more":(i-1).ToString(),i==2?"":"s","on the wall","bottle");
}
}

You missed the last verse, where you have to go to the store and buy some more.


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

#88 Shippou   Members   -  Reputation: 1471

Like
0Likes
Like

Posted 20 November 2013 - 12:10 AM

Who gets the award for "hardest to read code" ?


 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


 


#89 slicer4ever   Crossbones+   -  Reputation: 3485

Like
0Likes
Like

Posted 05 January 2014 - 02:48 PM

I just discovered this: http://esolangs.org/wiki/Var%27aq

klingon programming language with 99 bears implemented:

~ bottles { 
  latlh 1 rap'a' "" tam 
    { woD "s" } ghobe'chugh 
  " bottle" tam tlheghrar tam woD 
  " of beer" tlheghrar 
} pong

~ print-verse { 
  latlh latlh bottles tlheghrar " on the wall" tlheghrar cha'
  latlh latlh bottles tlheghrar cha'
  "Take one down and pass it around" cha'
  wa'boqHa' latlh latlh  { "No" } ghobe'chugh latlh bottles tlheghrar " on the wall" tlheghrar cha' 
  "" cha' 
  latlh { print-verse } HIja'chugh
} pong

99 print-verse

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

#90 rip-off   Moderators   -  Reputation: 8114

Like
4Likes
Like

Posted 05 January 2014 - 05:29 PM


klingon programming language with 99 bears implemented:

Fixed for you:

~ bottles { 
  latlh 1 rap'a' "" tam 
    { woD "s" } ghobe'chugh 
  " bottle" tam tlheghrar tam woD 
  " of bear" tlheghrar 
} pong
 
~ print-verse { 
  latlh latlh bottles tlheghrar " on the wall" tlheghrar cha'
  latlh latlh bottles tlheghrar cha'
  "Take one down and pass it around" cha'
  wa'boqHa' latlh latlh  { "No" } ghobe'chugh latlh bottles tlheghrar " on the wall" tlheghrar cha' 
  "" cha' 
  latlh { print-verse } HIja'chugh
} pong
 
99 print-verse


#91 swiftcoder   Senior Moderators   -  Reputation: 9857

Like
1Likes
Like

Posted 06 January 2014 - 07:58 AM

Apparently our syntax highlighter is discriminatory towards klingons.


Tristam MacDonald - Software Engineer @Amazon - [swiftcoding]


#92 boogyman19946   Members   -  Reputation: 1051

Like
2Likes
Like

Posted 10 January 2014 - 12:47 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.

 

Someone has already kinda-sorta beat you to the punch biggrin.png

 

I'm not sure if anyone has mentioned this in the thread, but... introducing HQ9+

9

99 Bottles of Beer in 1 character.

 

EDIT: Ehh, what the heck, I thought I'd join the fun. My weapon of choice is C:

#define z(s,t) h(s,t," bottle%s of beer"," on the wall",i:,"o more",i==1?"":"s")
#define h(a,q,b,c,d,e,f) printf(a b c", "a b".\n%s, "q b c".\n\n",i?d"N"e,f,i!=0?d"n"e,f,i?"Take one down and pass it around":"Go to the store and buy some more",i==0?99:i!=1?i-1:"n"e,i==2?"":"s")
main(){int i=99;for(;i>=0;i--)i==0?z("%s","%i"):i==1?z("%i","%s"):z("%i","%i");}

Also, I got it on codepad to show that it in fact executes correctly: http://codepad.org/CIeL7PZm

 

It is 358 characters I think. I compiled it using GCC with both -std=99 and -std=89 and the results are the same. It compiles with many warnings but it runs nonetheless. 

 

Fun fact, I ran GCC -E when I was testing the macros for correctness, and they expand to over 1000 characters of code XD


Edited by boogyman19946, 10 January 2014 - 10:30 PM.

"If highly skilled generalists are rare, though, then highly skilled innovators are priceless." - ApochPiQ

My personal links :)
- Khan Academy - For all your math needs
- Java API Documentation - For all your Java info needs :D
- C++ Standard Library Reference - For some of your C++ needs ^.^

#93 Shippou   Members   -  Reputation: 1471

Like
1Likes
Like

Posted 28 January 2014 - 01:04 PM

I have yet to see some one use WhiteSpace ...


 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


 


#94 Ravyne   Crossbones+   -  Reputation: 7116

Like
1Likes
Like

Posted 28 January 2014 - 06:10 PM

#include <stdio.h>

int main (void)
{
#define P(A)printf(A,i,i,i)
#define B(A,P)#A" bottle"#P" of beer on the wall"
#define V(P)B(%d,P)", %d bottle"#P" of beer.\nTake one down and pass it around, "

    int i=99;
    P(V(s));

#define R B(%d,s)".\n\n"V(s)

    while(--i>1)
        P(R);

#define R B(1,\?\b)".\n\n"V(\?\b)

    P(R B(no more,s)".\n\n"B(No more,s)", no more bottles of beer.\nGo to the store and buy some more, "B(99,s)".\n");
}

378 characters including significant whitespace, 396 with the #include . Output is perfect including caps, punctuation, newlines and final verses. I could probably hoist out a few #defines, or #define the #defines and save another 20-30 chars, but I'm weary of this exercise for the moment smile.png Gotta love code that only reads top to bottom!


Edited by Ravyne, 28 January 2014 - 06:15 PM.


#95 boogyman19946   Members   -  Reputation: 1051

Like
0Likes
Like

Posted 28 January 2014 - 08:19 PM

#include <stdio.h>

int main (void)
{
#define P(A)printf(A,i,i,i)
#define B(A,P)#A" bottle"#P" of beer on the wall"
#define V(P)B(%d,P)", %d bottle"#P" of beer.\nTake one down and pass it around, "

    int i=99;
    P(V(s));

#define R B(%d,s)".\n\n"V(s)

    while(--i>1)
        P(R);

#define R B(1,\?\b)".\n\n"V(\?\b)

    P(R B(no more,s)".\n\n"B(No more,s)", no more bottles of beer.\nGo to the store and buy some more, "B(99,s)".\n");
}

378 characters including significant whitespace, 396 with the #include . Output is perfect including caps, punctuation, newlines and final verses. I could probably hoist out a few #defines, or #define the #defines and save another 20-30 chars, but I'm weary of this exercise for the moment smile.png Gotta love code that only reads top to bottom!

 

Codepad seems to be messing up your output. At the 1 bottle of beer line, it prints bottle? instead of bottle, although my console seems to get it right.


Edited by boogyman19946, 28 January 2014 - 08:23 PM.

"If highly skilled generalists are rare, though, then highly skilled innovators are priceless." - ApochPiQ

My personal links :)
- Khan Academy - For all your math needs
- Java API Documentation - For all your Java info needs :D
- C++ Standard Library Reference - For some of your C++ needs ^.^

#96 Alpha_ProgDes   Crossbones+   -  Reputation: 4688

Like
0Likes
Like

Posted 28 January 2014 - 08:23 PM

Apparently our syntax highlighter is discriminatory towards klingons.


You should fix that Senior Moderator guy.
Beginner in Game Development? Read here.
 
Super Mario Bros clone tutorial written in XNA 4.0 [MonoGame, ANX, and MonoXNA] by Scott Haley
 
If you have found any of the posts helpful, please show your appreciation by clicking the up arrow on those posts Posted Image
 
Spoiler

#97 Ravyne   Crossbones+   -  Reputation: 7116

Like
1Likes
Like

Posted 28 January 2014 - 10:07 PM


Codepad seems to be messing up your output. At the 1 bottle of beer line, it prints bottle? instead of bottle, although my console seems to get it right.

 

It must not support the escape sequence for backspace then. I have to feed one of my macros, and I do so with a \?\b (question mark, backspace) sequence to keep it happy. I suppose I could have just used an \a (alert/bell) sequence and saved 4 characters, but I felt like that was cheating.

 

Cleaning up, hoisting " on the wall", and using \a, now I've got:

#include <stdio.h>

int main()
{
#define P(M)printf(M,i,i,i) //27:27
#define W " on the wall" //24:51
#define B(A,S)#A" bottle"#S" of beer" //37:88
#define V(S)B(%d,S)W", "B(%d,S)".\nTake one down and pass it around, " //70:158

	int i = 99; //11:169
	P(V(s)); //8:177

#define R(S)B(%d,S)W".\n\n"V(S) //31:208

	while (--i>1) //13:221
		P(R(s)); //8: 229

	P(R(\a)B(no more, s)W".\n\n"B(No more, s)W", "B(no more, s)".\nGo to the store and buy some more, "B(99, s)W"."); //109:338
} //+18 (#include) +12 (main) = 370 characters

Which is 338 characters for the body, and 370 in total (#include, main, body), including significant whitespace. And its actually fairly reasonable to follow.



#98 Sutayh   Members   -  Reputation: 173

Like
0Likes
Like

Posted 10 February 2014 - 02:20 AM

Just throwing my hat in the ring, though it's not the shortest:

#include<iostream>
#include<sstream>
using namespace std;
string m="o more",v=" bottle",t=" of beer",s=" on the wall";
string a(int n,int c=0)
{
stringstream b;
b << n;
return (n?b.str():(c?"N":"n")+m)+(n==1?v:v+"s");
}
int main()
{
for(int l=99;l>-1;--l)
{
cout << a(l,1)+t+s+", "+a(l)+t+".\n"+(l?"Take one down and pass it around, ":"Go to the store and buy some more, ")+(a(l?l-1:99))+t+s+".\n\n";
}
} 

370 characters, 332 without "any" whitespace.


Edited by Sutayh, 11 February 2014 - 02:56 AM.






PARTNERS