-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathday01.go
46 lines (43 loc) · 777 Bytes
/
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
package main
import (
"flag"
"fmt"
"io/ioutil"
"strconv"
"strings"
)
var inputFile = flag.String("inputFile", "inputs/day01.input", "Relative file path to use as input.")
var partB = flag.Bool("partB", false, "Whether to use the Part B logic.")
func main() {
flag.Parse()
bytes, err := ioutil.ReadFile(*inputFile)
if err != nil {
return
}
contents := string(bytes)
sum := 0
split := strings.Split(contents, "\n")
for _, s := range split {
if s == "" {
continue
}
n, err := strconv.Atoi(s)
if err != nil {
fmt.Printf("Failed to parse %s\n", s)
break
}
fuel := (n / 3) - 2
if fuel > 0 {
sum += fuel
}
if *partB {
for fuel >= 1 {
fuel = (fuel / 3) - 2
if fuel > 0 {
sum += fuel
}
}
}
}
fmt.Println(sum)
}