advent-20/solutions/day06.go

60 lines
1011 B
Go
Raw Normal View History

2024-03-01 20:45:33 +00:00
package solutions
import (
"strings"
)
func Day06Part01(isTest bool) int {
input := ReadInput("06", isTest)
groups := strings.Split(input, "\n\n")
sum := 0
for _, group := range groups {
frequency := make(map[rune]int)
for _, letter := range group {
// remove non a-z character
if letter < 97 || letter > 122 {
continue
}
previousValue, _ := frequency[letter]
frequency[letter] = previousValue + 1
}
sum += len(frequency)
}
return sum
}
func Day06Part02(isTest bool) int {
2024-03-01 20:49:15 +00:00
input := ReadInput("06", isTest)
groups := strings.Split(input, "\n\n")
sum := 0
for _, group := range groups {
persons := strings.Split(group, "\n")
personAmount := len(persons)
frequency := make(map[rune]int)
2024-03-01 20:45:33 +00:00
2024-03-01 20:49:15 +00:00
for _, person := range persons {
for _, letter := range person {
previousValue, _ := frequency[letter]
frequency[letter] = previousValue + 1
}
}
for _, value := range frequency {
if value == personAmount {
sum += 1
}
}
}
return sum
2024-03-01 20:45:33 +00:00
}