adventofcode-2020/day6/part1/main.go

71 lines
1.3 KiB
Go
Raw Normal View History

2020-12-06 05:21:40 +00:00
package main
import (
"bufio"
"io"
"log"
"os"
"strings"
)
type Group struct {
QuestionsAnsweredWithYes []rune
Persons []string
}
func FromLines(lines []string) *Group {
g := &Group{
QuestionsAnsweredWithYes: []rune{},
Persons: []string{},
}
for _, line := range lines {
g.Persons = append(g.Persons, line)
thisPersonsAnswer:
for _, answer := range line {
for _, alreadyAnswered := range g.QuestionsAnsweredWithYes {
if alreadyAnswered == answer {
continue thisPersonsAnswer
}
}
g.QuestionsAnsweredWithYes = append(g.QuestionsAnsweredWithYes, answer)
}
}
return g
}
func main() {
groups := []*Group{}
f, err := os.Open("input")
if err != nil {
panic(err)
}
r := bufio.NewReader(f)
currentGroup := []string{}
for {
line, err := r.ReadString('\n')
line = strings.TrimSpace(line)
if err == io.EOF || len(line) <= 0 {
if len(currentGroup) > 0 {
groups = append(groups, FromLines(currentGroup))
currentGroup = []string{}
}
if err == io.EOF {
break
}
continue
}
if err != nil {
panic(err)
}
currentGroup = append(currentGroup, line)
}
sum := 0
for _, group := range groups {
sum += len(group.QuestionsAnsweredWithYes)
}
log.Printf("Sum: %d", sum)
}