Created
September 19, 2014 13:13
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 SnapperChain extends App { | |
def state(n: Int, k: Int): Boolean = | |
BigInt(k + 1) % (BigInt(2) pow n) == 0 | |
def process(lineIn: Iterator[String])(lineOut: String => Unit) = { | |
for (i <- 1 to lineIn.next().toInt) { | |
val Array(n, k) = lineIn.next() split ' ' map (_.toInt) | |
lineOut(s"Case #$i: ${if (state(n, k)) "ON" else "OFF"}") | |
} | |
} | |
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 SnapperChain._ | |
class SnapperChainTest extends FunSuite { | |
test("sample #1") { | |
assert(state(1,0) === false) | |
} | |
test("sample #2") { | |
assert(state(1,1) === true) | |
} | |
test("sample #4") { | |
assert(state(4,47) === true) | |
} | |
test("sample case") { | |
val input = """4 | |
1 0 | |
1 1 | |
4 0 | |
4 47""".lines | |
val expected = """Case #1: OFF | |
Case #2: ON | |
Case #3: OFF | |
Case #4: ON""".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