-
Notifications
You must be signed in to change notification settings - Fork 1
/
05.go
77 lines (66 loc) · 1.12 KB
/
05.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package main
import (
"bufio"
"fmt"
"log"
"os"
"strings"
)
func isNice(word string) int {
vowels := 0
for _, v := range "aeiou" {
vowels += strings.Count(word, string(v))
}
if vowels < 3 {
return 0
}
doubles := 0
for i := 0; i < len(word)-1; i++ {
if word[i] == word[i+1] {
doubles++
}
}
if doubles == 0 {
return 0
}
for _, v := range []string{"ab", "cd", "pq", "xy"} {
if strings.Contains(word, v) {
return 0
}
}
return 1
}
func isReallyNice(word string) int {
repeats, duplicates := 0, 0
for i := 0; i < len(word)-2; i++ {
if word[i] == word[i+2] {
repeats++
}
if strings.Index(word[i+2:len(word)], word[i:i+2]) > -1 {
duplicates++
}
}
if repeats > 0 && duplicates > 0 {
return 1
}
return 0
}
func main() {
file, err := os.Open("input/05.txt")
if err != nil {
log.Fatal(err)
}
defer file.Close()
nice, reallyNice := 0, 0
scanner := bufio.NewScanner(file)
for scanner.Scan() {
text := scanner.Text()
nice += isNice(text)
reallyNice += isReallyNice(text)
}
if err := scanner.Err(); err != nil {
log.Fatal(err)
}
fmt.Println(nice)
fmt.Println(reallyNice)
}