/
main.go
87 lines (64 loc) · 1.07 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
78
79
80
81
82
83
84
85
86
87
package main
import (
"encoding/csv"
"fmt"
"io"
"os"
"path"
"strconv"
"strings"
)
type thingy struct {
min, max int
char uint8
pass string
}
func parse(r io.Reader) {
csvReader := csv.NewReader(r)
csvReader.Comma = ':'
rows, err := csvReader.ReadAll()
if err != nil {
panic(err)
}
matching := 0
for _, row := range rows {
t := thingy{}
first := row[0]
parts := strings.Split(first, " ")
amount := parts[0]
nums := strings.Split(amount, "-")
min, err := strconv.Atoi(nums[0])
if err != nil {
panic(err)
}
t.min = min
max, err := strconv.Atoi(nums[1])
if err != nil {
panic(err)
}
t.max = max
t.char = parts[1][0]
t.pass = row[1]
found := count(t.char, t.pass)
if found >= t.min && found <= t.max {
matching++
}
}
fmt.Println(matching)
}
func count(char uint8, pass string) int {
num := 0
for i := 0; i < len(pass); i++ {
if pass[i] == char {
num++
}
}
return num
}
func main() {
input, err := os.Open(path.Join("2020", "2", "input.txt"))
if err != nil {
panic(err)
}
parse(input)
}