-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path01.go
74 lines (60 loc) · 1.41 KB
/
01.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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package main
import (
"fmt"
"sort"
"strconv"
"strings"
)
// AOC202201Helper builds a sorted list of supplies from the specified input
func AOC202201Helper(input string) ([]int, error) {
var supplies []int
lines := strings.Split(input, "\n")
sum := 0
for _, line := range lines {
if line == "" {
if sum != 0 {
supplies = append(supplies, sum)
sum = 0
}
continue
}
lineInt, err := strconv.Atoi(line)
if err != nil {
return nil, fmt.Errorf("can't parse \"%s\" as an integer: %v", line, err)
}
sum += lineInt
}
// Allow inputs without newlines at the end
if sum != 0 {
supplies = append(supplies, sum)
}
sort.Ints(supplies)
return supplies, nil
}
// AOC202201SumMaxN sums the n biggest items of supplies
func AOC202201SumMaxN(supplies []int, n int) (int, error) {
if len(supplies) < n {
return 0, fmt.Errorf("input %+v contains less than %d elements", supplies, n)
}
sum := 0
for _, supply := range supplies[len(supplies)-n:] {
sum += supply
}
return sum, nil
}
func AOC2022011(input string) (string, error) {
supplies, err := AOC202201Helper(input)
if err != nil {
return "", err
}
res, err := AOC202201SumMaxN(supplies, 1)
return fmt.Sprintf("%d", res), err
}
func AOC2022012(input string) (string, error) {
supplies, err := AOC202201Helper(input)
if err != nil {
return "", err
}
res, err := AOC202201SumMaxN(supplies, 3)
return fmt.Sprintf("%d", res), err
}