Created
April 22, 2016 12:34
-
-
Save lala7573/b64ba0789c0b568e8ba1fa57e5f3aaec 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
package codejam | |
import java.io.{PrintStream, File} | |
import scala.annotation.tailrec | |
import scala.io.Source | |
object CountingSheep { | |
def solve(sheepCount : Int) : String = { | |
@tailrec | |
def inner(times : Int, set : Set[Char]) : Int = { | |
if (sheepCount == 0 || set.size == 10) return sheepCount * (times - 1) | |
inner(times + 1, (sheepCount * times).toString.toSet ++ set) | |
} | |
inner(1, Set()) match { | |
case 0 => "INSOMNIA" | |
case x => x.toString | |
} | |
} | |
def process(iter: Iterator[String])(pr: String => Unit) { | |
for (count <- 1 to iter.next().toInt) yield { | |
val sheepCount = iter.next().toInt | |
pr(s"Case #$count: ${solve(sheepCount)}") | |
} | |
} | |
def main(args: Array[String]) { | |
val in = Source.fromFile(new File("A-large-practice.in")) | |
val out = new PrintStream(new File("A-large-practice.out")) | |
try { | |
process(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