Add day 6 solution.
This commit is contained in:
parent
b8cd01b914
commit
c8cc6d34ae
3 changed files with 84 additions and 18 deletions
61
2020/day06.go
Normal file
61
2020/day06.go
Normal file
|
@ -0,0 +1,61 @@
|
|||
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)
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue