-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathday_13a.cpp
41 lines (37 loc) · 1.23 KB
/
day_13a.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
#include <cctype>
#include <fstream>
#include <iostream>
#include <string>
int main() {
std::ifstream file{"../input/day_13_input"};
int arrive_abs; // attive at bus stop
file >> arrive_abs;
std::string all_bus_f; // all buses frequencies
file >> all_bus_f;
int earliest_bus; // To be found
int extra_time = arrive_abs; // Ensures > arrive_abs % all_bus_f
int begin = 0;
int end = all_bus_f.find(",", begin);
while (end != std::string::npos) {
const std::string bus_f = all_bus_f.substr(begin, end - begin);
if (bus_f.find_first_not_of("1234567890") == std::string::npos) {
const int temp = std::abs((arrive_abs % stoi(bus_f)) - stoi(bus_f));
if (temp < extra_time) {
extra_time = temp;
earliest_bus = stoi(bus_f);
}
}
begin = end + 1;
end = all_bus_f.find(",", begin);
}
const std::string bus_f = all_bus_f.substr(begin, end - begin);
if (bus_f.find_first_not_of("1234567890") == std::string::npos) {
const int temp = std::abs((arrive_abs % stoi(bus_f)) - stoi(bus_f));
if (temp < extra_time) {
extra_time = temp;
earliest_bus = stoi(bus_f);
}
}
std::cout << earliest_bus * extra_time << '\n';
return earliest_bus * extra_time;
}