-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday01.go
63 lines (55 loc) · 899 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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package main
import (
"fmt"
"io/ioutil"
"strconv"
"strings"
)
func check(err error) {
if err != nil {
panic(err)
}
}
func calculateFuel(mass int) int {
return mass/3 - 2
}
func max(a int, b int) int {
if a > b {
return a
}
return b
}
func a(masses []int) int {
total := 0
for _, mass := range masses {
total += calculateFuel(mass)
}
return total
}
func b(masses []int) int {
total := 0
for _, mass := range masses {
fuel := calculateFuel(mass)
newFuel := fuel
for true {
newFuel = max(calculateFuel(newFuel), 0)
if newFuel == 0 {
break
}
fuel += newFuel
}
total += fuel
}
return total
}
func main() {
text, err := ioutil.ReadFile("input01.txt")
check(err)
lines := strings.Split(string(text), "\n")
masses := make([]int, len(lines))
for i, v := range lines {
masses[i], err = strconv.Atoi(v)
check(err)
}
fmt.Println(b(masses))
}