-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.go
77 lines (66 loc) · 1.12 KB
/
main.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"
"io"
"strings"
lib "github.com/teivah/advent-of-code"
)
func fs1(input io.Reader) int {
scanner := bufio.NewScanner(input)
two := 0
three := 0
for scanner.Scan() {
s := scanner.Text()
if letters(s, 2) {
two++
}
if letters(s, 3) {
three++
}
}
return two * three
}
func letters(s string, count int) bool {
set := make(map[rune]int)
for i := 0; i < len(s); i++ {
r := rune(s[i])
set[r]++
}
for _, v := range set {
if v == count {
return true
}
}
return false
}
func fs2(input io.Reader) string {
words := lib.ReaderToStrings(input)
set := make(map[string]int)
for _, s := range words {
for empty := 0; empty < len(s); empty++ {
sb := strings.Builder{}
for i := 0; i < empty; i++ {
sb.WriteByte(s[i])
}
sb.WriteRune('_')
for i := empty + 1; i < len(s); i++ {
sb.WriteByte(s[i])
}
set[sb.String()]++
}
}
for k, v := range set {
if v == 2 {
sb := strings.Builder{}
for i := 0; i < len(k); i++ {
r := rune(k[i])
if r == '_' {
continue
}
sb.WriteRune(r)
}
return sb.String()
}
}
return ""
}