60 lines
910 B
Go
60 lines
910 B
Go
|
package main
|
||
|
|
||
|
import (
|
||
|
"bufio"
|
||
|
"fmt"
|
||
|
"log"
|
||
|
"math"
|
||
|
"os"
|
||
|
"strconv"
|
||
|
)
|
||
|
|
||
|
func main() {
|
||
|
inp := StdinToStringSlice()
|
||
|
fmt.Println("Fuel Required:", part1(inp))
|
||
|
}
|
||
|
|
||
|
func part1(inp []string) int {
|
||
|
var total int
|
||
|
for _, v := range inp {
|
||
|
total = total + part2(fuelRequired(float64(Atoi(v))))
|
||
|
}
|
||
|
return total
|
||
|
}
|
||
|
|
||
|
func part2(fuel int) int {
|
||
|
ret := fuel
|
||
|
for fuel > 0 {
|
||
|
fuel = fuelRequired(float64(fuel))
|
||
|
fmt.Println(fuel)
|
||
|
ret = ret + fuel
|
||
|
}
|
||
|
return ret
|
||
|
}
|
||
|
|
||
|
func fuelRequired(mass float64) int {
|
||
|
res, _ := math.Modf(mass / 3)
|
||
|
if int(res)-2 < 0 {
|
||
|
return 0
|
||
|
}
|
||
|
return int(res) - 2
|
||
|
}
|
||
|
|
||
|
func StdinToStringSlice() []string {
|
||
|
var input []string
|
||
|
scanner := bufio.NewScanner(os.Stdin)
|
||
|
for scanner.Scan() {
|
||
|
input = append(input, scanner.Text())
|
||
|
}
|
||
|
return input
|
||
|
}
|
||
|
|
||
|
func Atoi(i string) int {
|
||
|
var ret int
|
||
|
var err error
|
||
|
if ret, err = strconv.Atoi(i); err != nil {
|
||
|
log.Fatal("Invalid Atoi")
|
||
|
}
|
||
|
return ret
|
||
|
}
|