2019/day1/main.go
2019-12-02 19:27:03 +01:00

55 lines
755 B
Go

package main
import (
"bufio"
"log"
"os"
"strconv"
)
func main() {
file, err := os.Open("input.txt")
if err != nil {
log.Fatal(err)
}
defer file.Close()
completeFuel := 0
scanner := bufio.NewScanner(file)
for scanner.Scan() {
txt := scanner.Text()
convert, err := strconv.ParseFloat(txt, 64)
if err != nil {
log.Fatal(err)
}
completeFuel += CalcFuelRecusive(convert)
}
if err := scanner.Err(); err != nil {
log.Fatal(err)
}
log.Println(completeFuel)
}
func CalcFuelRecusive(mass float64) int {
sum := 0
for mass > 0 {
fuel := CalcRequiredFuel(mass)
if fuel > 0 {
sum += fuel
}
mass = float64(fuel)
}
return sum
}
func CalcRequiredFuel(mass float64) int {
fuel := mass / 3
return int(fuel) - 2
}