-
Notifications
You must be signed in to change notification settings - Fork 0
/
challenge18.cpp
72 lines (63 loc) · 1.75 KB
/
challenge18.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
// AquaQ Challenge Hub
// Challenge 18: Emit time
// https://challenges.aquaq.co.uk/challenge/18
#include <chrono>
#include <fstream>
#include <iostream>
#include <string>
#include <vector>
#include <date/date.h>
#include <gsl/util>
static bool readFile(const std::string& fileName, std::vector<std::string>& lines)
{
std::ifstream iss{fileName};
if (!iss) {
std::cerr << "Cannot open file " << fileName << std::endl;
return false;
}
auto closeStream = gsl::finally([&iss] { iss.close(); });
std::string str;
while (std::getline(iss, str)) {
lines.push_back(str);
}
return true;
}
using namespace std::chrono_literals;
inline bool isPalindromic(const std::chrono::seconds& sec)
{
const auto hms = date::hh_mm_ss<std::chrono::seconds>(sec);
return ((hms.hours() % 24) / 10) / 1h == (hms.seconds() % 10) / 1s &&
((hms.hours() % 24) % 10) / 1h == (hms.seconds() / 10) / 1s &&
(hms.minutes() / 10) / 1min == (hms.minutes() % 10) / 1min;
}
std::chrono::seconds toSec(const std::string& time)
{
std::istringstream iss{time};
std::chrono::seconds sec;
// Format: 13:41:00
iss >> date::parse("%T", sec);
return sec;
}
int main(int argc, char* argv[])
{
std::vector<std::string> lines{};
if (argc == 2) {
if (!readFile(argv[1], lines)) {
return EXIT_FAILURE;
}
}
size_t sum{};
for (const auto& line : lines) {
auto secUp = toSec(line);
auto secDown{secUp};
size_t delta{};
while (!isPalindromic(secUp) && !isPalindromic(secDown)) {
secUp += 1s;
secDown -= 1s;
delta++;
}
sum += delta;
}
std::cout << sum << std::endl;
return EXIT_SUCCESS;
}