-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
37 lines (31 loc) · 806 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
package main
import (
"cristianrb-aoc-2022/io"
"fmt"
"sort"
"strconv"
)
func main() {
data, _ := io.ReadLines("Day-01/input_01")
caloriesOrdered := mapCaloriesToIntOrdered(data)
fmt.Printf("Part 1: %d\n", caloriesOrdered[0])
fmt.Printf("Part 2: %d\n", caloriesOrdered[0]+caloriesOrdered[1]+caloriesOrdered[2])
}
func mapCaloriesToIntOrdered(lines []string) []int {
actualCalories := 0
caloriesList := []int{}
for _, line := range lines {
if line == "" {
caloriesList = append(caloriesList, actualCalories)
actualCalories = 0
} else {
calories, _ := strconv.Atoi(line)
actualCalories += calories
}
}
caloriesList = append(caloriesList, actualCalories)
sort.Slice(caloriesList, func(i, j int) bool {
return caloriesList[i] > caloriesList[j]
})
return caloriesList
}