master
Icedream 2020-12-06 06:21:40 +01:00
parent a4e13c8f36
commit 7681d7bbc2
Signed by: icedream
GPG Key ID: 1573F6D8EFE4D0CF
3 changed files with 2426 additions and 0 deletions

2262
day6/input Normal file

File diff suppressed because it is too large Load Diff

70
day6/part1/main.go Normal file
View File

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

94
day6/part2/main.go Normal file
View File

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