[2020-12-06] Add solutions for day06
parent
1abc9306a3
commit
49401e90ad
@ -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)
|
||||
}
|
Loading…
Reference in New Issue