adventOfCode/helpers/combinations.go
AKP fb0872daa6
I have no idea what this is for but it was here
Signed-off-by: AKP <tom@tdpain.net>
2021-10-19 18:26:50 +01:00

26 lines
553 B
Go

package helpers
// The following two functions were taken from https://stackoverflow.com/a/19249957
func GenerateCombinations(alphabet string, length int) <-chan string {
c := make(chan string)
go func(c chan string) {
defer close(c)
addLetter(c, "", alphabet, length)
}(c)
return c
}
func addLetter(c chan string, combo string, alphabet string, length int) {
if length <= 0 {
return
}
var newCombo string
for _, ch := range alphabet {
newCombo = combo + string(ch)
c <- newCombo
addLetter(c, newCombo, alphabet, length-1)
}
}