Last active
August 29, 2015 14:11
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
object RopeIntranet extends App { | |
def isCrossed(x: Array[Int], y: Array[Int]): Boolean = (x(0) - y(0)) * (x(1) - y(1)) < 0 | |
def numberOfInterection(wires: IndexedSeq[Array[Int]]): Int = { | |
for { | |
i <- 0 until wires.size - 1 | |
j <- i + 1 until wires.size | |
} yield isCrossed(wires(i), wires(j)) | |
}.count(_ == true) | |
def process(lineIn: Iterator[String])(lineOut: String => Unit) = | |
for (i <- 1 to lineIn.next().toInt) { | |
val n = lineIn.next().toInt | |
val wires = Vector.fill(n) { lineIn.next() split ' ' map (_.toInt) } | |
lineOut(s"Case #$i: ${numberOfInterection(wires)}") | |
} | |
val writer = new java.io.PrintWriter("a.large.out") | |
try { | |
process(io.Source.fromFile("A-large-practice.in").getLines) { s => | |
writer.println(s) | |
writer.flush() | |
} | |
} finally { | |
writer.flush(); writer.close() | |
} | |
} |
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._ | |
import RopeIntranet._ | |
class RopeIntranetTest extends FunSuite { | |
test("isCrossed") { | |
assert(isCrossed(Array(1, 10), Array(5,5)) === true) | |
assert(isCrossed(Array(7, 7), Array(5,5)) === false) | |
} | |
test("sample #1") { | |
assert(numberOfInterection(Vector(Array(1,10), Array(5,5), Array(7,7))) === 2) | |
} | |
test("sample #2") { | |
assert(numberOfInterection(Vector(Array(1,1), Array(2,2))) === 0) | |
} | |
test("sample case") { | |
val input = """2 | |
3 | |
1 10 | |
5 5 | |
7 7 | |
2 | |
1 1 | |
2 2""".lines | |
val expected = """Case #1: 2 | |
Case #2: 0""".lines | |
lineComparison(input, expected) | |
} | |
test("full large case") { | |
val input = io.Source.fromFile("A-large-practice.in").getLines() | |
val expected = io.Source.fromFile("a.large.out.ref").getLines() | |
lineComparison(input, expected) | |
} | |
test("full small case") { | |
val input = io.Source.fromFile("A-small-practice.in").getLines() | |
val expected = io.Source.fromFile("a.small.out.ref").getLines() | |
lineComparison(input, expected) | |
} | |
def lineComparison(input: Iterator[String], expected: Iterator[String]) { | |
process(input) { s => | |
for (line <- s.lines) assert(line.trim === expected.next().trim) | |
} | |
assert(expected.hasNext === false) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment