Last active
August 29, 2015 14:04
-
-
Save jb77/da3b4b5bd7478500552e to your computer and use it in GitHub Desktop.
Recursive Puzzle Solver
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
/* | |
http://richardwiseman.wordpress.com/2014/08/04/answer-to-the-friday-puzzle-269/ | |
OK, onto the puzzle. Can you create a 10-digit number, | |
where the first digit is how many zeros in the number, | |
the second digit is how many 1s in the number etc | |
until the tenth digit which is how many 9s in the number. | |
*/ | |
def selfReferential(number:String) : Boolean = { | |
val numsWithIdx=number.zipWithIndex | |
numsWithIdx.forall( nIdx => number.count(_.asDigit==nIdx._2)==nIdx._1.asDigit) | |
} //> selfReferential: (number: String)Boolean | |
val seed="9000000000" //> seed : String = 9000000000 | |
def correct(s:String):String = { | |
if(selfReferential(s)) s | |
else { | |
val counts=0.to(9).map(idx=>s.count(_.asDigit==idx)) | |
println(counts.mkString) | |
val newCandidate=counts.mkString | |
correct(newCandidate) | |
} | |
} //> correct: (s: String)String | |
correct(seed) //> 9000000001 | |
//| 8100000001 | |
//| 7200000010 | |
//| 7110000100 | |
//| 6300000100 | |
//| 7101001000 | |
//| 6300000100 | |
//| 7101001000 | |
//| 6300000100 | |
//| 7101001000 | |
//| 6300000100 | |
// and so on! |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment