83 lines
1.4 KiB
Go
83 lines
1.4 KiB
Go
|
package main
|
||
|
|
||
|
import (
|
||
|
"fmt"
|
||
|
"io"
|
||
|
"log"
|
||
|
)
|
||
|
|
||
|
func main() {
|
||
|
var input []string
|
||
|
var readInp string
|
||
|
for {
|
||
|
_, err := fmt.Scan(&readInp)
|
||
|
if err != nil {
|
||
|
if err != io.EOF {
|
||
|
log.Fatal(err)
|
||
|
}
|
||
|
break
|
||
|
}
|
||
|
input = append(input, readInp)
|
||
|
}
|
||
|
|
||
|
var numNice int
|
||
|
for _, k := range input {
|
||
|
if isNicePt2(k) {
|
||
|
numNice++
|
||
|
}
|
||
|
}
|
||
|
fmt.Printf("There are %d nice strings (out of %d total)\n", numNice, len(input))
|
||
|
}
|
||
|
|
||
|
func isNicePt1(s string) bool {
|
||
|
var prev byte
|
||
|
var numVowels int
|
||
|
var hasDouble bool
|
||
|
for i := range s {
|
||
|
if s[i] == 'b' && prev == 'a' {
|
||
|
return false
|
||
|
} else if s[i] == 'd' && prev == 'c' {
|
||
|
return false
|
||
|
} else if s[i] == 'q' && prev == 'p' {
|
||
|
return false
|
||
|
} else if s[i] == 'y' && prev == 'x' {
|
||
|
return false
|
||
|
} else if s[i] == 'a' || s[i] == 'e' || s[i] == 'i' || s[i] == 'o' || s[i] == 'u' {
|
||
|
numVowels++
|
||
|
}
|
||
|
if prev == s[i] {
|
||
|
hasDouble = true
|
||
|
}
|
||
|
prev = s[i]
|
||
|
}
|
||
|
return (numVowels > 2 && hasDouble)
|
||
|
}
|
||
|
|
||
|
func isNicePt2(s string) bool {
|
||
|
var splitRepeat bool
|
||
|
var double bool
|
||
|
for i := range s {
|
||
|
if i > 0 {
|
||
|
double = double || hasDouble(s[i-1], s[i], s[i+1:])
|
||
|
}
|
||
|
if i > 1 {
|
||
|
splitRepeat = splitRepeat || (s[i-2] == s[i])
|
||
|
}
|
||
|
if double && splitRepeat {
|
||
|
return true
|
||
|
}
|
||
|
}
|
||
|
return false
|
||
|
}
|
||
|
|
||
|
func hasDouble(b1, b2 byte, s string) bool {
|
||
|
var prev byte
|
||
|
for i := range s {
|
||
|
if prev == b1 && s[i] == b2 {
|
||
|
return true
|
||
|
}
|
||
|
prev = s[i]
|
||
|
}
|
||
|
return false
|
||
|
}
|