Day 6.
parent
a4e13c8f36
commit
7681d7bbc2
File diff suppressed because it is too large
Load Diff
|
@ -0,0 +1,70 @@
|
|||
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)
|
||||
}
|
|
@ -0,0 +1,94 @@
|
|||
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)
|
||||
}
|
Loading…
Reference in New Issue