Skip to content

Instantly share code, notes, and snippets.

@krikit
Created September 4, 2015 11:51
Show Gist options
  • Save krikit/5e7ca840a5477ff8057c to your computer and use it in GitHub Desktop.
Save krikit/5e7ca840a5477ff8057c to your computer and use it in GitHub Desktop.
import java.io.{File, PrintWriter}
object IoError extends App {
def slice(stream: String): List[String] = {
(for (i <- 0 until (stream.length / 8)) yield {
stream.substring(i * 8, (i + 1) * 8)
}).toList
}
def toNum(inputStr: String): Int = {
def toNumInner(input: List[Char], acc: Int): Int = input match {
case Nil => acc
case head :: tail => head match {
case 'I' => toNumInner(tail, (acc << 1) + 1)
case 'O' => toNumInner(tail, acc << 1)
}
}
toNumInner(inputStr.toList, 0)
}
def toStr(stream: String): String = {
slice(stream).map(toNum(_)).map(_.toChar).mkString("")
}
val lines = io.Source.fromFile("A-small-practice.in").getLines()
val out = new PrintWriter(new File("A-small-practice.out" ))
for (i <- 1 to lines.next.toInt) {
lines.next
out.println(s"Case #${i}: ${toStr(lines.next)}")
}
out.close
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment