Last active
June 3, 2018 17:54
-
-
Save supcik/3acb6b12f2b85cbb9d026c1b154e0789 to your computer and use it in GitHub Desktop.
recursive 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
func (v verbalSum) recursiveSolve( | |
letters []rune, digits []int, mapping map[rune]int, | |
) (combinations int, solutions int) { | |
if len(letters) == 0 { | |
if v.isValidSolution(mapping) { | |
fmt.Printf("Found %v\n", v.solutionString(mapping)) | |
return 1, 1 // one solution tried, one solution found | |
} | |
return 1, 0 // one solution tried, zero solution found | |
} | |
letter := letters[0] // take the first letter | |
combinations, solutions = 0, 0 | |
for i, digit := range digits { // try all digits for the letter "letter" | |
mapping[letter] = digit | |
c, s := v.recursiveSolve(letters[1:], withoutItem(digits, i), mapping) | |
combinations += c | |
solutions += s | |
} | |
delete(mapping, letter) | |
return | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment