Skip to content

Instantly share code, notes, and snippets.

@myeesan
Last active August 29, 2015 14:23
Show Gist options
  • Save myeesan/a2b536fb22a3397b67a7 to your computer and use it in GitHub Desktop.
Save myeesan/a2b536fb22a3397b67a7 to your computer and use it in GitHub Desktop.
import java.io.PrintWriter
import scala.io.Source
class Battleship {
def lines = Source.fromFile("large.in").getLines.toList.tail
val writer = new PrintWriter("large.out")
def run: Unit = {
lines.map(solve).zipWithIndex.foreach({ case (r, i) => writer.println("Case #" + (i + 1) + ": " + r) })
writer.flush()
writer.close()
}
def solve(s: String): Long = {
val Array(r, c, w) = s.split(" ").map(_.toLong)
if (c % w == 0)
r * (c / w) + (w - 1)
else
r * (c / w) + w
}
}
import org.scalatest.FunSuite
class BattleshipTest extends FunSuite {
val battleship = new Battleship
test("1") {
assert("1 4 2" === battleship.lines.head)
}
test("2") {
assert(3 === battleship.solve("1 4 2"))
assert(7 === battleship.solve("1 7 7"))
assert(10 === battleship.solve("2 5 1"))
}
test("print") {
battleship.run;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment