95 lines
1.7 KiB
Go
95 lines
1.7 KiB
Go
package main
|
|
|
|
import (
|
|
"bufio"
|
|
"io"
|
|
"log"
|
|
"os"
|
|
"strings"
|
|
)
|
|
|
|
type Group struct {
|
|
QuestionsAnsweredWithYes []rune
|
|
Persons []string
|
|
}
|
|
|
|
func (g *Group) SumOfAgreedQuestions() int {
|
|
sum := 0
|
|
for _, answer := range g.QuestionsAnsweredWithYes {
|
|
allAnsweredWithYes := true
|
|
for _, person := range g.Persons {
|
|
personHasAnsweredWithYes := false
|
|
for _, personAnswer := range person {
|
|
if personAnswer == answer {
|
|
personHasAnsweredWithYes = true
|
|
break
|
|
}
|
|
}
|
|
if !personHasAnsweredWithYes {
|
|
allAnsweredWithYes = false
|
|
break
|
|
}
|
|
}
|
|
if allAnsweredWithYes {
|
|
sum++
|
|
}
|
|
}
|
|
return sum
|
|
}
|
|
|
|
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 += group.SumOfAgreedQuestions()
|
|
}
|
|
|
|
log.Printf("Sum: %d", sum)
|
|
}
|