adventofcode/2019/day01/main.go

40 lines
633 B
Go
Raw Normal View History

2019-12-01 13:42:53 +00:00
package main
import (
"fmt"
"math"
2019-12-05 15:01:52 +00:00
helpers "git.bullercodeworks.com/brian/adventofcode/helpers"
2019-12-01 13:42:53 +00:00
)
func main() {
2019-12-05 15:01:52 +00:00
inp := helpers.StdinToStringSlice()
2019-12-01 13:42:53 +00:00
fmt.Println("Fuel Required:", part1(inp))
}
func part1(inp []string) int {
var total int
for _, v := range inp {
2019-12-05 15:01:52 +00:00
total = total + part2(fuelRequired(float64(helpers.Atoi(v))))
2019-12-01 13:42:53 +00:00
}
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
}