Created
June 6, 2014 13:13
-
-
Save sungkmi/7445b5daa95f2a82e26a 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
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