Last active
August 12, 2016 15:30
-
-
Save ponkotuy/24758fc99b35a153780e76c4f0bd0993 to your computer and use it in GitHub Desktop.
Part2 4.6
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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