Skip to content

Instantly share code, notes, and snippets.

@sungkmi
Created December 26, 2014 13:01
Show Gist options
  • Save sungkmi/e8b5ba1289f968a01e87 to your computer and use it in GitHub Desktop.
Save sungkmi/e8b5ba1289f968a01e87 to your computer and use it in GitHub Desktop.
object FairWarning extends App {
def minSec(seconds: Seq[BigInt]): BigInt = {
val gcd = seconds.map(_ - seconds.min).reduce(_ gcd _)
val r = seconds.head % gcd
if (r == 0) 0 else gcd - r
}
def process(lineIn: Iterator[String])(lineOut: String => Unit) =
for (i <- 1 to lineIn.next().toInt) {
val line = lineIn.next()
val seconds = (line split ' ').tail.map(BigInt(_))
lineOut(s"Case #$i: ${minSec(seconds)}")
}
val writer = new java.io.PrintWriter("b.large.out")
try {
process(io.Source.fromFile("B-large-practice.in").getLines) { s =>
writer.println(s)
writer.flush()
}
} finally {
writer.flush(); writer.close()
}
}
import org.scalatest._
import FairWarning._
class FairWarningTest extends FunSuite {
test("sample #1") {
assert(minSec(Seq(26000000, 11000000, 6000000)) === 4000000)
}
test("sample #2") {
assert(minSec(Seq(1, 10, 11)) === 0)
}
test("sample #3") {
assert(minSec(Seq(BigInt("800000000000000000001"), BigInt("900000000000000000001"))) === BigInt("99999999999999999999"))
}
test("sample case") {
val input = """3
3 26000000 11000000 6000000
3 1 10 11
2 800000000000000000001 900000000000000000001""".lines
val expected = """Case #1: 4000000
Case #2: 0
Case #3: 99999999999999999999""".lines
lineComparison(input, expected)
}
test("full small case") {
val input = io.Source.fromFile("B-small-practice.in").getLines()
val expected = io.Source.fromFile("b.small.out.ref").getLines()
lineComparison(input, expected)
}
test("full large case") {
val input = io.Source.fromFile("B-large-practice.in").getLines()
val expected = io.Source.fromFile("b.large.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