Skip to content

Instantly share code, notes, and snippets.

@sungkmi
Created May 8, 2015 12:48
Show Gist options
  • Save sungkmi/8e1e0881ba67bb93eb53 to your computer and use it in GitHub Desktop.
Save sungkmi/8e1e0881ba67bb93eb53 to your computer and use it in GitHub Desktop.
object OminousOmino extends App {
def richardWin(x: Int, r: Int, c: Int): Boolean =
(r * c % x != 0) || {
val s = r min c
x match {
case 3 => s == 1
case 4 => s <= 2
case 5 => s <= 2 || (s, r max c) == (3, 5)
case 6 => s <= 3
case _ => x >= 7
}
}
def process(lineIn: Iterator[String])(lineOut: String => Unit) =
for (i <- 1 to lineIn.next().toInt) {
val Array(x, r, c) = lineIn.next().split(' ').map(_.toInt)
lineOut(s"Case #$i: ${if (richardWin(x, r, c)) "RICHARD" else "GABRIEL"}")
}
val filename = "D-large-practice"
val writer = new java.io.PrintWriter(filename + ".out")
try {
process(io.Source.fromFile(filename + ".in").getLines) { s =>
writer.println(s); writer.flush()
}
} finally {
writer.flush(); writer.close()
}
}
import org.scalatest._
import OminousOmino._
class OminousOminoTest extends FunSuite {
test("sample #1") {
assert(richardWin(2, 2, 2) === false)
}
test("sample #2") {
assert(richardWin(2, 1, 3) === true)
}
test("sample #3") {
assert(richardWin(4, 4, 1) === true)
}
test("sample #4") {
assert(richardWin(3, 2, 3) === false)
}
test("sample case") {
val input = """4
2 2 2
2 1 3
4 4 1
3 2 3""".lines
val expected = """Case #1: GABRIEL
Case #2: RICHARD
Case #3: RICHARD
Case #4: GABRIEL""".lines
lineComparison(input, expected)
}
test("full small case") {
val input = io.Source.fromFile("D-small-practice.in").getLines()
val expected = io.Source.fromFile("D-small-practice.out").getLines()
lineComparison(input, expected)
}
test("full large case") {
val input = io.Source.fromFile("D-large-practice.in").getLines()
val expected = io.Source.fromFile("D-large-practice.out").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, "Finished too fast.")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment