-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
81 lines (62 loc) · 1.06 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
package main
import (
"bufio"
"fmt"
"io"
"os"
"path"
)
func input() *os.File {
input, err := os.Open(path.Join("2020", "6", "input.txt"))
if err != nil {
panic(err)
}
return input
}
type group struct {
answers map[byte]int
people int
}
func parse(r io.Reader) []group {
var groups []group
currentGroup := group{
answers: make(map[byte]int),
}
finishGroup := func() {
groups = append(groups, currentGroup)
currentGroup = group{
answers: make(map[byte]int),
}
}
scanner := bufio.NewScanner(r)
for scanner.Scan() {
row := scanner.Bytes()
if len(row) == 0 {
finishGroup()
continue
}
currentGroup.people += 1
for i := 0; i < len(row); i++ {
currentGroup.answers[row[i]] += 1
}
}
finishGroup()
if scanner.Err() != nil {
panic(scanner.Err())
}
return groups
}
func solve(groups []group) int {
total := 0
for _, group := range groups {
for _, numAnswers := range group.answers {
if numAnswers == group.people {
total += 1
}
}
}
return total
}
func main() {
fmt.Println(solve(parse(input())))
}