Skip to content

Instantly share code, notes, and snippets.

@krikit
Last active August 29, 2015 14:11
Show Gist options
  • Save krikit/e71cdd5bbe93808c0dbf to your computer and use it in GitHub Desktop.
Save krikit/e71cdd5bbe93808c0dbf to your computer and use it in GitHub Desktop.
object RopeIntranet {
def main(args: Array[String]) {
val writer = new java.io.PrintWriter("a-large.out")
try {
process(io.Source.fromFile("A-large-practice.in").getLines)(writer.println)
} finally {
writer.flush()
writer.close()
}
}
def process(lineIn: Iterator[String])(lineOut: String => Unit) = {
for (i <- 1 to lineIn.next().toInt) {
val ropes = for (j <- 1 to lineIn.next().toInt) yield {
val Array(lhs, rhs) = lineIn.next().split(" ").map(_.toInt)
(lhs, rhs)
}
lineOut(s"Case #$i: ${countRopes(ropes.toList, 0)}")
}
}
def isCrossed(lhs: (Int, Int), rhs: (Int, Int)): Boolean =
(lhs._1 - rhs._1) * (lhs._2 - rhs._2) < 0
def countCrossing(rope: (Int, Int), ropes: List[(Int, Int)]): Int =
ropes.filter(isCrossed(_, rope)).length
@annotation.tailrec
def countRopes(ropes: List[(Int, Int)], count: Int): Int = {
if (ropes.length < 2) count
else {
val count1 = countCrossing(ropes.head, ropes.tail)
countRopes(ropes.tail, count + count1)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment