diff --git a/nav.py b/nav.py new file mode 100644 index 0000000..ca03c84 --- /dev/null +++ b/nav.py @@ -0,0 +1,67 @@ +#!/usr/bin/env python3 +"""Navigation computer of the SS Probably Fine. + +Usage: python3 nav.py DESTINATION +""" + +import sys + +# Distances between neighboring waypoints, in million km. +DISTANCES = { + ("earth", "moon"): 0.4, + ("earth", "gateway"): 0.4, + ("gateway", "mars"): 225.0, +} + +# Every route starts at Earth. +ROUTES = { + "moon": ["earth", "moon"], + "mars": ["earth", "gateway", "mars"], +} + +NAMES = { + "earth": "Earth", + "moon": "Moon", + "gateway": "Lunar Gateway", + "mars": "Mars", +} + +CRUISE_SPEED = 0.87 # million km per day +FUEL_PER_MKM = 0.142 # tonnes of fuel per million km +LAUNCH_FUEL = 380.0 # tonnes of fuel to escape Earth's gravity + + +def distance(a, b): + if (a, b) in DISTANCES: + return DISTANCES[(a, b)] + return DISTANCES[(b, a)] + + +def legs(route): + return [(route[i], route[i + 1]) for i in range(len(route) - 1)] + + +def plan(destination): + route = ROUTES[destination] + total = 0.0 + fuel = 0.0 + for start, end in legs(route): + d = distance(start, end) + total += d + fuel += d * FUEL_PER_MKM + if start == "earth": + fuel += LAUNCH_FUEL + return route, total, total / CRUISE_SPEED, fuel + + +def main(): + destination = sys.argv[1] + route, total, days, fuel = plan(destination) + print("Route: " + " -> ".join(NAMES[w] for w in route)) + print(f"Distance: {total:.1f} million km") + print(f"Travel time: {round(days)} days") + print(f"Fuel required: {round(fuel)} t") + + +if __name__ == "__main__": + main()