re-organise repo
This commit is contained in:
44
2022/go/day01/day01.go
Normal file
44
2022/go/day01/day01.go
Normal file
@@ -0,0 +1,44 @@
|
||||
package day01
|
||||
|
||||
import (
|
||||
"sort"
|
||||
"strings"
|
||||
|
||||
"adventofcode2022/utils"
|
||||
)
|
||||
|
||||
type elf struct {
|
||||
calories int
|
||||
}
|
||||
|
||||
func Part1(input string) int {
|
||||
elves := common(input)
|
||||
return elves[0].calories
|
||||
}
|
||||
|
||||
func Part2(input string) int {
|
||||
elves := common(input)
|
||||
return elves[0].calories + elves[1].calories + elves[2].calories
|
||||
}
|
||||
|
||||
func common(input string) []elf {
|
||||
lines := strings.Split(input, "\n")
|
||||
|
||||
elves := []elf{}
|
||||
e := elf{}
|
||||
for _, line := range lines {
|
||||
if line == "" {
|
||||
elves = append(elves, e)
|
||||
e = elf{}
|
||||
} else {
|
||||
e.calories += utils.MustAtoi(line)
|
||||
}
|
||||
}
|
||||
elves = append(elves, e)
|
||||
|
||||
sort.Slice(elves, func(i, j int) bool {
|
||||
return elves[i].calories > elves[j].calories
|
||||
})
|
||||
|
||||
return elves
|
||||
}
|
||||
Reference in New Issue
Block a user