-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathinput_parser.py
More file actions
151 lines (120 loc) · 4.31 KB
/
Copy pathinput_parser.py
File metadata and controls
151 lines (120 loc) · 4.31 KB
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
def offset_pos(pos1, pos2):
sum = 0
sum += abs(pos2[0] - pos1[0])
sum += abs(pos2[1] - pos1[0])
return sum
class Map(object):
def __init__(self, rows, columns, vehicles):
self.columns = columns
self.rows = rows
self.vehicles = vehicles
def get_vehicles(self, x, y):
"""Get the vehicles at this intersection."""
pass
def get_rows(self):
"""Get the total city rows."""
return self.rows
def get_columns(self):
"""Get the total city columns."""
return self.columns
class Ride(object):
def __init__(self, start, end, time_start, time_end):
self.step = 0
self.remaining_time = time_end - time_start
self.start = start
self.end = end
self.time_start = time_start
self.time_end = time_end
def get_start(self):
"""Get the start intersection for this ride."""
return self.start
def get_end(self):
"""Get the end intersection for this ride."""
return self.end
def get_time_start(self):
"""Get the start time for this ride."""
return self.time_start
def get_time_end(self):
"""Get the end time step for this ride."""
return self.time_end
def get_remaining_time(self):
"""Get the remaining time for this ride."""
return self.remaining_time
def _update(self):
self.step = self.step + 1
if self.step >= self.time_start:
self.remaining_time = self.remaining_time - 1
def __str__(self):
return ("From {0} to {1}, starting at {2} and lasting at {3}".format(
self.start,
self.end,
self.time_start,
self.time_end
))
class Vehicle(object):
def __init__(self):
self.position = [0, 0]
self.next_position = [0, 0]
self.rides = []
def get_intersection(self):
"""Get the current vehicle intersection."""
return self.position
def move(self, x, y):
"""Move the vehicle to the corresponding case nex step."""
if self.position[0] - x == -1 and self.position[0] - x == 1:
if self.position[0] - x == -1 and self.position[0] - x == 1:
self.next_position = [x, y]
def take_ride(self, ride):
self.rides.append(ride)
self.rides.sort(key=lambda v: v.time_start)
def _update(self):
self.position = self.next_position
def can_handle_this(self, ride):
for r, i in zip(self.rides, range(len(self.rides))):
offset_start = ride.time_end - r.time_end - offset_pos(ride.start, ride.end)
if i != len(self.rides) - 1:
offset_end = self.rides[i + 1] - ride.time_end
else:
offset_end = 1
if offset_start <= 0 and offset_end <= 0:
continue
try:
if offset_pos(r.end, ride.start) < ride.time_end - r.time_end and offset_pos(self.rides[i + 1], ride.end) < offset_end:
return 1
except IndexError:
return 1
return 0
class Input(object):
def __init__(self, file):
self.file = file
lines = self.file.split('\n')
lines.pop(len(lines) - 1)
values = [int(k) for k in lines[0].split(' ')]
self.vehicles = []
for i in range(values[2]):
self.vehicles.append(Vehicle())
self.rides = []
for k, v in zip(lines, range(len(lines))):
if v != 0 and v != len(lines):
values = [int(i) for i in k.split(' ')]
self.rides.append(Ride(
[values[0], values[1]],
[values[2], values[3]],
values[4],
values[5]
))
self.rides.sort(key=lambda v: v.time_start)
self.steps = values[5]
self.map = Map(values[0], values[1], self.vehicles)
def get_map(self):
"""Get the map object from the file."""
return self.map
def get_vehicles(self):
"""Get the list containing the Vehicles objects."""
return self.vehicles
def get_rides(self):
"""Get the list containing the rides objects."""
return self.rides
def get_steps(self):
"""Get the total number of steps."""
return self.steps