Skip to content

Instantly share code, notes, and snippets.

@daros
Created June 8, 2011 23:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save daros/1015722 to your computer and use it in GitHub Desktop.
Save daros/1015722 to your computer and use it in GitHub Desktop.
CodeBreaker kata
package CodeBreaker
import collection.immutable.List._
import annotation.tailrec
class Marker(val secretList: List[String]) {
def guess(guessList: List[String]) = {
val zipped = guessList zip secretList
val possible = zipped.filter{case (a,b) => a != b}.unzip
def correct = {
zipped.filter{case (a,b) => a == b}.map(_ => "p")
}
@tailrec
def colorCorrect(ms: List[String], guessRest: List[String], secretRest: List[String]): List[String] = {
guessRest match {
case g :: gs if secretRest.contains(g) => {
val s1 = secretRest.splitAt(secretRest.indexOf(g))
colorCorrect("m" :: ms, s1._1 ++ s1._2.tail, gs)
}
case _ :: gs => colorCorrect(ms, secretRest, gs)
case _ => ms
}
}
(correct ++ colorCorrect(List(), possible._1, possible._2)).mkString("[", " ", "]")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment