-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday01.go
70 lines (49 loc) · 1.33 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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package day1
import (
"sort"
"strconv"
"strings"
)
func parseInput(input string) [][]int {
var parsedElfMeals [][]int
for _, elfMeals := range strings.Split(input, "\n\n") {
var elfNumericCalories []int
for _, mealCalories := range strings.Split(elfMeals, "\n") {
numericCalorie, parsingError := strconv.Atoi(mealCalories)
if parsingError != nil {
panic(parsingError)
}
elfNumericCalories = append(elfNumericCalories, numericCalorie)
}
parsedElfMeals = append(parsedElfMeals, elfNumericCalories)
}
return parsedElfMeals
}
func sum(items []int) (totalCalories int) {
total := 0
for _, value := range items {
total = total + value
}
return total
}
func part1(input string) (result int) {
parsedElfMeals := parseInput(input)
maxCalorieCount := 0
for _, elfMeals := range parsedElfMeals {
totalCalories := sum(elfMeals)
if maxCalorieCount < totalCalories {
maxCalorieCount = totalCalories
}
}
return maxCalorieCount
}
func part2(input string) (result int) {
parsedElfMeals := parseInput(input)
var totalElfCalories []int
for _, elfMeals := range parsedElfMeals {
totalCalories := sum(elfMeals)
totalElfCalories = append(totalElfCalories, totalCalories)
}
sort.Sort(sort.Reverse(sort.IntSlice(totalElfCalories)))
return totalElfCalories[0] + totalElfCalories[1] + totalElfCalories[2]
}