-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
166 lines (152 loc) · 6.44 KB
/
main.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
import sys
current_time = 0
lines = 0
columns = 0
total_vehicles = 0
total_rides = 0
per_ride_bonus = 0
simulation_time = 0
ride_list = []
ride_dict = {}
# free_rides = []
vehicle_dict = {}
vehicle_list = []
free_vehicles = {}
all_rides_assigned = False
time_dict = {} # when its time to update a vehicle
class Ride:
def __init__(self, id, start_x, start_y, end_x, end_y, earliest_start, latest_finish):
self.id = id
self.start_x = start_x
self.start_y = start_y
self.end_x = end_x
self.end_y = end_y
self.earliest_start = earliest_start
self.latest_finish = latest_finish
self.distance = manhattan(self.start_x, self.start_y, self.end_x , self.end_y)
self.free = True
class Vehicle:
def __init__(self, id):
self.id = id
self.current_x = 0
self.current_y = 0
self.assigned_rides = [] # index of assigned rides
self.free = True
def manhattan(start_x, start_y, end_x, end_y):
return abs(start_x - end_x) + abs(start_y - end_y)
def score(input, vehicle, ride):
score = 0
distance_to_starting_point = manhattan(vehicle.current_x, vehicle.current_y, ride.start_x, ride.start_y)
ride_distance = ride.distance
starting_time = max(current_time + distance_to_starting_point, ride.earliest_start)
# starting_time = current_time + distance_to_starting_point
total_time = starting_time + ride_distance
if input == 'a':
if starting_time <= ride.earliest_start:
score += per_ride_bonus
if total_time < ride.latest_finish:
score += ride.distance
# waiting_time = max(current_time - ride.earliest_start, 0)
waiting_time = max(current_time + distance_to_starting_point - ride.earliest_start, 0)
return score - distance_to_starting_point # maybe subtract distance_to_starting_point*some_factor
elif input == 'b':
if starting_time <= ride.earliest_start:
score += per_ride_bonus
if total_time < ride.latest_finish:
score += ride.distance
# waiting_time = max(current_time - ride.earliest_start, 0)
waiting_time = max(current_time + distance_to_starting_point - ride.earliest_start, 0)
return score - distance_to_starting_point # maybe subtract distance_to_starting_point*some_factor
elif input == 'c':
if starting_time <= ride.earliest_start:
score += per_ride_bonus
if total_time < ride.latest_finish:
score += ride.distance
# waiting_time = max(current_time - ride.earliest_start, 0)
waiting_time = max(current_time + distance_to_starting_point - ride.earliest_start, 0)
return score - distance_to_starting_point # maybe subtract distance_to_starting_point*some_factor
elif input == 'd':
if starting_time <= ride.earliest_start:
score += per_ride_bonus
if total_time < ride.latest_finish:
score += ride.distance
# waiting_time = max(current_time - ride.earliest_start, 0)
waiting_time = max(current_time + distance_to_starting_point - ride.earliest_start, 0)
return score - distance_to_starting_point # maybe subtract distance_to_starting_point*some_factor
elif input == 'e':
if starting_time <= ride.earliest_start:
score += per_ride_bonus
if total_time < ride.latest_finish:
score += ride.distance
# waiting_time = max(current_time - ride.earliest_start, 0)
waiting_time = max(current_time + distance_to_starting_point - ride.earliest_start, 0)
return score - distance_to_starting_point # maybe subtract distance_to_starting_point*some_factor
def main(input):
global lines
global columns
global total_vehicles
global total_rides
global per_ride_bonus
global simulation_time
global ride_list
global ride_dict
global vehicle_list
global vehicle_dict
global free_vehicles
global all_rides_assigned
with open('./inputs/'+input+'.in', 'r') as inputFile:
header = next(inputFile).split()
lines = int(header[0])
columns = int(header[1])
total_vehicles = int(header[2])
total_rides = int(header[3])
per_ride_bonus = int(header[4])
simulation_time = int(header[5])
for ride in range(total_rides):
line = next(inputFile).split()
ride_dict[ride] = Ride(ride, int(line[0]),int(line[1]),int(line[2]),int(line[3]),int(line[4]),int(line[5]))
ride_list.append(Ride(ride, int(line[0]),int(line[1]),int(line[2]),int(line[3]),int(line[4]),int(line[5])))
vehicle_list = [Vehicle(v) for v in range(total_vehicles)]
for v in range(total_vehicles):
vehicle_dict[v] = Vehicle(v)
free_vehicles[v] = True
for current_time in range(simulation_time):
if current_time in time_dict: # if its time to update a vehicle
for vehicle in time_dict[current_time]:
vehicle.free = True
last_ride = vehicle.assigned_rides[-1]
vehicle.current_x = last_ride.end_x
vehicle.current_y = last_ride.end_y
if all_rides_assigned:
break
for vehicle in vehicle_list:
scores = []
if vehicle.free == False:
continue
for ride in ride_list:
if ride.free == False:
continue
scores.append([ride, score(input, vehicle, ride)])
if len(scores) == 0: # No free rides left
all_rides_assigned = True
break
best_ride = max(scores, key=lambda x: x[1])[0]
best_ride.free = False
vehicle.assigned_rides.append(best_ride)
vehicle.free = False
distance_to_starting_point = manhattan(vehicle.current_x, vehicle.current_y, ride.start_x, ride.start_y)
ending_time = current_time + distance_to_starting_point + ride.distance
if ending_time in time_dict:
time_dict[ending_time].append(vehicle)
else:
time_dict[ending_time] = [vehicle]
def output(input):
with open('outputs/'+input+'.out', 'w') as outputFile:
for vehicle in vehicle_list:
rides = ''
for ride in vehicle.assigned_rides:
rides += ' ' + str(ride.id)
outputFile.write(str(len(vehicle.assigned_rides)) + rides + '\n' )
if __name__ == '__main__':
main(sys.argv[1])
output(sys.argv[1])