Day 6: Part 1 and 2

This commit is contained in:
2020-12-06 10:37:17 -04:00
parent bb8c8090ee
commit 27038ae795
5 changed files with 2356 additions and 3 deletions

10
main.go
View File

@@ -3,7 +3,7 @@ package main
import ( import (
"fmt" "fmt"
"github.com/thatguygriff/aoc2020/five" "github.com/thatguygriff/aoc2020/six"
) )
func main() { func main() {
@@ -24,6 +24,10 @@ func main() {
// fmt.Println(four.PartTwo()) // fmt.Println(four.PartTwo())
// Day 5 // Day 5
fmt.Println(five.PartOne()) // fmt.Println(five.PartOne())
fmt.Println(five.PartTwo()) // fmt.Println(five.PartTwo())
// Day 6
fmt.Println(six.PartOne())
fmt.Println(six.PartTwo())
} }

2128
six/customs.txt Normal file

File diff suppressed because it is too large Load Diff

105
six/day_six.go Normal file
View File

@@ -0,0 +1,105 @@
package six
import (
"bufio"
"fmt"
"os"
)
type group struct {
questions []string
}
type flight struct {
groups []group
}
func (g *group) uniqueQuestion() int {
questions := map[string]bool{}
for _, answers := range g.questions {
for _, q := range answers {
if !questions[string(q)] {
questions[string(q)] = true
}
}
}
return len(questions)
}
func (g *group) consensusQuestions() int {
questions := map[string][]bool{}
for _, answers := range g.questions {
for _, q := range answers {
questions[string(q)] = append(questions[string(q)], true)
}
}
consensus := 0
for _, q := range questions {
if len(q) == len(g.questions) {
consensus++
}
}
return consensus
}
func (f *flight) load(filename string) error {
file, err := os.Open(filename)
if err != nil {
return err
}
defer file.Close()
scanner := bufio.NewScanner(file)
var g *group
for scanner.Scan() {
if scanner.Text() == "" {
// Blank line means we have moved on to the next passport
if g != nil {
f.groups = append(f.groups, *g)
g = nil
}
continue
}
if g == nil {
g = &group{}
}
g.questions = append(g.questions, scanner.Text())
}
// commit the last passport if there isn't a blank line
if g != nil {
f.groups = append(f.groups, *g)
}
return nil
}
// PartOne Find the sum of all the unique questions in all groups
func PartOne() string {
f := flight{}
f.load("six/customs.txt")
sum := 0
for _, g := range f.groups {
sum += g.uniqueQuestion()
}
return fmt.Sprintf("The sum of all the unique questions in all groups is %d", sum)
}
// PartTwo Find the sum of all the consensus questions in all groups
func PartTwo() string {
f := flight{}
f.load("six/customs.txt")
sum := 0
for _, g := range f.groups {
sum += g.consensusQuestions()
}
return fmt.Sprintf("The sum of all the consensus questions in all groups is %d", sum)
}

97
six/day_six_test.go Normal file
View File

@@ -0,0 +1,97 @@
package six
import "testing"
func Test_data_load(t *testing.T) {
f := flight{}
err := f.load("sample.txt")
if err != nil {
t.Log(err)
t.FailNow()
}
if len(f.groups) != 6 {
t.Logf("Expected 6 groups, Got %d groups", len(f.groups))
t.FailNow()
}
uQ := f.groups[0].uniqueQuestion()
if uQ != 6 {
t.Logf("Expected 6 questions in Group 1, Got %d questions", uQ)
t.FailNow()
}
uQ = f.groups[1].uniqueQuestion()
if uQ != 3 {
t.Logf("Expected 3 questions in Group 2, Got %d questions", uQ)
t.FailNow()
}
uQ = f.groups[2].uniqueQuestion()
if uQ != 3 {
t.Logf("Expected 3 questions in Group 3, Got %d questions", uQ)
t.FailNow()
}
uQ = f.groups[3].uniqueQuestion()
if uQ != 3 {
t.Logf("Expected 3 questions in Group 4, Got %d questions", uQ)
t.FailNow()
}
uQ = f.groups[4].uniqueQuestion()
if uQ != 1 {
t.Logf("Expected 1 questions in Group 5, Got %d questions", uQ)
t.FailNow()
}
uQ = f.groups[5].uniqueQuestion()
if uQ != 1 {
t.Logf("Expected 1 questions in Group 6, Got %d groups", uQ)
t.FailNow()
}
}
func Test_flight_consensus_questions(t *testing.T) {
f := flight{}
err := f.load("sample.txt")
if err != nil {
t.Log(err)
t.FailNow()
}
if len(f.groups) != 6 {
t.Logf("Expected 6 groups, Got %d groups", len(f.groups))
t.FailNow()
}
cQ := f.groups[1].consensusQuestions()
if cQ != 3 {
t.Logf("Expected 3 questions in Group 2, Got %d questions", cQ)
t.FailNow()
}
cQ = f.groups[2].consensusQuestions()
if cQ != 0 {
t.Logf("Expected 0 questions in Group 3, Got %d questions", cQ)
t.FailNow()
}
cQ = f.groups[3].consensusQuestions()
if cQ != 1 {
t.Logf("Expected 3 questions in Group 4, Got %d questions", cQ)
t.FailNow()
}
cQ = f.groups[4].consensusQuestions()
if cQ != 1 {
t.Logf("Expected 1 questions in Group 5, Got %d questions", cQ)
t.FailNow()
}
cQ = f.groups[5].consensusQuestions()
if cQ != 1 {
t.Logf("Expected 1 questions in Group 6, Got %d groups", cQ)
t.FailNow()
}
}

19
six/sample.txt Normal file
View File

@@ -0,0 +1,19 @@
abcx
abcy
abcz
abc
a
b
c
ab
ac
a
a
a
a
b