From c8cc6d34ae625abe8a4c1c4c9cc8aa65791ab45e Mon Sep 17 00:00:00 2001 From: Anna Wiggins Date: Fri, 11 Dec 2020 03:32:15 +0000 Subject: [PATCH] Add day 6 solution. --- 2020/day04.go | 20 ++----------- 2020/day06.go | 61 ++++++++++++++++++++++++++++++++++++++++ 2020/lib/util/strings.go | 21 ++++++++++++++ 3 files changed, 84 insertions(+), 18 deletions(-) create mode 100644 2020/day06.go create mode 100644 2020/lib/util/strings.go diff --git a/2020/day04.go b/2020/day04.go index dc234f2..41fa5b3 100644 --- a/2020/day04.go +++ b/2020/day04.go @@ -9,25 +9,9 @@ import ( "strings" "git.annabunch.es/annabunches/adventofcode/2020/lib/fileutils" + "git.annabunch.es/annabunches/adventofcode/2020/lib/util" ) -func splitPassports(input []string) []string { - converted := []string{} - - current := "" - for _, line := range input { - if line == "" { - converted = append(converted, current) - current = "" - continue - } - - current += " " + line - } - - return converted -} - func countValidPassports(input []string, step string) int { total := 0 for _, line := range input { @@ -147,7 +131,7 @@ func makeInt(input string) int { func main() { step := os.Args[1] values := fileutils.InputParserStrings(os.Args[2]) - values = splitPassports(values) + values = util.SplitOnBlankLine(values) fmt.Println("Valid Passports:", countValidPassports(values, step)) } diff --git a/2020/day06.go b/2020/day06.go new file mode 100644 index 0000000..00a6e2c --- /dev/null +++ b/2020/day06.go @@ -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) +} diff --git a/2020/lib/util/strings.go b/2020/lib/util/strings.go new file mode 100644 index 0000000..a2f35ae --- /dev/null +++ b/2020/lib/util/strings.go @@ -0,0 +1,21 @@ +package util + +// Takes a slice of strings as from reading each line in a file. +// Concatenates strings, creating new ones when blank lines are encountered +// NB: adds a single space to the beginning of each concatenated line. +func SplitOnBlankLine(input []string) []string { + converted := []string{} + + current := "" + for _, line := range input { + if line == "" { + converted = append(converted, current) + current = "" + continue + } + + current += " " + line + } + + return converted +}