Created
October 10, 2014 12:49
-
-
Save sungkmi/c3fa1467b3b42cdb5c29 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
object FreeCellStatistics extends App { | |
def isPossible(n: Long, pd: Long, pg: Long): Boolean = pg match { | |
case 0 if pd > 0 => false | |
case 100 if pd < 100 => false | |
case _ => | |
@annotation.tailrec | |
def gcd(a: Long, b: Long): Long = if (b == 0) a else gcd(b, a % b) | |
n >= 100 / gcd(pd, 100) | |
} | |
def process(lineIn: Iterator[String])(lineOut: String => Unit) = for (i <- 1 to lineIn.next().toInt) { | |
val Array(n, pd, pg) = lineIn.next() split ' ' map (_.toLong) | |
lineOut(s"Case #$i: ${if (isPossible(n, pd, pg)) "Possible" else "Broken"}") | |
} | |
val writer = new java.io.PrintWriter("a.large.out") | |
try { | |
process(io.Source.fromFile("A-large-practice.in").getLines)(writer.println) | |
} 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 FreeCellStatistics._ | |
class FreeCellStatisticsTest extends FunSuite { | |
test("sample #1") { | |
assert(isPossible(1, 100, 50) === true) | |
} | |
test("sample #2") { | |
assert(isPossible(10, 10, 100) === false) | |
} | |
test("sample #3") { | |
assert(isPossible(9, 80, 56) === true) | |
} | |
test("sample case") { | |
val input = """3 | |
1 100 50 | |
10 10 100 | |
9 80 56""".lines | |
val expected = """Case #1: Possible | |
Case #2: Broken | |
Case #3: Possible""".lines | |
process(input) { s => | |
for (line <- s.lines) | |
assert(line === expected.next()) | |
} | |
} | |
test("full small case") { | |
val input = io.Source.fromFile("A-small-practice.in").getLines() | |
val expected = io.Source.fromFile("a.small.out.ref").getLines() | |
process(input) { s => | |
for (line <- s.lines) | |
assert(line.trim === expected.next().trim) | |
} | |
} | |
test("full large case") { | |
val input = io.Source.fromFile("A-large-practice.in").getLines() | |
val expected = io.Source.fromFile("a.large.out.ref").getLines() | |
process(input) { s => | |
for (line <- s.lines) | |
assert(line.trim === expected.next().trim) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment