Day6
This commit is contained in:
parent
21e6b80a5e
commit
59cf5ed2c3
65
day6/day6.go
Normal file
65
day6/day6.go
Normal file
@ -0,0 +1,65 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"AoC2020/helper"
|
||||
"fmt"
|
||||
"os"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func main() {
|
||||
args := os.Args[1:]
|
||||
input, err := helper.GetFile(args[0])
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
}
|
||||
groups := strings.Split(input, "\n\n")
|
||||
fmt.Println(day6(groups))
|
||||
fmt.Println(day6Part2(groups))
|
||||
}
|
||||
|
||||
func day6(groups []string) int {
|
||||
var count int
|
||||
for _, group := range groups {
|
||||
count += len(getYesAnsweredQuestions(group))
|
||||
}
|
||||
return count
|
||||
}
|
||||
|
||||
func day6Part2(groups []string) int {
|
||||
var count int
|
||||
for _, group := range groups {
|
||||
count += getQuestionsEveryoneAnsweredYes(group)
|
||||
}
|
||||
return count
|
||||
}
|
||||
|
||||
func getYesAnsweredQuestions(group string) string {
|
||||
var answeredQuestions string
|
||||
persons := strings.Split(group, "\n")
|
||||
for _, person := range persons {
|
||||
for _, answer := range person {
|
||||
if !strings.Contains(answeredQuestions, string(answer)) {
|
||||
answeredQuestions += string(answer)
|
||||
}
|
||||
}
|
||||
}
|
||||
return answeredQuestions
|
||||
}
|
||||
|
||||
func getQuestionsEveryoneAnsweredYes(group string) int {
|
||||
var questions [26]int
|
||||
persons := strings.Split(group, "\n")
|
||||
for _, person := range persons {
|
||||
for _, answer := range person {
|
||||
questions[answer-97] += 1
|
||||
}
|
||||
}
|
||||
count := 0
|
||||
for _,question := range questions {
|
||||
if question == len(persons){
|
||||
count++
|
||||
}
|
||||
}
|
||||
return count
|
||||
}
|
2167
day6/day6Input.txt
Normal file
2167
day6/day6Input.txt
Normal file
File diff suppressed because it is too large
Load Diff
15
day6/day6Test.txt
Normal file
15
day6/day6Test.txt
Normal file
@ -0,0 +1,15 @@
|
||||
abc
|
||||
|
||||
a
|
||||
b
|
||||
c
|
||||
|
||||
ab
|
||||
ac
|
||||
|
||||
a
|
||||
a
|
||||
a
|
||||
a
|
||||
|
||||
b
|
Loading…
Reference in New Issue
Block a user