LoginSignup
5
4

More than 5 years have passed since last update.

ワンライナーズンドコキヨシ with Ruby

Last updated at Posted at 2016-03-11

ズンドコキヨシ with Rubyを見てワンライナーで書いてみた。
ついでにコードゴルフに挑戦してみたが、私にはこれが限界であった。
もう少し短く出来そうな気がするのだが...。

z,d=a=%W|ズン ドコ|;0 until (0..4).map{puts w=a.sample;w}.join==z*4+d;puts "キ・ヨ・シ!"

他のズンドコ実装を見るといつの間にやら5個区切りの判定をしているのは私ぐらいになってしまったので、他の実装と合わせたものを急遽考えてみた。
...が、長くなってしまったのでもうちょっと実装を考え直したい。

z,d=a=%W|ズン ドコ|;s=[];puts (s=(s<<a.sample).last(5))[-1] until s.join==z*4+d;puts "キ・ヨ・シ!"

配列から文字列に変えたらもっと短く出来た。

z,d=a=%W|ズン ドコ|;puts (s+=w=a.sample)[w] until (s||="")[z*4+d];puts "キ・ヨ・シ!"

改行を諦めればputsよりもSTDOUTに文字列を流すほうが短く済む。

z,d=a=%W|ズン ドコ|;$><<(s+=w=a.sample)[w] until (s||="")[z*4+d];$><<"キ・ヨ・シ!"

最後に結果だけ出せばいいのであれば途中でSTDOUTに流す必要がなくなるので更に色々削れる。

s="";z,d=a=%W|ズン ドコ|;0until(s<<a.sample)[z*4+d];$><<s+"キ・ヨ・シ!"
5
4
3

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
4