Created
June 13, 2014 14:21
-
-
Save myeesan/2d1d1ba292254ff073fa to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import scala.io.Source | |
import java.io.PrintWriter | |
object TheRepeater { | |
val in = Source.fromFile("large.in").getLines | |
val printer = new PrintWriter("large.out") | |
case class Game(ls: List[String]) { | |
def isValid = { | |
val res = ls.map(merge(_).map(_._1)) | |
res.toSet.size == 1 | |
} | |
def minNum(dist: List[Int]): Int = { | |
val mins = for { | |
i <- 0 until dist.length | |
val target = dist(i) | |
val res = dist.map(n => Math.abs(n - target)).sum | |
} yield res | |
mins.min | |
} | |
def solve = { | |
if (isValid) { | |
val res = ls.map(merge(_)) | |
val res2 = for { | |
i <- 0 until res(0).length | |
val ns = res.map(_(i)._2) | |
} yield minNum(ns) | |
res2.sum | |
} else "Fegla Won" | |
} | |
private def merge(line: String): List[(String, Int)] = { | |
val result: List[(String, Int)] = Nil | |
line.map(_.toString).foldLeft(result) { (acc, x) => | |
if (acc != Nil && acc.last._1 == x) | |
acc.init :+ (acc.last._1, acc.last._2 + 1) | |
else | |
acc :+ (x.toString, 1) | |
} | |
} | |
} | |
def main(args: Array[String]) { | |
val games = for { | |
i <- 1 to in.next.toInt | |
val game = Game(in.take(in.next.toInt).toList) | |
} yield { | |
printer.println(s"Case #$i: ${game.solve}") | |
} | |
printer.flush | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment