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 = {
@annotation.tailrec
def minOps0(armin: Int, motes: List[Int], limit: Int, count: Int = 0): Int =
if (motes.isEmpty) count
else if (count >= limit) limit
else {
val (nextArmin, nextMotes, nextLimit, nextCount) =
if (armin > motes.head)
(armin + motes.head, motes.tail, limit, count)
else
(armin + armin - 1, motes, math.min(limit, count + motes.size), count + 1)
minOps0(nextArmin, nextMotes, nextLimit, nextCount)
}
minOps0(armin, motes.sorted.toList, motes.size)
}
def process(lineIn: Iterator[String])(lineOut: String => Unit) = {
def nextInts() = lineIn.next() split ' ' map (_.toInt)
for (i <- 1 to lineIn.next().toInt) {
val Array(a, n) = nextInts()
val motes = nextInts()
lineOut(s"Case #$i: ${minOps(a, motes)}")
}
}
val writer = new java.io.PrintWriter("a.large.out")
try {
process(io.Source.fromFile("A-large-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