adventofcode/2020/day06/main.go

75 lines
1.4 KiB
Go
Raw Permalink Normal View History

2020-12-06 16:31:50 +00:00
package main
import (
"bytes"
"fmt"
h "git.bullercodeworks.com/brian/adventofcode/helpers"
)
func main() {
fmt.Println("# Day 6")
inp := h.StdinToStringSlice()
part2(inp)
}
func part1(inp []string) {
yeses := make(map[byte]bool)
var total int
for _, v := range inp {
if v == "" {
fmt.Printf("Group Total: %d\n", len(yeses))
total = total + len(yeses)
yeses = make(map[byte]bool)
} else {
for _, i := range v {
yeses[byte(i)] = true
}
}
}
fmt.Printf("Group Total: %d\n", len(yeses))
total = total + len(yeses)
fmt.Printf("Answer: %d\n", total)
}
func part2(inp []string) {
var total int
yeses := []byte{}
newgroup := true
for _, v := range inp {
if v == "" {
newgroup = true
var grouptotal int
for yk := range yeses {
if yeses[yk] != ' ' {
grouptotal++
}
}
total = total + grouptotal
fmt.Printf("Totals: %d -> %d\n", grouptotal, total)
} else {
if newgroup {
newgroup = false
yeses = []byte(v)
fmt.Println("Initializing", string(yeses))
} else {
for yk, yv := range yeses {
if !bytes.Contains([]byte(v), []byte{yv}) {
yeses[yk] = ' '
fmt.Println("Removing", string(yv), "->", string(yeses))
}
}
}
}
}
var grouptotal int
for yk := range yeses {
if yeses[yk] != ' ' {
grouptotal++
}
}
total = total + grouptotal
fmt.Printf("Totals: %d -> %d\n", grouptotal, total)
fmt.Printf("Answer: %d\n", total)
}