勉強会当日は時間内に解けなかったので、家で解き直しました。
もっと精進します。
object Hena {
def makeBox(src:String, w:Int, h:Int) = {
src.map(arg => {
val i = arg.toString.toInt
List.fill((i - h).abs)('0') ++ List.fill(i)('1')
}).toList.transpose
}
def overflowFromLeft(i:Int, l:List[Char]) = {
List.fill(i)(' ') ++ l.mkString.drop(i).toList
}
def overflowFromRight(i:Int, l:List[Char]) = {
l.mkString.dropRight(i).toList ++ List.fill(i)(' ')
}
def overflow(arg:List[Char]) = {
val l = overflowFromLeft(arg.indexOf('1'), arg)
overflowFromRight(l.size - arg.lastIndexOf('1') - 1, l)
}
def solve(src:String) = {
val items = src.split("0").filter(!_.isEmpty)
items.map(item => {
val (w, h) = (item.length, item.max.toInt)
makeBox(item, w, h).map(arg => arg match {
case _ if(arg.head == '0' || arg.last == '0') => overflow(arg)
case _ => arg
}).flatten
}).mkString.count(_ == '0').toString
}
def test(src:String, expected:String) = {
val actual = solve(src)
println(if(expected == actual) "ok" else "***NG***")
}
def main(args:Array[String]) = {
/*0*/ test( "83141310145169154671122", "24" )
// 以下略
}
}