58 lines
900 B
Go
58 lines
900 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
|
|
recursiveFuel := 0
|
|
|
|
scanner := bufio.NewScanner(file)
|
|
for scanner.Scan() {
|
|
txt := scanner.Text()
|
|
convert, err := strconv.ParseFloat(txt, 64)
|
|
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
|
|
completeFuel += CalcRequiredFuel(convert)
|
|
recursiveFuel += CalcFuelRecusive(convert)
|
|
}
|
|
|
|
if err := scanner.Err(); err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
|
|
log.Println("Part1: " + strconv.Itoa(completeFuel))
|
|
log.Println("Part2: " + strconv.Itoa(recursiveFuel))
|
|
}
|
|
|
|
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
|
|
}
|