2019-12-02 18:27:03 +00:00
|
|
|
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
|
2019-12-02 18:28:59 +00:00
|
|
|
recursiveFuel := 0
|
2019-12-02 18:27:03 +00:00
|
|
|
|
|
|
|
scanner := bufio.NewScanner(file)
|
|
|
|
for scanner.Scan() {
|
|
|
|
txt := scanner.Text()
|
|
|
|
convert, err := strconv.ParseFloat(txt, 64)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2019-12-02 18:28:59 +00:00
|
|
|
completeFuel += CalcRequiredFuel(convert)
|
|
|
|
recursiveFuel += CalcFuelRecusive(convert)
|
2019-12-02 18:27:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := scanner.Err(); err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2019-12-02 18:28:59 +00:00
|
|
|
log.Println("Part1: " + strconv.Itoa(completeFuel))
|
|
|
|
log.Println("Part2: " + strconv.Itoa(recursiveFuel))
|
2019-12-02 18:27:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|