Use comparator as function

This commit is contained in:
Brian Buller 2017-12-04 06:54:33 -06:00
parent e37dda03a5
commit 7ac9e3e9b6

View File

@ -13,15 +13,14 @@ func main() {
part2(inp)
}
func part1(inp []string) {
fmt.Println("== Part 1 ==")
func findValid(inp []string, cmp func(string, string) bool) {
cnt := 0
for _, v := range inp {
wrds := strings.Split(v, " ")
isGood := true
for i := range wrds {
for j := i + 1; j < len(wrds); j++ {
if wrds[i] == wrds[j] {
if cmp(wrds[i], wrds[j]) {
isGood = false
}
if !isGood {
@ -39,30 +38,20 @@ func part1(inp []string) {
fmt.Println("Valid Passphrases:", cnt)
}
func part1(inp []string) {
fmt.Println("== Part 1 ==")
cmp := func(wrd1, wrd2 string) bool {
return wrd1 == wrd2
}
findValid(inp, cmp)
}
func part2(inp []string) {
fmt.Println("== Part 2 ==")
cnt := 0
for _, v := range inp {
wrds := strings.Split(v, " ")
isGood := true
for i := range wrds {
for j := i + 1; j < len(wrds); j++ {
if isAnagram(wrds[i], wrds[j]) {
isGood = false
cmp := func(wrd1, wrd2 string) bool {
return isAnagram(wrd1, wrd2)
}
if !isGood {
break
}
}
if !isGood {
break
}
}
if isGood {
cnt++
}
}
fmt.Println("Valid Passphrases:", cnt)
findValid(inp, cmp)
}
func isAnagram(wrd1, wrd2 string) bool {