-
-
Save krikit/5e7ca840a5477ff8057c 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
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