99本のビールでCode Golf


ビールが好きです。
99 Bottles of Beerという有名なお題があるようなので今日はそのCode Golfの話。

Code Golfとは

コードゴルフはコンピュータプログラミング・コンテストの一種。参加者は与えられたアルゴリズムを、可能な限りもっとも短いソースコードで記述することを競う[1]。バイナリサイズではなく、ソースコードの文字数がスコアとなる
Wikipedia

例えばFizzBuzz問題のCode Golf解説がこちら

なのですが、Code Golfのお題が大量にあるのを見つけた。
それがこれ。https://code.golf/

99 bottles of beer

Display the complete lyrics for the song: 99 Bottles of Beer on the Wall.
→「"99 Bottles of Beer on the Wall." の歌詞を表示するプログラムを書け」ということなのですね。

Beer.java
import java.text.MessageFormat;

class Beer {
    static String bottles(int n) {
        return MessageFormat.format("{0,choice,0#No more bottles|1#One bottle|2#{0} bottles} of beer", n);
    }

    public static void main(String[] args) {
        String bottles = bottles(99);
        for (int n = 99; n > 0; ) {
            System.out.println(bottles + " on the wall");
            System.out.println(bottles);
            System.out.println("Take one down, pass it around");
            bottles = bottles(--n);
            System.out.println(bottles + " on the wall");
            System.out.println();
        }
    }
}

とか書いていく。安直には以下みたいな感じ。
ポイントとしては歌詞の最後のほう 1 はbottlesではなくてbottleだしNo more以下の歌詞をどう処理するかです。

Beer.java
import java.text.MessageFormat;

class Beer {
    static String bottles(int n) {
        return MessageFormat.format("{0,choice,0#no more bottles|1#1 bottle|2#{0} bottles} of beer", n);
    }

    public static void main(String[] args) {
        String bottles = bottles(99);
        for (int n = 99; n > 0; ) {
            System.out.println(bottles + " on the wall, " + bottles+".");
            bottles = bottles(--n);
            System.out.println("Take one down and pass it around, "+ bottles + " on the wall.");
            System.out.println();
        }
        System.out.println("No more bottles of beer on the wall, no more bottles of beer.");
        System.out.println("Go to the store and buy some more, 99 bottles of beer on the wall.");
    }
}

実行

とか、お題はクリアできるのだけどランクインするためにはもう少し削らないといけない。

結果

Beer.java
import java.text.MessageFormat; 
class Beer {
static String b(int n) {
return MessageFormat.format("{0,choice,0#no more bottles|1#1 bottle|2#{0} bottles} of beer", n);
}
public static void main(String[] args) {
String b = b(99);
for (int n = 99; n > 0;) {
System.out.println(b + " on the wall, " + b+".");
b= b(--n);
System.out.println("Take one down and pass it around, "+ b+ " on the wall.\n");
}
System.out.println("No more bottles of beer on the wall, no more bottles of beer.\nGo to the store and buy some more, 99 bottles of beer on the wall.");}
}

とか小賢しい感じに削ると、とりあえず14位になったよ。

今日は以上~~!