[2020-12-06] Add solutions for day06

master
Elis Hirwing 3 years ago
parent 1abc9306a3
commit 49401e90ad
Signed by: etu
GPG Key ID: D57EFA625C9A925F

@ -18,6 +18,8 @@ day05:
go run main.go day05 input
day06:
go run main.go day06 input
day07:
day08:
day09:

@ -0,0 +1,15 @@
abc
a
b
c
ab
ac
a
a
a
a
b

File diff suppressed because it is too large Load Diff

@ -0,0 +1,48 @@
package day06
import (
"git.elis.nu/etu/aoc2020/utils"
)
// Build voting groups where we store tho gorup size, the question
// that got votes, and how many votes each question got.
type Group struct {
Size int
Votes map[byte]int
}
var rows []Group
func ParseFile(input string) {
row := Group{
Size: 0,
Votes: make(map[byte]int),
}
// Parse file
for _, line := range utils.GetLinesFromFile("day06/" + input + ".txt") {
// Commit group to list on group change
if line == "" {
rows = append(rows, row)
row = Group{
Size: 0,
Votes: make(map[byte]int),
}
continue
}
// Increment group size
row.Size++
// Go through votes
for i := 0; i < len(line); i++ {
// Increment vote count
row.Votes[line[i]]++
}
}
// Don't drop last entry
rows = append(rows, row)
}

@ -0,0 +1,16 @@
package day06
import (
"log"
)
func Solve1() {
counter := 0
for _, row := range rows {
// Sum the amount of votes in total
counter += len(row.Votes)
}
log.Printf("2020-12-06.01: Answer: %d\n", counter)
}

@ -0,0 +1,22 @@
package day06
import (
"log"
)
func Solve2() {
counter := 0
for _, row := range rows {
// Get the amount of votes on each question
for _, amount := range row.Votes {
// Check that everyone voted yes on that
if amount == row.Size {
// If so, increment counter
counter++
}
}
}
log.Printf("2020-12-06.02: Answer: %d\n", counter)
}

@ -9,6 +9,7 @@ import (
"git.elis.nu/etu/aoc2020/day03"
"git.elis.nu/etu/aoc2020/day04"
"git.elis.nu/etu/aoc2020/day05"
"git.elis.nu/etu/aoc2020/day06"
"git.elis.nu/etu/aoc2020/utils"
)
@ -32,5 +33,8 @@ func main() {
case "day05":
utils.Perf("2020-12-05", day05.ParseFile, day05.Solve1, day05.Solve2)
case "day06":
utils.Perf("2020-12-06", day06.ParseFile, day06.Solve1, day06.Solve2)
}
}

Loading…
Cancel
Save