-
Notifications
You must be signed in to change notification settings - Fork 0
/
Main.py
125 lines (104 loc) · 3.71 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
import math
def distance(x0, x1, y0, y1):
return math.fabs(x0 - x1) + math.fabs(y0 - y1)
class Ride(object):
def __init__(self, a, b, x, y, s, f, id):
"""
:param a: the row of the start intersection (0 ≤ a < R)
:param b: the column of the start intersection (0 ≤ b < C )
:param x: the row of the finish intersection (0 ≤ x < R)
:param y: the column of the finish intersection (0 ≤ y < C )
:param s: the earliest start (0 ≤ s < T)
:param f: the latest finish (0 ≤ f ≤ T), (f ≥ s + |x − a| + |y − b|)
○ note that f can be equal to T – this makes the latest finish equal to the end of the simulation
:return:
"""
self.a = a
self.b = b
self.x = x
self.y = y
self.s = s
self.f = f
self.id = id
self.distance = distance(a, x, b, y)
self.s_latest = self.f - self.distance - 1 # latest time at which you can leave and still arrive in time
class Car(object):
def __init__(self, x, y, id):
self.x = x
self.y = y
self.id = id
def read_file(filename):
"""
:param filename:
:var R: number of rows of the grid (1≤R≤10000)
:var C: number of columns of the grid (1 ≤ C ≤ 10000)
:var F: number of vehicles in the fleet (1 ≤ F ≤ 1000)
:var N: number of rides (1≤N ≤10000)
:var B: per-ride bonus for starting the ride on time (1 ≤ B ≤ 10000)
:var T: number of steps in the simulation (1 ≤ T ≤ 109)
:return: Nothing
"""
f = open(filename)
global R, C, F, N, B, T
R, C, F, N, B, T = map(int, f.readline().strip().split())
global rides
rides = []
for i in range(N):
# ugly but it works
arr = list(map(int, f.readline().strip().split()))
rides.append(Ride(arr[0], arr[1], arr[2], arr[3], arr[4], arr[5], i))
rides.sort(key=lambda ride: ride.s) # sort rides with earliest start time first
global cars
cars = []
for i in range(F):
cars.append(Car(0, 0, i))
f.close()
def main():
files = ["Problem/a_example.in",
"Problem/b_should_be_easy.in",
"Problem/c_no_hurry.in",
"Problem/d_metropolis.in",
"Problem/e_high_bonus.in"]
read_file(files[0])
# # For testing all the files
# for file in files:
# read_file(file)
# def rewrite_line(nums, num_to_remove):
# line = ''
# nums.remove(num_to_remove)
# for num in nums:
# line += ' ' + num
# return line
#
#
# def is_valid_file(submission_array):
# """submission_array is of the form ['vehicle1 ride1 ride2... rideN',...,'vehicleN ride1... rideN']"""
# assigned_rides = []
# if len(submission_array) != F:
# return False
# for i in range(F):
# nums = submission_array[i].split()
# for x in range(1, len(nums)):
# if nums[x] in assigned_rides:
# submission_array[i] = str(i+1) + ' ' + rewrite_line(nums, nums[x])
# continue
# assigned_rides.append(nums[x])
# return submission_array
def write_output(data):
"""
:param data: a list of space-deliminated arrays: ["{M} {R0} {R1} {R2} etc", "{M} {R0} {R1} {R2} etc", etc]
● M:
number of rides assigned to the vehicle (0 ≤ M ≤ N)
● R0, R1, ..., RM-1:
ride numbers assigned to the vehicle, in the order in which the vehicle will performthem (0≤Ri <N)
:return: nothing
"""
output_file = open("output.txt", "w+")
output = ""
for item in data:
output += item + "\n"
output_file.write(output)
output_file.close()
#test
if __name__ == '__main__':
main()