Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@ponkotuy
Last active August 12, 2016 15:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ponkotuy/24758fc99b35a153780e76c4f0bd0993 to your computer and use it in GitHub Desktop.
Save ponkotuy/24758fc99b35a153780e76c4f0bd0993 to your computer and use it in GitHub Desktop.
Part2 4.6
import scala.io.StdIn
object Main extends App {
val input = StdIn.readLine()
val heights = input.foldLeft(Seq(0)) { case (hs, x) =>
val add = x match {
case '\\' => hs.last - 1
case '/' => hs.last + 1
case '_' => hs.last
}
hs :+ add
}
def calcArea(heights: Seq[Int]): Int = {
val surface = math.min(heights.head, heights.last)
heights.filter(_ < surface).map(surface - _).sum
}
val builder = List.newBuilder[Int]
var fst = 0
val areas = heights.map { h =>
builder += h
if(fst <= h) {
val hs = builder.result
println(hs)
builder.clear
builder += h
fst = h
calcArea(hs)
} else 0
} :+ calcArea(builder.result)
println(areas.filter(_ != 0))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment