Use comparator as function

This commit is contained in:
Brian Buller 2017-12-04 06:54:33 -06:00
parent e37dda03a5
commit 7ac9e3e9b6
1 changed files with 13 additions and 24 deletions

View File

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