feat: navigation computer for Moon and Mars
This commit is contained in:
parent
122848a883
commit
ed76e8c409
1 changed files with 67 additions and 0 deletions
67
nav.py
Normal file
67
nav.py
Normal file
|
|
@ -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()
|
||||
Loading…
Reference in a new issue