61 lines
1.0 KiB
Go
61 lines
1.0 KiB
Go
package day1
|
|
|
|
import (
|
|
"sort"
|
|
"strconv"
|
|
)
|
|
|
|
type Solver struct {
|
|
elves [][]int
|
|
}
|
|
|
|
func (s *Solver) ParseInput(input []string) {
|
|
elves := make([][]int, 0)
|
|
|
|
elfIndex := 0
|
|
elves = append(elves, make([]int, 0))
|
|
for _, line := range input {
|
|
if line == "" {
|
|
elfIndex += 1
|
|
elves = append(elves, make([]int, 0))
|
|
} else {
|
|
n, _ := strconv.Atoi(line)
|
|
elves[elfIndex] = append(elves[elfIndex], n)
|
|
}
|
|
}
|
|
|
|
s.elves = elves
|
|
}
|
|
|
|
func (s *Solver) SolvePart1() any {
|
|
mostCalories := 0
|
|
for _, calories := range s.elves {
|
|
sum := 0
|
|
for _, c := range calories {
|
|
sum += c
|
|
}
|
|
if sum > mostCalories {
|
|
mostCalories = sum
|
|
}
|
|
}
|
|
return mostCalories
|
|
}
|
|
|
|
func (s *Solver) SolvePart2() any {
|
|
caloriesPerElf := make([]int, len(s.elves))
|
|
for i, calories := range s.elves {
|
|
sum := 0
|
|
for _, c := range calories {
|
|
sum += c
|
|
}
|
|
caloriesPerElf[i] = sum
|
|
}
|
|
sort.Ints(caloriesPerElf)
|
|
|
|
top3sum := 0
|
|
for i := len(caloriesPerElf) - 3; i < len(caloriesPerElf); i++ {
|
|
top3sum += caloriesPerElf[i]
|
|
}
|
|
return top3sum
|
|
}
|