LoginSignup
3
3

More than 5 years have passed since last update.

第21回オフラインリアルタイムどう書く groovy で

Posted at

問題 : http://d.hatena.ne.jp/torazuka/20140509/yhpg
他の実装へのリンク : http://qiita.com/torazuka/items/cbdb6b581a57e4754dd4
イベント : http://yhpg.doorkeeper.jp/events/10503

当日時間があれば書きたかった groovy。

// tested with Groovy Version: 2.3.0 JVM: 1.7.0_07 Vendor: Oracle Corporation OS: Mac OS X

def solve_impl( src )
{
  fixed=new HashSet()
  (0..<5).inject( [:] ){ acc,pri->
    src.each{
      (num,w)=it
      key=w[pri]
      att=( acc[key]=acc[key] ?: [] )
      if ( ! fixed.contains(num) && att.size<4 ){
        att.push( num )
        fixed.add(num)
      }
    }
    acc
  }
}

def format( r )
{
  r.keySet().findAll{ r[it] && ! r[it].isEmpty() }.sort().collect{
    [it, r[it].sort().join(":")].join("_")
  }.join("|")
}

def parse( src ){
  src.split(/\|/).collect{
    (a,b)=it.split("_")
    [a as Integer, b ]
  }
}

def solve(src){
  format( solve_impl( parse( src ) ) )
}

def test( src, expected )
{
  actual = solve( src )
  if ( actual == expected ){
    println "ok"
  } else {
    printf( "%s->%s / %s\n", src, actual, expected )
  }
}

/*0*/ test( "1_12345", "1_1" );
/*1*/ test( "30_32451", "3_30" );

いつも通り、テストデータの大半は省略。

ruby版( http://qiita.com/Nabetani/items/7d7c46438339b730cc4b )をそのまま移植した。

初期値付き HashMap がない(と思うんだけど、どうだろう)から ||= を使おうと思ったらそれもなく、仕方ないので if 文を書いてしまった。
なんか残念。だったんだけど、エルビス演算子( ?: )を使う機会が得られたのでよしとしようか。

3
3
4

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
3
3