-
-
Save myeesan/a2b536fb22a3397b67a7 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
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 | |
} | |
} |
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.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