58 lines
1.1 KiB
Go
58 lines
1.1 KiB
Go
package day06
|
|
|
|
import (
|
|
"math"
|
|
"tools"
|
|
)
|
|
|
|
func getCharCounterSet(input []string) []tools.CounterSet {
|
|
charcounts := make([]tools.CounterSet, len(input[0]))
|
|
for i := range charcounts {
|
|
charcounts[i] = tools.NewCounterSet()
|
|
}
|
|
|
|
for _, line := range input {
|
|
for i, c := range line {
|
|
charcounts[i].Add(byte(c), 1)
|
|
}
|
|
}
|
|
|
|
return charcounts
|
|
}
|
|
|
|
func Part1(puzzle tools.AoCPuzzle) interface{} {
|
|
transmission := puzzle.GetInputArray()
|
|
answer := make([]byte, len(transmission[0]))
|
|
charcounts := getCharCounterSet(transmission)
|
|
|
|
for i, s := range charcounts {
|
|
maxCount := 0
|
|
for _, c := range s.Keys() {
|
|
if s.Get(c) > maxCount {
|
|
maxCount = s.Get(c)
|
|
answer[i] = c.(byte)
|
|
}
|
|
}
|
|
}
|
|
|
|
return string(answer)
|
|
}
|
|
|
|
func Part2(puzzle tools.AoCPuzzle) interface{} {
|
|
transmission := puzzle.GetInputArray()
|
|
answer := make([]byte, len(transmission[0]))
|
|
charcounts := getCharCounterSet(transmission)
|
|
|
|
for i, s := range charcounts {
|
|
minCount := math.MaxInt16
|
|
for _, c := range s.Keys() {
|
|
if s.Get(c) < minCount {
|
|
minCount = s.Get(c)
|
|
answer[i] = c.(byte)
|
|
}
|
|
}
|
|
}
|
|
|
|
return string(answer)
|
|
}
|