Skip to content

Instantly share code, notes, and snippets.

@myeesan
Created December 19, 2014 13:20
Show Gist options
  • Save myeesan/4649eaed818311e94444 to your computer and use it in GitHub Desktop.
Save myeesan/4649eaed818311e94444 to your computer and use it in GitHub Desktop.
import scala.io.Source
import java.io.PrintWriter
object RopeIntranet {
implicit def strToInt(s: String) = s.toInt
implicit class StringWrapper(line: String) {
def toRope: Rope = line.split(" ") match {
case Array(l, r) => Rope(l, r)
}
}
val in = Source.fromFile("A-large-practice.in").getLines
val out = new PrintWriter("A-large-practice.out")
case class Rope(l: Int, r: Int) {
def isCross(another: Rope): Boolean = l > another.l ^ r > another.r
}
case class TestCase(ropes: List[Rope]) {
def solve: Int = loop(ropes)
private def loop(ropes: List[Rope]): Int = ropes match {
case Nil => 0
case x :: xs => xs.foldLeft(0)((acc, b) => if (x.isCross(b)) acc + 1 else acc) + loop(xs)
}
}
def main(args: Array[String]) {
val ls = for {
i <- 1 to in.next
val ropes = in.take(in.next).toList map (_.toRope)
val res = TestCase(ropes).solve
} yield s"Case #$i: ${res}"
ls.foreach(out.println)
out.flush
}
}
import org.scalatest.FunSuite
import org.junit.runner.RunWith
import org.scalatest.junit.JUnitRunner
import RopeIntranet._
@RunWith(classOf[JUnitRunner])
class RopeIntranetTest extends FunSuite{
test("x"){
val r1 = Rope(10, 20)
val r2 = Rope(20, 10)
assert(true === r1.isCross(r2))
assert(true === r2.isCross(r1))
}
test("x2"){
val r1 = Rope(10, 10)
val r2 = Rope(20, 20)
assert(false === r1.isCross(r2))
assert(false === r2.isCross(r1))
}
test("x3"){
val l = """1 10
5 5
7 7""".lines
val r = l.map(_.split(" ")).map{ case Array(l, r) => Rope(l, r)}.toList
val tc = TestCase(r)
assert(2 === tc.solve)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment