第六回オフラインリアルタイムどう書くの参考問題
http://qiita.com/items/4c60f10b73812e86441c
の実装。
まだ出てない言語で行こうということで、まずは groovy で。
Land.groovy
// groovy 2.0.1
def ptInRect( x, y, a, b )
{
(0..1).every{
(a[it]..b[it]).contains( [x,y][it] )
}
}
def solve( src )
{
areas=src.split(",").collect{
it.split( "-" ).collect{ it.chars.collect{ it-('0' as char)} }
}
(0..99).count{ xy->
x=xy%10
y=xy/10 as int /*// to help markdown */
areas.every{
ptInRect( x, y, it[0], it[1] ) || ptInRect( x, y, it[0], it[2] )
}
}
}
testData().each{
actual = solve( it[0] )
if ( actual != it[1] ){
printf( "**EXPECTED is %d ***", it[1] )
}
printf( "%s -> %d\n", it[0], actual );
}
def testData(){
"""\
#1 23-94-28,89-06-51 11
#2 11-84-58,02-73-69 40
#3 18-41-86,77-04-32 26
#4 81-88-23,64-58-14 0
#49 28-31-92,13-98-79 48
""".split( /\n/ ).collect{
m = ( it =~ /\#\d+\s+(\S+)\s+(\d+)/ )
m ? [ m[0][1], Integer.decode( m[0][2] ) ] : null
}.findAll{ it }
;}
例によって、テストデータの大半は省略。
ruby の map や all が groovy だと collect や every であることになかなか慣れない。あと、divmod が無いらしい(見つけられてないだけかも)。
それと、10/2 が 2(Integer) ではなく 2.5(BigDecimal) になることを忘れていて、ちと苦しんだ。
実装の方針は、マス目の数が多くなったら遅すぎて駄目になるけどまあ 10✕10 固定なので全マス検査でいいよね、という態度で。
ruby と比べて気持ちいいのは、やっぱり it。