-
Notifications
You must be signed in to change notification settings - Fork 0
/
analyse_hide_and_seek
130 lines (102 loc) · 3.63 KB
/
analyse_hide_and_seek
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
128
129
#!/usr/bin/python3
# -*- coding: utf-8 -*-
"""Analyse an attempt at the Hide and Seek.
This is the simplest challenge to analyse. Boats go to waypoints A, B, C, A.
They reach a marker by getting within RADIUS metres of it.
Boats that complete more of the course beat boats that complete less of it.
For boats that reach the same number of waypoints, the duration
from their start times determines the winner.
Bonus or penalty times may adjust this.
Usage:
./analyse_fleet_race attempt.csv
"""
import csv
import sys
from pyproj import Proj
import math
import datetime
# Your UTM zone depends on your longitude; see http://www.dmap.co.uk/utmworld.htm
utm_zone = 51
projection = Proj(proj='utm', zone=utm_zone, ellps='WGS84')
def latlon_to_utm(lat, lon):
"""Returns (x, y) coordinates in metres"""
return projection(lon, lat)
# CSV values
# ISO 8601
# Lat
# Lon
#
# y^
# |
# +-> x
# A ------------> B
#
#
# B ------------> A
wpA = (29.863003, 121.542088)
wpB = (29.863291, 121.541788)
RADIUS = 5 #acceptance radius in m
#######################################################################################
# Convert all coordinates to UTM, so we can work with metres on a flat plane
wpA_utm = latlon_to_utm(wpA[0], wpA[1])
wpB_utm = latlon_to_utm(wpB[0], wpB[1])
def dist2wp(M, wp):
"""Direct distance between two points in metres"""
return math.sqrt((M[0]-wp[0])**2 + (M[1] - wp[1])**2)
def isWpReached(M, wp):
return dist2wp(M, wp) < RADIUS
def is_csv(file_name):
return file_name.endswith(".csv")
def analyse_hide_and_seek(csv_file, path, output):
positions_utm = []
with open(path) as logfile:
data = csv.reader(logfile, delimiter=',')
for row in list(data)[1:]:
time = datetime.datetime.strptime(row[0], "%Y-%m-%dT%H:%M:%SZ")
latitude = float(row[1])
longitude = float(row[2])
positions_utm.append((time, latlon_to_utm(latitude, longitude)))
starttime = 0
WPA_REACHED = False
WPB_REACHED = False
hide_time = 0
HIDE = 0
Mode = '0' # 0: A/B, A: TOWARDS A, B: TOWARDS B
line = '' + csv_file + ','
for timestamp, point in positions_utm:
if Mode == '0' and isWpReached(point, wpA_utm):
# or isWpReached(point, wpA_utm)):
Mode = 'B'
print("WPA reached at", timestamp)
start_time = timestamp
line = line + ',' + 'A' + ',' + timestamp
if Mode == '0' and isWpReached(point, wpB_utm):
# or isWpReached(point, wpA_utm)):
Mode = 'A'
print("WPB reached at", timestamp)
start_time = timestamp
line = line + ',' + 'B' + ',' + timestamp
if Mode == 'A' and isWpReached(point, wpA_utm):
# or isWpReached(point, wpA_utm)):
Mode = 'B'
print("WPA reached at", timestamp)
start_time = timestamp
line = line + ',' + 'A' + ',' + timestamp
hide_time += 1
if Mode == 'B' and isWpReached(point, wpB_utm):
# or isWpReached(point, wpA_utm)):
Mode = 'A'
print("WPB reached at", timestamp)
start_time = timestamp
line = line + ',' + 'B' + ',' + timestamp
hide_time += 1
line = str(hide_time) + line + '\n'
output.write(line)
if __name__ == "__main__":
path = './data/North/PM'
# csv_file = sys.argv[1]
out_file = open("day4_data_N_PM_B.csv", 'w')
for _, _, files in os.walk(path):
for file in files:
if is_csv(file):
get_score(file, os.path.join(path, file), out_file)