Browse Source

[2020-12-06] Add solutions for day06

master
Elis Hirwing 10 months ago
parent
commit
49401e90ad
Signed by: etu GPG Key ID: D57EFA625C9A925F
  1. 2
      Makefile
  2. 15
      day06/example.txt
  3. 2199
      day06/input.txt
  4. 48
      day06/parse.go
  5. 16
      day06/solve1.go
  6. 22
      day06/solve2.go
  7. 4
      main.go

2
Makefile

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

15
day06/example.txt

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

2199
day06/input.txt

File diff suppressed because it is too large

48
day06/parse.go

@ -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)
}

16
day06/solve1.go

@ -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)
}

22
day06/solve2.go

@ -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)
}

4
main.go

@ -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