Skip to content

Instantly share code, notes, and snippets.

@krikit
Created March 27, 2015 13:19
Show Gist options
  • Save krikit/6c2c3f5a3e41115a9d53 to your computer and use it in GitHub Desktop.
Save krikit/6c2c3f5a3e41115a9d53 to your computer and use it in GitHub Desktop.
object AllYourBase extends App {
def uniq(code: String): String = {
def uniq_inner(codes: List[Char], seen: Set[Char], acc: String): String = {
codes match {
case Nil => acc
case head :: tail => seen.find(_ == head) match {
case None => uniq_inner(tail, seen ++ Set(head), acc + head)
case Some(_) => uniq_inner(tail, seen, acc)
}
}
}
uniq_inner(code.toList, Set(), "")
}
def base(code: String): Int = uniq(code).size match {
case 1 => 2
case b => b
}
def getMap(code: String): Map[Char, Int] = {
val uniq_code = uniq(code)
(for ((c, i) <- uniq_code.zipWithIndex) yield {
if (i == 0) (c, 1)
else if (i == 1) (c, 0)
else (c, i)
}).toMap
}
def transform(code: String): String = {
val transMap = getMap(code)
(for (c <- code) yield transMap(c)).mkString
}
def solve(code: String): Long = {
val trans = transform(code)
java.lang.Long.parseLong(trans, base(code))
}
//////////
// main //
//////////
if (args.length > 0) Console.setIn(new java.io.FileInputStream(args(0)))
if (args.length > 1) Console.setOut(new java.io.FileOutputStream(args(1)))
for (t <- 1 to readInt) {
println(s"Case #${t}: ${solve(readLine)}")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment