Add day 6 solution.

This commit is contained in:
Anna Rose 2020-12-11 03:32:15 +00:00
parent b8cd01b914
commit c8cc6d34ae
3 changed files with 84 additions and 18 deletions

View File

@ -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))
}

61
2020/day06.go Normal file
View 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)
}

21
2020/lib/util/strings.go Normal file
View File

@ -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
}