Skip to content

Instantly share code, notes, and snippets.

@theodoreLee
Created June 19, 2015 12:25
Show Gist options
  • Save theodoreLee/487a5849065c5507c22d to your computer and use it in GitHub Desktop.
Save theodoreLee/487a5849065c5507c22d to your computer and use it in GitHub Desktop.
import java.io.FileOutputStream
import scala.io.Source
object Battleship {
val INPUT = "A-large-practice.in"
val OUTPUT = INPUT.takeWhile(_ != '.') + ".out"
val isConsole = false
def main(args: Array[String]): Unit = {
val itr = Source.fromFile(INPUT).getLines()
val stream = if (isConsole) Console.out else new FileOutputStream(OUTPUT)
try {
Console.withOut(stream) {
val sets = itr.next().toInt
(1 to sets).foreach { set =>
val Array(r,c,w) = itr.next().split(' ').map(_.toInt)
println(f"Case #$set: ${minimumScore(r, c, w)}")
}
}
} finally {
stream.flush()
if (!isConsole) stream.close()
}
}
def minimumScore(R:Int, C:Int, W:Int) = {
val mod = if(C % W == 0) 1 else 0
R * (C / W) + W - mod
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment