0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

オフラインどう書く第13回回答例 ( Scala )

Last updated at Posted at 2013-09-06

勉強会当日は時間内に解けなかったので、家で解き直しました。
もっと精進します。

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" ) 
    // 以下略
  }
}
0
0
0

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?