Skip to content

Instantly share code, notes, and snippets.

@lala7573
Created April 22, 2016 12:34
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lala7573/b64ba0789c0b568e8ba1fa57e5f3aaec to your computer and use it in GitHub Desktop.
Save lala7573/b64ba0789c0b568e8ba1fa57e5f3aaec to your computer and use it in GitHub Desktop.
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