Skip to content

Instantly share code, notes, and snippets.

@waynejo
Last active August 29, 2015 14:02
Show Gist options
  • Save waynejo/b2f1275f8605d25bcde6 to your computer and use it in GitHub Desktop.
Save waynejo/b2f1275f8605d25bcde6 to your computer and use it in GitHub Desktop.
Deceitful War
import scala.io.Source
import scala.annotation.tailrec
object Main {
def findGreaterThan(kens: List[Double], value: Double): Double = {
kens.filter(_ > value).last
}
def war(naomies: List[Double], kens: List[Double]): Int = {
if (naomies.isEmpty) {
0
} else {
if (naomies.head > kens.head) {
1 + war(naomies.tail, kens.init)
} else {
war(naomies.tail, kens diff List(findGreaterThan(kens, naomies.head)))
}
}
}
def deceitful(naomies: List[Double], kens: List[Double]): Int = {
kens.length - war(kens, naomies)
}
def main(args: Array[String]) {
def parseBoxLine(lineString: String): List[Double] = lineString.split(" ").map(_.toDouble).toList.sorted.reverse
val lines = Source.fromFile("input.txt").getLines()
val caseNum = lines.next().toInt
1 to caseNum foreach { caseIndex =>
{
lines.next()
val naomies = parseBoxLine(lines.next())
val kens = parseBoxLine(lines.next())
println(s"Case #${caseIndex}: ${deceitful(naomies, kens)} ${war(naomies, kens)}")
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment