-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
37 lines (31 loc) · 810 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
// https://adventofcode.com/2019/day/1
// --- Day 1: The Tyranny of the Rocket Equation ---
package main
import (
"fmt"
"github.com/CZero/gofuncy/lfs"
)
func main() {
input, err := lfs.ReadLines("input.txt")
if err != nil {
panic(err)
}
fmt.Printf("Fuel needed: %d\n", CalcSumFuel(input))
}
// CalcSumFuel calculates the sum of fuel needed for all modules
func CalcSumFuel(input []string) int {
var sum int // Total fuel needed for the modules and their fuel
for _, line := range input {
sum += CalcFuelNeed(lfs.SilentAtoi(line))
}
return sum
}
// CalcFuelNeed calculates the fuel needed for a module, recursively calculating the fuel needed for the fuel too.
func CalcFuelNeed(mass int) int {
need := mass/3 - 2
if need > 0 {
return need + CalcFuelNeed(need)
} else {
return 0
}
}