2020 day 6 - string parsing and hash maps to collect counts

This commit is contained in:
alexchao26
2020-12-06 00:20:45 -05:00
parent d3d97e1db3
commit 9a9dec2523
4 changed files with 2313 additions and 24 deletions
+2147
View File
File diff suppressed because it is too large Load Diff
+40 -19
View File
@@ -3,9 +3,8 @@ package main
import (
"flag"
"fmt"
"io/ioutil"
"strings"
"github.com/alexchao26/advent-of-code-go/util"
)
func main() {
@@ -14,38 +13,60 @@ func main() {
flag.Parse()
fmt.Println("Running part", part)
fileBytes, err := ioutil.ReadFile("input.txt")
if err != nil {
panic("Reading file" + err.Error())
}
if part == 1 {
ans := part1(util.ReadFile("./input.txt"))
util.CopyToClipboard(fmt.Sprintf("%v", ans))
ans := part1(string(fileBytes))
fmt.Println("Output:", ans)
} else {
ans := part2(util.ReadFile("./input.txt"))
util.CopyToClipboard(fmt.Sprintf("%v", ans))
ans := part2(string(fileBytes))
fmt.Println("Output:", ans)
}
}
func part1(input string) int {
parsed := parseInput(input)
_ = parsed
var sum int
return 0
groups := strings.Split(input, "\n\n")
for _, group := range groups {
questionsSeen := map[string]bool{}
people := strings.Split(group, "\n")
for _, person := range people {
for _, question := range strings.Split(person, "") {
questionsSeen[question] = true
}
}
sum += len(questionsSeen)
}
return sum
}
func part2(input string) int {
parsed := parseInput(input)
_ = parsed
var sum int
return 0
}
groups := strings.Split(input, "\n\n")
for _, group := range groups {
questionsToCount := map[string]int{}
func parseInput(input string) []int {
var ans []int
people := strings.Split(group, "\n")
for _, person := range people {
for _, question := range strings.Split(person, "") {
questionsToCount[question]++
}
}
lines := strings.Split(input, "\n")
for _, l := range lines {
ans = append(ans, util.StrToInt(l))
for _, count := range questionsToCount {
if count == len(people) {
sum++
}
}
}
return ans
return sum
}
+22 -5
View File
@@ -1,14 +1,17 @@
package main
import "testing"
import (
"testing"
"github.com/alexchao26/advent-of-code-go/util"
)
var tests1 = []struct {
name string
want int
input string
// add extra args if needed
}{
// {"actual", ACTUAL_ANSWER, util.ReadFile("input.txt")},
{"actual", 6387, util.ReadFile("input.txt")},
}
func TestPart1(t *testing.T) {
@@ -25,9 +28,23 @@ var tests2 = []struct {
name string
want int
input string
// add extra args if needed
}{
// {"actual", ACTUAL_ANSWER, util.ReadFile("input.txt")},
{"actual", 6, `abc
a
b
c
ab
ac
a
a
a
a
b`},
{"actual", 3039, util.ReadFile("input.txt")},
}
func TestPart2(t *testing.T) {
+104
View File
@@ -0,0 +1,104 @@
--- Day 6: Custom Customs ---
As your flight approaches the regional airport where you'll switch to a much larger plane, customs declaration forms are distributed to the passengers.
The form asks a series of 26 yes-or-no questions marked a through z. All you need to do is identify the questions for which anyone in your group answers "yes". Since your group is just you, this doesn't take very long.
However, the person sitting next to you seems to be experiencing a language barrier and asks if you can help. For each of the people in their group, you write down the questions for which they answer "yes", one per line. For example:
abcx
abcy
abcz
In this group, there are 6 questions to which anyone answered "yes": a, b, c, x, y, and z. (Duplicate answers to the same question don't count extra; each question counts at most once.)
Another group asks for your help, then another, and eventually you've collected answers from every group on the plane (your puzzle input). Each group's answers are separated by a blank line, and within each group, each person's answers are on a single line. For example:
abc
a
b
c
ab
ac
a
a
a
a
b
This list represents answers from five groups:
The first group contains one person who answered "yes" to 3 questions: a, b, and c.
The second group contains three people; combined, they answered "yes" to 3 questions: a, b, and c.
The third group contains two people; combined, they answered "yes" to 3 questions: a, b, and c.
The fourth group contains four people; combined, they answered "yes" to only 1 question, a.
The last group contains one person who answered "yes" to only 1 question, b.
In this example, the sum of these counts is 3 + 3 + 3 + 1 + 1 = 11.
For each group, count the number of questions to which anyone answered "yes". What is the sum of those counts?
--- Part Two ---
As you finish the last group's customs declaration, you notice that you misread one word in the instructions:
You don't need to identify the questions to which anyone answered "yes"; you need to identify the questions to which everyone answered "yes"!
Using the same example as above:
abc
a
b
c
ab
ac
a
a
a
a
b
This list represents answers from five groups:
In the first group, everyone (all 1 person) answered "yes" to 3 questions: a, b, and c.
In the second group, there is no question to which everyone answered "yes".
In the third group, everyone answered yes to only 1 question, a. Since some people did not answer "yes" to b or c, they don't count.
In the fourth group, everyone answered yes to only 1 question, a.
In the fifth group, everyone (all 1 person) answered "yes" to 1 question, b.
In this example, the sum of these counts is 3 + 0 + 1 + 1 + 1 = 6.
For each group, count the number of questions to which everyone answered "yes". What is the sum of those counts?