-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday01-1.go
41 lines (38 loc) · 811 Bytes
/
day01-1.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
package main
import (
"bufio"
"fmt"
"os"
"strconv"
)
func main() {
// Read input file
file, err := os.Open("input.txt")
if err != nil {
fmt.Println("Error opening file")
}
defer file.Close()
scanner := bufio.NewScanner(file)
// Find maximum sum of calories
maxCalories := 0
currentElfCalories := 0
for scanner.Scan() {
// Get line from file
line := scanner.Text()
// Convert string to int
calories, _ := strconv.Atoi(line)
// Add calories to current elf
currentElfCalories += calories
// Check if current elf has more calories than the maximum
if currentElfCalories > maxCalories {
maxCalories = currentElfCalories
}
// check if line is empty
if line == "" {
// Start with new Elf
currentElfCalories = 0
}
}
// Print maxCalories
fmt.Println(maxCalories)
}