-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
106 lines (90 loc) · 1.67 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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
package main
import (
input "aoc2020/inpututils"
"fmt"
"strings"
)
func main() {
fmt.Println("--- Part One ---")
fmt.Println(Part1("input.txt"))
fmt.Println("--- Part Two ---")
fmt.Println(Part2("input.txt"))
}
func Part1(filename string) int {
lines := input.ReadRaw(filename)
splitLines := strings.Split(lines, "\n\n")
sum := 0
for _, line := range splitLines {
allLetters := strings.ReplaceAll(line, "\n", "")
sum += countDistinctLetters(allLetters)
}
return sum
}
func Part2(filename string) int {
lines := input.ReadRaw(filename)
splitLines := strings.Split(lines, "\n\n")
sum := 0
for _, line := range splitLines {
strings := strings.Split(line, "\n")
sum += countCommonLetters(strings)
}
return sum
}
func countDistinctLetters(s string) int {
seen := set{}
for _, c := range s {
seen.add(int(c))
}
return seen.len()
}
func countCommonLetters(strings []string) int {
seen := set{}
for _, c := range strings[0] {
seen.add(int(c))
}
for _, a := range strings[1:] {
for key, _ := range seen {
if !in(stringToIntArray(a), key) {
seen.delete(key)
}
}
}
return seen.len()
}
// create set type as Go doesn't have one
type set map[int]struct{}
func (s set) add(n int) {
s[n] = struct{}{}
}
func (s set) has(n int) bool {
_, ok := s[n]
return ok
}
func (s set) delete(n int) {
_, ok := s[n]
if ok {
delete(s, n)
}
}
func (s set) len() int {
var l int
for _, _ = range s {
l += 1
}
return l
}
func stringToIntArray(s string) []int {
vsm := make([]int, len(s))
for i, v := range s {
vsm[i] = int(v)
}
return vsm
}
func in(a []int, e int) bool {
for _, i := range a {
if i == e {
return true
}
}
return false
}