Skip to content

Instantly share code, notes, and snippets.

@krikit
Last active August 29, 2015 14:00
Show Gist options
  • Save krikit/11104372 to your computer and use it in GitHub Desktop.
Save krikit/11104372 to your computer and use it in GitHub Desktop.
Google Code Jam 2014 Qualification Round A Magic Trick
object magic_trick extends App {
def solve(first_answer: Int, first_cards: List[List[Int]], second_answer: Int, second_cards: List[List[Int]]): String = {
val first_choice = first_cards(first_answer - 1)
val second_choice = second_cards(second_answer - 1)
val answer = (first_choice.toSet & second_choice.toSet).toList
answer match {
case head :: Nil => head.toString
case Nil => "Volunteer cheated!"
case head :: tail => "Bad magician!"
}
}
//////////
// main //
//////////
if (args.length > 0) Console.setIn(new java.io.FileInputStream(args(0)))
if (args.length > 1) Console.setOut(new java.io.FileOutputStream(args(1)))
for (t <- 1 to readInt) {
val first_answer = readInt
val first_cards = for (row <- 1 to 4) yield readLine.split(" ").map(_.toInt).toList
val second_answer = readInt
val second_cards = for (row <- 1 to 4) yield readLine.split(" ").map(_.toInt).toList
println("Case #" + t + ": " + solve(first_answer, first_cards.toList, second_answer, second_cards.toList))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment