-
Notifications
You must be signed in to change notification settings - Fork 140
Expand file tree
/
Copy pathd1.go
More file actions
48 lines (40 loc) · 709 Bytes
/
d1.go
File metadata and controls
48 lines (40 loc) · 709 Bytes
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
package main
import (
"io/ioutil"
"strconv"
"strings"
)
func calcTotalFuel(mass int) int {
fuel := calcFuel(mass)
if fuel <= 0 {
return 0
}
return fuel + calcTotalFuel(fuel)
}
func calcFuel(mass int) int {
return (mass / 3) - 2
}
func castToInts(vs []string) []int {
arr := make([]int, len(vs))
for i, v := range vs {
casted, _ := strconv.Atoi(v)
arr[i] = casted
}
return arr
}
func main() {
content, _ := ioutil.ReadFile("./input.txt")
masses := castToInts(strings.Split(string(content), "\n"))
// p1
p1sum := 0
for _, v := range masses {
p1sum += calcFuel(v)
}
println(p1sum)
// p2
p2sum := 0
for _, v := range masses {
p2sum += calcTotalFuel(v)
}
println(p2sum)
}