-
Notifications
You must be signed in to change notification settings - Fork 0
/
day01.go
56 lines (48 loc) · 1.12 KB
/
day01.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
package main
import (
"fmt"
"os"
"sort"
"strconv"
"github.com/dergeberl/aoc/utils"
)
func main() {
input, err := os.ReadFile("input.txt")
if err != nil {
os.Exit(1)
}
fmt.Printf("Part 1: %v\n", SolveDay01Part1(string(input)))
fmt.Printf("Part 2: %v\n", SolveDay01Part2(string(input)))
}
// SolveDay01Part1 returns the number of calories from the elf with the most calories
func SolveDay01Part1(input string) int {
calories := getCaloriesFromInput(input)
sort.Ints(calories)
return calories[len(calories)-1]
}
// SolveDay01Part2 returns the sum of calories from the 3 elf with the most calories
func SolveDay01Part2(input string) int {
calories := getCaloriesFromInput(input)
sort.Ints(calories)
return utils.SumSlice(calories[len(calories)-3:])
}
func getCaloriesFromInput(input string) []int {
line, _ := utils.InputToSlice(input)
elf := 0
var calories []int
for i := range line {
if line[i] == "" {
elf++
continue
}
if len(calories)-1 < elf {
calories = append(calories, 0)
}
lintInt, err := strconv.Atoi(line[i])
if err != nil {
continue
}
calories[elf] += lintInt
}
return calories
}