-
-
Save myeesan/4649eaed818311e94444 to your computer and use it in GitHub Desktop.
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.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 | |
} | |
} |
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 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