Skip to content

Instantly share code, notes, and snippets.

@waynejo
Last active August 29, 2015 14:25
Show Gist options
  • Save waynejo/8a59f42e2a50884160cd to your computer and use it in GitHub Desktop.
Save waynejo/8a59f42e2a50884160cd to your computer and use it in GitHub Desktop.
package Main
import java.io.{FileOutputStream, FileInputStream}
import scala.annotation.tailrec
import scala.io.StdIn
object Main extends App {
Console.setIn(new FileInputStream("example.in"))
Console.setIn(new FileInputStream("A-large-practice.in"))
Console.setOut(new FileOutputStream("A-large-practice.out"))
case class Arrow(x: Int, y: Int, direction: Char)
def solve(arrows: IndexedSeq[Arrow]): Option[Int] = {
def hasPointOnOtherDirection(arrow: Arrow): Boolean =
"<>^v".filter(x => x != arrow.direction).exists(x => hasPoint(arrow, x))
def hasPoint(arrow: Arrow, direction: Char): Boolean = {
direction match {
case '<' => arrows.exists(x => x.x < arrow.x && x.y == arrow.y)
case '>' => arrows.exists(x => x.x > arrow.x && x.y == arrow.y)
case '^' => arrows.exists(x => x.x == arrow.x && x.y < arrow.y)
case 'v' => arrows.exists(x => x.x == arrow.x && x.y > arrow.y)
}
}
(Option(0) /: arrows)((acc: Option[Int], arrow: Arrow) => acc match {
case Some(sums: Int) if hasPoint(arrow, arrow.direction) => Some(sums)
case Some(sums: Int) if hasPointOnOtherDirection(arrow) => Some(sums + 1)
case _ => None
})
}
val cases = StdIn.readLine().toInt
(1 to cases) foreach { n =>
val Array(r, _) = StdIn.readLine().split(" ").map(_.toInt)
val arrows = for (y <- 0 until r) yield StdIn.readLine().zipWithIndex.map(x => Arrow(x._2, y, x._1))
println(s"Case #$n: ${
solve(arrows.flatten.filter(x => x.direction != '.')) match {
case Some(x) => x
case None => "IMPOSSIBLE"
}
}")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment