Created
June 8, 2011 23:33
-
-
Save daros/1015722 to your computer and use it in GitHub Desktop.
CodeBreaker kata
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
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