-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathd06.go
More file actions
59 lines (46 loc) · 1.25 KB
/
d06.go
File metadata and controls
59 lines (46 loc) · 1.25 KB
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
package d06
import (
"strconv"
"strings"
"com.github/augustoccesar/adventofcode/golang/structure"
)
type Day06 struct{}
func (d *Day06) Year() int { return 2020 }
func (d *Day06) Day() int { return 6 }
func (d *Day06) PartOne() string {
total := 0
groups := strings.Split(structure.ReadDefaultInput(d), "\n\n")
for _, g := range groups {
groupSet := map[rune]bool{}
group := strings.Replace(g, "\n", "", -1)
for _, item := range group {
groupSet[item] = true
}
total += len(groupSet)
}
return strconv.Itoa(total)
}
func (d *Day06) PartTwo() string {
total := 0
groups := strings.Split(structure.ReadDefaultInput(d), "\n\n")
for _, g := range groups {
nInGroup := len(strings.Split(g, "\n")) // People in the group
totalInGroup := 0 // Items everyone in the group answered "yes"
groupSet := map[rune]int{}
group := strings.Replace(g, "\n", "", -1)
for _, item := range group {
if val, ok := groupSet[item]; ok {
groupSet[item] = val + 1
continue
}
groupSet[item] = 1
}
for _, count := range groupSet {
if count == nInGroup { // N of yes to an item == N of people
totalInGroup++ // Add one item to the total in the group
}
}
total += totalInGroup
}
return strconv.Itoa(total)
}