-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
61 lines (55 loc) · 1000 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
53
54
55
56
57
58
59
60
61
package main
import (
"fmt"
"io/ioutil"
"math"
"strconv"
"strings"
)
func main() {
step1()
step2()
}
func step1() {
input := load()
sumOfFuel := 0
for _, line := range strings.Split(input, "\n") {
mass, err := strconv.Atoi(line)
if err != nil {
fmt.Println(err)
return
}
fuel := findFuelRequirement(mass)
sumOfFuel += fuel
}
fmt.Println("step 1 -->", sumOfFuel)
}
func step2() {
input := load()
sumOfFuel := 0
for _, line := range strings.Split(input, "\n") {
mass, err := strconv.Atoi(line)
if err != nil {
fmt.Println(err)
return
}
for mass > 0 {
fuelRequirement := findFuelRequirement(mass)
if fuelRequirement > 0 {
sumOfFuel += fuelRequirement
}
mass = fuelRequirement
}
}
fmt.Println("step 2 -->", sumOfFuel)
}
func findFuelRequirement(mass int) int {
return int(math.Floor(float64(mass/3)) - 2)
}
func load() string {
text, err := ioutil.ReadFile("./data.txt")
if err != nil {
fmt.Print(err)
}
return string(text)
}