Skip to content

Instantly share code, notes, and snippets.

@rbobillot
Created June 20, 2016 21:36
Show Gist options
  • Save rbobillot/53fa3ad89c290baacca04063e316cb64 to your computer and use it in GitHub Desktop.
Save rbobillot/53fa3ad89c290baacca04063e316cb64 to your computer and use it in GitHub Desktop.
object Scramble {
val moves = Array("Up", "Down", "Left", "Right", "Front", "Back").map(_.head)
val orien = Array("2" -> 1, "'" -> 2, "" -> 3).flatMap{case (rot, weight) => 1 to weight map (_ => rot)}
val rands = orien.flatMap(x => moves.map(_ + x))
def isNum(num:String) = num forall ('0' to '9' contains _)
def getRandElemFromArray[T](arr:Array[T]) = arr( util.Random.nextInt(arr.size) )
def generate(size:Int) = 1 to size map (_ => getRandElemFromArray(rands)) mkString " "
def scramble(size:String) = if (false == isNum(size)) "Cannot Scramble" else generate(size.toInt)
def main(av:Array[String]):Unit = av.size match {
case 0 => println("usage: ./scramble <size>")
case _ => println( scramble(av.head) )
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment