-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
42 lines (37 loc) · 800 Bytes
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package main
import (
"fmt"
"sort"
"strconv"
. "github.com/baspar/adventofcode2022/internal"
)
type DayImpl struct {
stacks []int
}
func (d *DayImpl) Init(lines []string) error {
stack := 0
for _, line := range lines {
if len(line) == 0 {
d.stacks = append(d.stacks, stack)
stack = 0
} else if calories, err := strconv.Atoi(line); err == nil {
stack += calories
} else {
return err
}
}
if stack > 0 {
d.stacks = append(d.stacks, stack)
}
sort.Ints(d.stacks)
return nil
}
func (d *DayImpl) Part1() (string, error) {
return fmt.Sprint(d.stacks[len(d.stacks)-1]), nil
}
func (d *DayImpl) Part2() (string, error) {
return fmt.Sprint(d.stacks[len(d.stacks)-1] + d.stacks[len(d.stacks)-2] + d.stacks[len(d.stacks)-3]), nil
}
func main() {
Run(&DayImpl{}, false)
}