Created
April 24, 2020 15:52
-
-
Save chaotic3quilibrium/abc06dc0a9196254850315bc500d2324 to your computer and use it in GitHub Desktop.
ScalaBoi: A John Conway Puzzler - Part 1 of 2 - Oops, That Didn't Work! Why?!
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
//C1 - Initial Scala solution attempt | |
val lettersToDigitsPrefixSize = 3 | |
val letterToDigits = | |
List( | |
'a' -> List(1, 3, 7, 9), | |
'b' -> List(2, 4, 6, 8), | |
'c' -> List(1, 3, 7, 9), | |
'd' -> List(2, 4, 6, 8), | |
'e' -> List(5), | |
'f' -> List(2, 4, 6, 8), | |
'g' -> List(1, 3, 7, 9), | |
'h' -> List(2, 4, 6, 8), | |
'i' -> List(1, 3, 7, 9), | |
'j' -> List(0) | |
) | |
val digitsByLetter = | |
letterToDigits.toMap | |
val letters = | |
letterToDigits.map(_._1) | |
def generateCandidates = { | |
def recursive(remaining: List[(Char, List[Int])] = letterToDigits.take(lettersToDigitsPrefixSize), accumulator: List[Int] = List(0)): List[Int] = | |
if (remaining.isEmpty || accumulator.isEmpty) | |
accumulator | |
else { | |
val (letter, digits): (Char, List[Int]) = remaining.head | |
val indexOfLetter: Int = letters.indexOf(letter) + 1 | |
val accumulatorNew: List[Int] = | |
for { | |
prior <- accumulator | |
priorAsSetOfDigits = prior.toString.toSet[Char].map(_.asDigit) | |
digit <- digits.filter(!priorAsSetOfDigits.contains(_)) | |
value = (prior * 10) + digit | |
if (value % indexOfLetter) == 0 | |
} yield value | |
recursive(remaining.tail, accumulatorNew) | |
} | |
recursive() | |
} | |
println(generateCandidates) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment