Skip to content

Instantly share code, notes, and snippets.

@sungkmi
Last active August 29, 2015 14:06
Show Gist options
  • Save sungkmi/58044b985b86ec091b21 to your computer and use it in GitHub Desktop.
Save sungkmi/58044b985b86ec091b21 to your computer and use it in GitHub Desktop.
object Osmos extends App {
def minOps(armin: Int, motes: Seq[Int]): Int = {
def minOps0(armin: Int, motes: List[Int], upperLimit: Int, acc: Int = 0): Int = motes match {
case x :: Nil if armin > x => acc + 0
case x :: Nil if armin <= x => acc + 1
case x :: xs if armin > x => minOps0(armin + x, xs, xs.size, acc)
case x :: xs if armin <= x =>
if (acc < upperLimit) minOps0(armin + armin - 1, motes, upperLimit, acc+1)
else upperLimit
}
minOps0(armin, motes.sorted.toList, motes.size)
}
def process(lineIn: Iterator[String])(lineOut: String => Unit) = {
for (i <- 1 to lineIn.next().toInt) {
val Array(a, n) = lineIn.next().split(' ').map(_.toInt)
val motes = lineIn.next().split(' ').map(_.toInt)
lineOut(s"Case #$i: ${minOps(a, motes)}")
}
}
val writer = new java.io.PrintWriter("a.small.out")
try {
process(io.Source.fromFile("A-small-practice.in").getLines)(writer.println)
} finally {
writer.flush(); writer.close()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment