-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
331 lines (294 loc) · 7.48 KB
/
main.cpp
File metadata and controls
331 lines (294 loc) · 7.48 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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
#include <iostream>
#include <fstream>
#include <string>
#include <queue>
#include <vector>
#include <utility>
#include <map>
#include <bits/stdc++.h>
#include <stdlib.h> /* srand, rand */
#include <time.h>
using namespace std;
const bool DEBUG_MODE = false;
int N_TIME;
int N_INTERSECTIONS;
int N_STREETS;
int N_CARS;
int POINTS_PER_CAR;
int RES_INTERSECTIONS = 3;
int RES_IDSINTERSECTIONS[3] = {0,1,2};
int RES_STREETSCOUNT[3] = {1,2,1}; // Incoming streets
string RES_STREETNAMES[4] = {"rue-de-londres","rue-d-amsterdam","rue-d-athenes","rue-de-moscou"};
int RES_GREENTIME[4] = {1,1,1,1};
class car
{
public:
vector<string> path;
car() {}
car(vector<string> path)
{
this->path = path;
}
};
class street
{
public:
string name;
queue< pair<car, int> > cars;
int time;
int TrafficLightTime;
bool TrafficLightStatus;
street(string name, int time)
{
this->name = name;
this->time = time;
this->TrafficLightStatus = false;
}
void addCar(car car, int time = 0)
{
cars.push(make_pair(car, time));
}
};
class intersection
{
public:
vector<street> begin;
vector<street> end;
int timeRemaining;
int greenLightStreet;
intersection()
{
}
void setupTrafficLight()
{
if (this->end.size() == 1){
this->end.at(0).TrafficLightTime = -1;
this->end.at(0).TrafficLightStatus = true;
timeRemaining=-1;
greenLightStreet=0;
} else{
for(auto street: end)
street.TrafficLightTime = 1;
}
}
};
vector<street> streets;
vector<car> cars;
vector<intersection> intersections;
bool readSetup()
{
ifstream arq;
arq.open("file.txt");
char linha[5000];
if (!arq.is_open())
{
cout << "Could not read setup file";
return false;
}
else
{
string numBuffer = "";
int pos = 0;
//read parameters from first line
{
arq.getline(linha, 5000);
while (linha[pos] != ' ')
{
numBuffer += linha[pos];
pos++;
}
N_TIME = stoi(numBuffer);
numBuffer = "";
pos++;
while (linha[pos] != ' ')
{
numBuffer += linha[pos];
pos++;
}
N_INTERSECTIONS = stoi(numBuffer);
pos++;
numBuffer = "";
while (linha[pos] != ' ')
{
numBuffer += linha[pos];
pos++;
}
N_STREETS = stoi(numBuffer);
pos++;
numBuffer = "";
while (linha[pos] != ' ')
{
numBuffer += linha[pos];
pos++;
}
N_CARS = stoi(numBuffer);
pos++;
numBuffer = "";
while (linha[pos] != ' ')
{
numBuffer += linha[pos];
pos++;
}
POINTS_PER_CAR = stoi(numBuffer);
pos++;
numBuffer = "";
}
intersections = vector<intersection>(N_INTERSECTIONS);
int no1 = 0, no2 = 0;
for (int i = 0; i < N_STREETS; i++)
{
arq.getline(linha, 5000);
pos = 0;
numBuffer = "";
while (linha[pos] != ' ')
{
numBuffer += linha[pos];
pos++;
}
no1 = stoi(numBuffer);
numBuffer = "";
pos++;
while (linha[pos] != ' ')
{
numBuffer += linha[pos];
pos++;
}
no2 = stoi(numBuffer);
numBuffer = "";
pos++;
while (linha[pos] != ' ')
{
numBuffer += linha[pos];
pos++;
}
string nome = numBuffer;
numBuffer = "";
pos++;
while (linha[pos] != ' ')
{
numBuffer += linha[pos];
pos++;
}
int tam = stoi(numBuffer);
street newStreet(nome, tam);
intersections[no1].begin.push_back(newStreet);
intersections[no2].end.push_back(newStreet);
streets.push_back(newStreet);
}
for (int i = 0; i < N_CARS; i++)
{
arq.getline(linha, 5000);
car newCar;
pos = 0;
numBuffer = "";
while (linha[pos] != ' ')
{
numBuffer += linha[pos];
pos++;
}
int streetsNum = stoi(numBuffer);
pos++;
string path = "";
for (int j = 0; j < streetsNum; j++)
{
while (linha[pos] != ' ')
{
path += linha[pos];
pos++;
}
newCar.path.push_back(path);
}
cars.push_back(newCar);
}
return true;
}
}
map<string,long> pontos;
void solve()
{
for(car carro : cars){
for(auto street : carro.path){
if(pontos.find(street)==pontos.end()){
pontos[street]=1;
}
else{
pontos[street]++;
}
}
}
}
bool cmp(pair<string, long>& a,
pair<string, long>& b)
{
return a.second > b.second;
}
void sort(map<string, long>& M, ofstream& arq)
{
vector<pair<string, long> > A;
for (auto& it : M) {
A.push_back(it);
}
sort(A.begin(), A.end(), cmp);
int i = A.size() ;
for (auto& it : A) {
arq <<it.first << " "<< ((int)i/10 + 1) << "\n";
i--;
}
}
void save()
{
ofstream arq;
arq.open("solve.txt");
arq << N_INTERSECTIONS<<"\n"; //the number of intersections for which you specify the schedule.
for (int i = 0; i < N_INTERSECTIONS; i++)
{
arq << i << "\n"; //the ID of the intersection
arq << intersections[i].end.size() << "\n"; // the number of incoming streets
map<string,long> ordenado;
for (int j = 0; j < intersections[i].end.size(); j++) //indice do streetscount
{
//ordenado[intersections[i].end[j].name] = pontos[intersections[i].end[j].name];
srand (time(NULL));
arq << intersections[i].end[j].name << " " << rand() % 10 + 1 << "\n";
// the street name, how long each street will have a green light
}
//sort(ordenado, arq);
}
arq.close();
}
int main()
{
//Exemplo de mexer em arquivo
readSetup();
if (DEBUG_MODE)
{
cout << "\nN_TIME: " << N_TIME;
cout << "\nN_INTERSECTIONS: " << N_INTERSECTIONS;
cout << "\nN_STREETS: " << N_STREETS;
cout << "\nN_CARS: " << N_CARS;
cout << "\nPOINTS_PER_CAR " << POINTS_PER_CAR;
cout<<"\n\nIntersections: \n";
for(int i =0;i<N_INTERSECTIONS;i++){
cout<<"Intersection "<<i<<": \n";
cout<<"Streers starting here: \n";
for(street x: intersections[i].begin){
cout<<x.name<<endl;
}
cout<<"Streets ending here: \n";
for(street x: intersections[i].end){
cout<<x.name<<endl;
}
}
}
save();
return 0;
}
/*
Class car:
path
Class street
CarsQueue
CrossingTime
TrafficlightTime
TrafficlightStatus
*/