Skip to content

Instantly share code, notes, and snippets.

@krikit
Created June 19, 2015 12:25
Show Gist options
  • Save krikit/e0d8b9c471f19c9309dc to your computer and use it in GitHub Desktop.
Save krikit/e0d8b9c471f19c9309dc to your computer and use it in GitHub Desktop.
object Brattleship extends App {
def solve(r: Int, c: Int, w: Int): Int = {
val shotPerLine = c / w
val shotForAllLine = r * shotPerLine
val shotForSink = w - 1 + (if (c % w == 0) 0 else 1)
shotForAllLine + shotForSink
}
def process(lineIn: Iterator[String])(lineOut: String => Unit) =
for (i <- 1 to lineIn.next().toInt) {
val Array(r, c, w) = lineIn.next().split(" ").map(_.toInt)
lineOut(s"Case #$i: ${solve(r, c, w)}")
}
val filename = "A-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()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment