Skip to content

Instantly share code, notes, and snippets.

@jim-collins
Created September 17, 2015 21:24
Show Gist options
  • Save jim-collins/b7afa23f0488cfb38f11 to your computer and use it in GitHub Desktop.
Save jim-collins/b7afa23f0488cfb38f11 to your computer and use it in GitHub Desktop.
package dojo
/**
* Created with IntelliJ IDEA.
* User: Jim Collins
* Date: 17/09/2015
*/
trait Lcd {
val contents: List[String]
override def toString = contents mkString "\n"
}
object Lcd {
def op(a: Lcd, b: Lcd): Lcd = new Lcd {
override val contents: List[String] =
(a.contents zip b.contents).map((f: (String,String)) => f._1 + f._2)
}
def zero: Lcd = new Lcd {
override val contents = List("", "", "")
}
def concatenate(as: List[Lcd]): Lcd = as.foldLeft(zero)(op)
val Zero = new Lcd { override val contents: List[String] = List("._.", "|.|", "|_|")}
val One = new Lcd {override val contents: List[String] = List("...", "..|", "..|")}
val Two = new Lcd {override val contents: List[String] = List("._.", "._|", "|_.")}
val Three = new Lcd {override val contents: List[String] = List("._.", "._|", "._|")}
val Four = new Lcd {override val contents: List[String] = List("...", "|_|", "..|")}
val Five = new Lcd {override val contents: List[String] = List("._.", "|_.", "._|")}
val Six = new Lcd {override val contents: List[String] = List("._.", "|_.", "|_|")}
val Severn = new Lcd {override val contents: List[String] = List("._.", "..|", "..|")}
val Eight = new Lcd {override val contents: List[String] = List("._.", "|_|", "|_|")}
val Nine = new Lcd {override val contents: List[String] = List("._.", "|_|", "..|")}
val mappings = Map(0 -> Zero, 1 -> One, 2 -> Two, 3 -> Three, 4 -> Four, 5 -> Five, 6 -> Six, 7 -> Severn, 8 -> Eight, 9 -> Nine)
}
object Main{
def main (args: Array[String]) {
import Lcd._
println(concatenate(1234567890.toString.map(_.asDigit).map(mappings(_)).toList))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment