Skip to content

Instantly share code, notes, and snippets.

@sungkmi
Last active August 29, 2015 14:19
Show Gist options
  • Save sungkmi/105d5c69c90f05ed27a5 to your computer and use it in GitHub Desktop.
Save sungkmi/105d5c69c90f05ed27a5 to your computer and use it in GitHub Desktop.
object InfiniteHouseOfPancakes extends App {
def findMin(plates: Seq[Int]): Int =
(for (max <- 1 to plates.max) yield {
for (plate <- plates) yield (plate-1)/ max
}.sum + max).min
def process(lineIn: Iterator[String])(lineOut: String => Unit) =
for (i <- 1 to lineIn.next().toInt) {
val d = lineIn.next().toInt
val plates = lineIn.next().split(' ').map(_.toInt)
lineOut(s"Case #$i: ${findMin(plates)}")
}
val filename = "B-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()
}
}
import org.scalatest._
import InfiniteHouseOfPancakes._
class InfiniteHouseOfPancakesTest extends FunSuite {
test("sample #1") {
assert(findMin(Seq(3)) === 3)
}
test("sample #2") {
assert(findMin(Seq(1,2,1,2)) === 2)
}
test("sample #3") {
assert(findMin(Seq(4)) === 3)
}
test("sample case") {
val input = """3
1
3
4
1 2 1 2
1
4""".lines
val expected = """Case #1: 3
Case #2: 2
Case #3: 3""".lines
lineComparison(input, expected)
}
test("full small case") {
val input = io.Source.fromFile("B-small-practice.in").getLines()
val expected = io.Source.fromFile("B-small-practice.out").getLines()
lineComparison(input, expected)
}
test("full large case") {
val input = io.Source.fromFile("B-large-practice.in").getLines()
val expected = io.Source.fromFile("B-large-practice.out").getLines()
lineComparison(input, expected)
}
def lineComparison(input: Iterator[String], expected: Iterator[String]) {
process(input) { s =>
for (line <- s.lines) assert(line.trim === expected.next().trim)
}
assert(expected.hasNext === false, "Finished too fast.")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment