-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathP161.cpp
56 lines (54 loc) · 1.24 KB
/
P161.cpp
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
#include <iostream>
#include <stdio.h>
int main() {
int signals[100];
while(true) {
// read signals:
int numSignals = 0;
int minSignal = 9000;
while(true) {
int signal;
std::cin >> signal;
if(signal == 0)
break;
signals[numSignals] = signal;
++numSignals;
if(signal < minSignal)
minSignal = signal;
}
if(numSignals == 0)
return 0;
// simulate:
int okk = false;
for(int time = 2*minSignal; time <= 5*60*60;) {
//Check if all green:
bool ok = true;
for(int i = 0; i < numSignals; ++i) {
int in = time % (2*signals[i]);
if(in >= signals[i]-5) {
//std::cerr << i << " not green at time " << time << std::endl;
ok = false;
break;
}
}
if(ok) {
//std::cerr << "All green at time " << time << std::endl;
printf("%02i:%02i:%02i\n", time / 3600, (time / 60) % 60, time % 60);
okk = true;
break;
}
// Find next green:
int toNextGreen = 90;
for(int i = 0; i < numSignals; ++i) {
int in = (2*signals[i]) - time % (2*signals[i]);
if(in < toNextGreen) {
toNextGreen = in;
}
}
time += toNextGreen;
}
if(!okk) {
std::cout << "Signals fail to synchronise in 5 hours" << std::endl;
}
}
}