-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
44 lines (35 loc) · 833 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
package main
import (
"log"
"strconv"
"github.com/sanderploegsma/advent-of-code/2019/go/utils"
)
func main() {
lines, _ := utils.ReadLines("input.txt")
log.Printf("[PART ONE] fuel requirements: %d", PartOne(lines))
log.Printf("[PART TWO] fuel requirements: %d", PartTwo(lines))
}
func PartOne(input []string) int {
fuel := 0
for _, module := range input {
mass, _ := strconv.Atoi(module)
fuel += mass/3 - 2
}
return fuel
}
func PartTwo(input []string) int {
fuel := 0
for _, module := range input {
mass, _ := strconv.Atoi(module)
fuel += CalculateFuel(mass)
}
return fuel
}
// CalculateFuel calculates the total fuel required for the given mass recursively until it reaches zero.
func CalculateFuel(mass int) int {
fuel := mass/3 - 2
if fuel < 0 {
return 0
}
return fuel + CalculateFuel(fuel)
}