Golf!

FerretBoy said:
Erm you mean the code is 40kb?

The below is 39 and it doesn't do anything..
Code:
#include <iostream>
int main ()
{	
}

Yes.....yes thats what I meant, sorry :( In the excitment I looked at 40 and say bytes, put 2 and 2 together and got 5 apparently :p, Ah well, yeah 40kb, I've since managed to get the code itself down to 4kb. But the exe is still 48kb
 
233
Code:
<?$a=' bottle%s of beer';$b=" on the wall";for($i=99;$i>0;){$s=$i>1?'s':'';printf("$i$a$b, $i$a.\n",$s,$s);echo($i-->1)?"Take one down and pass it around, $i":'Go to the store and buy some more, 99';printf("$a$b.\n\n",$i!=1?'s':'');}
 
Last edited:
357Bytes

Code:
void b(int _){if (_ == 1) {printf("1 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\n")    ; return 0;}printf("%i bottles of beer on the wall, %i bottles of beer.\nTake one down pass it around, %i bottles of beer on the wall.\n\n",_,_,_);return     b(--_);}int main(int argc,char** argv) { b(99); }

Haskell Version

alex@nop:~$ du -b beer2.hs
351 beer2.hs

351 bytes for the code (Too much for the binary! :-))

Code:
beer 1 = "1 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.\n"
beer n = show n ++ " bottles of beer on the wall, " ++ show n ++ " bottles of beer.\n"
	++ "Take one down and pass it around, " ++ show (n-1) ++ " bottles of beer on the wall.\n"  ++ beer (n-1)

main = putStrLn (beer 99)
 
Last edited:
Una said:
3440 Bytes.... can't get much smaller without using asm and stripping out the elf header etc, and making it totally non portable :-)

alex@nop:~$ du -b beer
3440 a.out

Code:
int main(int argc, char** argv)
{
	beer(99);
}

void beer(int bottles)
{
	if (bottles == 1)
	{
		printf("1 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\n");
		return 0;
	}
	printf("%i bottles of beer on the wall, %i bottles of beer.\nTake one down pass it around, %i bottles of beer on the wall.\n",bottles--,bottles,bottles);
	return beer(bottles--);
}

Looks like it could be made much smaller, also, it doesn't work with the singular after you've taken a bottle away from 2 bottles.
 
Mickey said:
Looks like it could be made much smaller, also, it doesn't work with the singular after you've taken a bottle away from 2 bottles.

Ah yeah good point :p !

EDIT: Fix0red.
 
Last edited:
Very similar PHP one (229 bytes):

Code:
$a="%d bottle%s of beer";$b=" on the wall";$i=99;while($i)printf("$a$b, $a.\n%s, $a$b.\n\n",$i,$i>1?'s':'',$i,$i>1?'s':'',$i>1?"Take one down and pass it around":"Go to the store and buy some more",--$i>0?$i:99,$i>1||!$i?'s':'');

I did write an extremely fruity gzip-based one based on a compressed version of the above but annoyingly had problems uncompressing weird characters. It was around 150 bytes though :)

arty

EDIT - Forgot to remove the braces, now 233 :p

EDIT - Dodgy bool cast, now 231.

EDIT - Added another dodgy bool, now 229.
 
Last edited:
C++ version at 358:
Code:
#include <iostream>
int main(char *z){using namespace std;char a[]=" bottle";char a2[]=" of beer";char b[]=" on the wall";for(int i=99;i>0;){char s=i>1?'s':'\0';cout<<i<<a<<s<<a2<<b<<", "<<i<<a<<s<<a2<<".\n";if(i-->1)cout<<"Take one down and pass it around, "<<i;else cout<<"Go to the store and buy some more, 99";cout<<a<<(i!=1?'s':'\0')<<a2<<b<<".\n\n";}}
 
Haskell again.

alex@nop:~$ du -b beer2.hs
321 beer2.hs

321Bytes.

Code:
bo,be :: String
bo = " bottles"++be++" on the wall"
be = " of beer"
b 1 = "\n"++"1 bottle"++be++", 1 bottle of beer.\nGo to the store and buy some more, 99"++bo++"."
b n = "\n"++show n++bo++ ", "++show n++" bottles"++be++".\n"++"Take one down and pass it around, "++show(n-1)++bo++".\n"++ b(n-1)
main = putStrLn (b 99)

Sure someone could get a perl version uber small :-)
 
Last edited:
c++ first stab, 374

Code:
#include <iostream>
#include <string>
using namespace std;typedef string s;int main (){s w=" of beer";s x=" bottle"+w;s y=x+" "+w;s z=" on the wall";for(int i=99;i>0;i--){s a=i>1?x:y;s b=i>2?x:y;cout<<i<<a<<z<<", "<<i<<a<< "."<<endl;if(i>1){cout<<"Take one down and pass it around, "<<i-1<<b<<z<<"."<<endl;}else cout<<"Go to the store and buy some more, 99 "<<x<<z<<".";}}
 
Code:
#include <iostream.h>
using namespace std;
int _tmain()
{for(int i = 99; i > 0; i--){
		cout << i << " bottles of beer on the wall, " << i << " bottles of beer\n";
		cout << "Take one round and pass it around " << i - 1 << " bottles of beer on the wall\n\n";
	}return 0;
}

265bytes

Second attempt, not gonna get below 100 I dont think lol
 
Last edited:
Steedie said:
Code:
#include <iostream.h>
using namespace std;
int _tmain()
{for(int i = 99; i > 0; i--){
		cout << i << " bottles of beer on the wall, " << i << " bottles of beer\n";
		cout << "Take one round and pass it around " << i - 1 << " bottles of beer on the wall\n\n";
	}return 0;
}

265bytes

Second attempt, not gonna get below 100 I dont think lol


You have no singular or bottle restocking on 1 :)
 
Una said:
You have no singular or bottle restocking on 1 :)

Bah.......

ok, well its only gonna be a simple if(bottles == 1) statement so add another 30bytes or so, still under 300, which aint tooooo bad lol
 
Mickey said:
C++ version at 358:
Code:
#include <iostream>
int main(char *z){using namespace std;char a[]=" bottle";char a2[]=" of beer";char b[]=" on the wall";for(int i=99;i>0;){char s=i>1?'s':'\0';cout<<i<<a<<s<<a2<<b<<", "<<i<<a<<s<<a2<<".\n";if(i-->1)cout<<"Take one down and pass it around, "<<i;else cout<<"Go to the store and buy some more, 99";cout<<a<<(i!=1?'s':'\0')<<a2<<b<<".\n\n";}}

Reduced it a little to 333:

Code:
#define O std::cout<<
#include <iostream>
int main(){char* a=" bottle";char* a2=" of beer";char* b=" on the wall";for(int i=99;i>0;){char s=i>1?'s':'\0';O i<<a<<s<<a2<<b<<", "<<i<<a<<s<<a2<<".\n";if(i-->1)O"Take one down and pass it around, "<<i;else O"Go to the store and buy some more, 99";O a<<(i!=1?'s':'\0')<<a2<<b<<".\n\n";}}
 
FerretBoy said:
Reduced it a little to 333:

Code:
#define O std::cout<<
#include <iostream>
int main(){char* a=" bottle";char* a2=" of beer";char* b=" on the wall";for(int i=99;i>0;){char s=i>1?'s':'\0';O i<<a<<s<<a2<<b<<", "<<i<<a<<s<<a2<<".\n";if(i-->1)O"Take one down and pass it around, "<<i;else O"Go to the store and buy some more, 99";O a<<(i!=1?'s':'\0')<<a2<<b<<".\n\n";}}
Knew somebody would ;) Been a looong time since i've done any C++
 
Mickey said:
Knew somebody would ;) Been a looong time since i've done any C++

just saw another change, 320 now:


Code:
#define O std::cout<<
#include <iostream>
int main(){char* a=" bottle",*a2=" of beer",*b=" on the wall",s;for(int i=99;i>0;){s=i>1?'s':'\0';O i<<a<<s<<a2<<b<<", "<<i<<a<<s<<a2<<".\n";if(i-->1)O"Take one down and pass it around, "<<i;else O"Go to the store and buy some more, 99";O a<<(i!=1?'s':'\0')<<a2<<b<<".\n\n";}}
 
Last edited:
ben_ said:
I cant believe OP posted this without posting the website to go along with it.
http://codegolf.com
I hope he didn't submit anyone's code ;)

Golfing is about 400 times older than that website, 99 bottles of beer is one of the oldest golfs, and you can't view other people's code on that website. Nice try though!
 
Golfing was one of the ways I learnt to code >10 years ago. As rob says, it's nothing new :)

Once you enter the real world though.. golfing gets a new rule along the lines of "... and in the least amount of time possible." :p
 
Last edited:
Back
Top Bottom