Skip to content

Instantly share code, notes, and snippets.

@ponkotuy ponkotuy/DiagramArea.scala
Last active Aug 12, 2016

Embed
What would you like to do?
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
You can’t perform that action at this time.