Skip to content

Instantly share code, notes, and snippets.

@sungkmi
Created June 6, 2014 13:13
Show Gist options
  • Save sungkmi/7445b5daa95f2a82e26a to your computer and use it in GitHub Desktop.
Save sungkmi/7445b5daa95f2a82e26a to your computer and use it in GitHub Desktop.
object ChargingChaos extends App {
def flippedSwitch(outlets: Set[BigInt], devices: Set[BigInt]): Option[Int] = {
val list = outlets.toList map { outlet =>
devices.map(outlet ^ _)
}
val result = (list.head /: list.tail)(_ & _)
if (result.isEmpty) None else Some(result.map(_.bitCount).min)
}
implicit def strToBigIntSet(s: String): Set[BigInt] = s.split(' ').map(BigInt(_, 2)).toSet
def process(iter: Iterator[String])(pr: String => Unit) =
for (i <- 1 to iter.next().toInt) yield {
val Array(n, l) = iter.next() split ' '
pr(s"Case #$i: ${flippedSwitch(iter.next(), iter.next()).getOrElse("NOT POSSIBLE")}")
}
import java.io._
val out = new PrintStream(new File("a.out"))
try {
process(io.Source.fromFile("A-large-practice.in").getLines) { s: String =>
out.println(s)
}
} finally {
out.flush; out.close
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment