-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
52 lines (46 loc) · 784 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
43
44
45
46
47
48
49
50
51
52
package main
import (
"bufio"
"fmt"
"log"
"os"
"sort"
"strconv"
)
func main() {
input, err := os.Open("input")
if err != nil {
log.Fatal(err)
}
defer input.Close()
scanner := bufio.NewScanner(input)
var calories []int64
var sum int64
for scanner.Scan() {
if scanner.Text() != "" {
i, err := strconv.ParseInt(scanner.Text(), 10, 64)
if err != nil {
log.Fatal(err)
}
sum += i
} else {
calories = append(calories, sum)
sum = 0
}
}
calories = append(calories, sum)
// Part 1
var max int64
for _, v := range calories {
if v > max {
max = v
}
}
fmt.Println(max)
// Part 2
sort.Slice(calories, func(i, j int) bool {
return calories[i] > calories[j]
})
top3 := calories[0] + calories[1] + calories[2]
fmt.Println(top3)
}