-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.py
80 lines (67 loc) · 3.17 KB
/
Solution.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
import sys
import math
import numpy as np
class HoverCraftStatus:
def __init__(self, x=0, y=0, next_x=0, next_y=0,
chkpt_dist=0, chkpt_angle=0, used_boost=False):
self.update_status(x, y, next_x, next_y, chkpt_dist, chkpt_angle, used_boost)
def update_status(self, x, y, next_x, next_y, chkpt_dist, chkpt_angle, used_boost=None):
self.x = x
self.y = y
self.next_x = next_x
self.next_y = next_y
self.chkpt_dist = chkpt_dist
self.chkpt_angle = chkpt_angle
if used_boost is not None:
self.used_boost = used_boost
# angle and distance based discount of thrust for optimum path
# The greater the distance, the less we care about accelerating in the wrong direction
# The greater the angle, the less we accelerate to avoid going longer distance
angle_dist_thres = 4000.0
def compute_thrust(angle, distance):
abs_angle = np.abs(angle)
dist_multiplier = min(1.0, distance/angle_dist_thres)
angle_based_discount = float(abs_angle)/90.0
dist_adjusted_discount = max(0.0, angle_based_discount * dist_multiplier)
return int(100.0*(1-dist_adjusted_discount))
# avoid overshooting the target
dist_thres = 1500.0
def adjust_by_distance(in_thrust, distance):
high = 1.0
low = 0.3
dist_discount = max(min(high, (distance/dist_thres)**2), low)
return int(in_thrust * dist_discount)
# optimize boost calculation to a specific location
def get_boost_to_loc(x, y, next_checkpoint_x, next_checkpoint_y, next_checkpoint_dist, next_checkpoint_angle, used_boost):
if next_checkpoint_angle >= 90 or next_checkpoint_angle <= -90:
thrust = 0
else:
# thrust = 100
thrust = compute_thrust(next_checkpoint_angle, next_checkpoint_dist)
thrust = adjust_by_distance(thrust, next_checkpoint_dist)
if not used_boost and abs(next_checkpoint_angle) < 10 and next_checkpoint_dist > angle_dist_thres:
thrust = 'BOOST'
used_boost = True
return used_boost, thrust
# game loop
used_boost = False
my_status = HoverCraftStatus()
while True:
# next_checkpoint_x: x position of the next check point
# next_checkpoint_y: y position of the next check point
# next_checkpoint_dist: distance to the next checkpoint
# next_checkpoint_angle: angle between your pod orientation and the direction of the next checkpoint
x, y, next_checkpoint_x, next_checkpoint_y, next_checkpoint_dist, next_checkpoint_angle = [int(i) for i in input().split()]
opponent_x, opponent_y = [int(i) for i in input().split()]
# Write an action using print
# To debug: print >> sys.stderr, "Debug messages..."
my_status.update_status(x, y, next_checkpoint_x, next_checkpoint_y, next_checkpoint_dist, next_checkpoint_angle)
used_boost, thrust = get_boost_to_loc(
my_status.x, my_status.y, my_status.next_x, my_status.next_y,
my_status.chkpt_dist, my_status.chkpt_angle, my_status.used_boost)
my_status.used_boost = used_boost
# You have to output the target position
# followed by the power (0 <= thrust <= 100)
# i.e.: "x y thrust"
print(str(next_checkpoint_x) + " " + str(next_checkpoint_y) + " " + \
str(thrust))