/
Mars_Lander_episode_2.py
127 lines (102 loc) · 4.27 KB
/
Mars_Lander_episode_2.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
import sys
import math
# Auto-generated code below aims at helping you parse
# the standard input according to the problem statement.
surface_n = int(input()) # the number of points used to draw the surface of Mars.
surface_x, surface_y = [], []
mars_gravity = 3.711
vertical_max_speed = 40
horizontal_max_speed = 20
margin_for_error_y = 20
margin_for_speed = 5
map_thrust = {
0: 0,
1: 1,
2: 2,
3: 3,
4: 4
}
# to get the location to land.
# it will check where the land is flat
def move_to_location(pos_x, pos_y):
perv_x = -1
prev_y = -1
for i in range(0, surface_n):
if pos_y[i] == prev_y:
# checking if the landing space is at least 1000
if abs(prev_x - pos_x[i]) >= 1000:
return [prev_x, prev_y, pos_x[i], pos_y[i]]
else:
prev_y, prev_x = pos_y[i], pos_x[i]
else:
prev_y, prev_x = pos_y[i], pos_x[i]
# this checks if the mars lander is in the landing site.
def ship_over_location(current_x, landing_site):
return landing_site[0] < current_x < landing_site[2]
# this is to check if the mars lander is nearing the end.
def check_nearing_landing(current_y, landing_site):
return current_y < landing_site[1] + margin_for_error_y
# check if speed is safe
def check_if_speed_safe(current_speed_x, current_speed_y):
return abs(current_speed_x) <= horizontal_max_speed - margin_for_speed and abs(current_speed_y) <= vertical_max_speed - margin_for_speed
# check if mars lander is moving in wrong direction
def check_direction(current_x, current_hor_speed, landing_site):
return (current_x < landing_site[0] and current_hor_speed < 0) or (landing_site[2] < current_x and current_hor_speed > 0)
# check if going horizontal too fast
def check_horizontal_speed(current_hor_speed, type):
if type == "FAST":
return abs(current_hor_speed) > 4 * horizontal_max_speed
elif type == "SLOW":
return abs(current_hor_speed) < 2 * horizontal_max_speed
# return angle to move to target
def angle_to_target(current_x, landing_site):
angle = int(math.degrees(math.acos(mars_gravity / 4.0)))
if current_x < landing_site[0]:
return -angle
elif landing_site[2] < current_x:
return angle
else:
return 0
# returns power to hover
def hover(current_speed_y):
if current_speed_y >= 0:
return 3
else:
return 4
# return best angle to slow down
def angle_to_slow_down(current_speed_x, current_speed_y):
speed = math.sqrt((current_speed_x * current_speed_x) + (current_speed_y * current_speed_y))
return int(math.degrees(math.asin(current_speed_x / speed)))
for i in range(surface_n):
# land_x: X coordinate of a surface point. (0 to 6999)
# land_y: Y coordinate of a surface point. By linking all the points together in a sequential fashion, you form the surface of Mars.
land_x, land_y = [int(j) for j in input().split()]
surface_x.append(land_x)
surface_y.append(land_y)
# game loop
while True:
# h_speed: the horizontal speed (in m/s), can be negative.
# v_speed: the vertical speed (in m/s), can be negative.
# fuel: the quantity of remaining fuel in liters.
# rotate: the rotation angle in degrees (-90 to 90).
# power: the thrust power (0 to 4).
x, y, h_speed, v_speed, fuel, rotate, power = [int(i) for i in input().split()]
# Write an action using print
# To debug: print >> sys.stderr, "Debug messages..."
# rotate power. rotate is the desired rotation angle. power is the desired thrust power.
# if ship not over location
correct_site = move_to_location(surface_x, surface_y)
if not ship_over_location(x, correct_site):
if (check_direction(x, h_speed, correct_site)) or (check_horizontal_speed(h_speed, "FAST")):
print("%s %s" % (angle_to_slow_down(h_speed, v_speed), map_thrust.get(4)))
elif check_horizontal_speed(h_speed, "SLOW"):
print("%s %s" % (angle_to_target(x, correct_site), map_thrust.get(4)))
else:
print("%s %s" % (map_thrust.get(0), hover(v_speed)))
else:
if check_nearing_landing(y, correct_site):
print("0 3")
elif check_if_speed_safe(h_speed, v_speed):
print("0 2")
else:
print("%s 4" % angle_to_slow_down(h_speed, v_speed))