adventofcode/2020/day06.go
2020-12-11 03:32:15 +00:00

62 lines
1.0 KiB
Go

package main
import (
"fmt"
"os"
"strings"
"git.annabunch.es/annabunches/adventofcode/2020/lib/fileutils"
"git.annabunch.es/annabunches/adventofcode/2020/lib/util"
)
func countAnswerUnion(input string) int {
charMap := make(map[rune]bool)
for _, char := range input {
if char == ' ' {
continue
}
if _, ok := charMap[char]; !ok {
charMap[char] = true
}
}
return len(charMap)
}
func countAnswerIntersection(input string) int {
charMap := make(map[rune]int)
answers := strings.Split(strings.Trim(input, " "), " ")
for _, answer := range answers {
for _, char := range answer {
charMap[char]++
}
}
total := 0
for _, count := range charMap {
if count == len(answers) {
total++
}
}
return total
}
func main() {
step := os.Args[1]
values := fileutils.InputParserStrings(os.Args[2])
groups := util.SplitOnBlankLine(values)
total := 0
for _, line := range groups {
if step == "1" {
total += countAnswerUnion(line)
}
if step == "2" {
total += countAnswerIntersection(line)
}
}
fmt.Println("Total:", total)
}