-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path02.cpp
71 lines (58 loc) · 1.46 KB
/
02.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
#include <algorithm>
#include <cctype>
#include <chrono>
#include <climits>
#include <iostream>
#include <vector>
using std::string;
using std::vector;
int find_evenly_divisble_pair(const vector<int> &numbers) noexcept {
for (const int a : numbers) {
for (const int b : numbers) {
if (a == b) {
continue;
}
if ((a % b) == 0) {
return a / b;
}
}
}
return -1;
}
int main() {
auto tstart = std::chrono::high_resolution_clock::now();
int pt1 = 0;
int pt2 = 0;
string input;
vector<int> numbers;
while (std::getline(std::cin, input)) {
numbers.clear();
int max = INT_MIN;
int min = INT_MAX;
auto it = input.begin();
while (it != input.end() && isdigit(*it)) {
int n = std::stoi(&(*it));
numbers.push_back(n);
max = std::max(max, n);
min = std::min(min, n);
while (isdigit(*it)) {
it++;
}
if (isspace(*it)) {
it++;
}
}
pt1 += (max - min);
pt2 += find_evenly_divisble_pair(numbers);
}
std::cout << "--- Day 2: Corruption Checksum ---\n";
std::cout << "Part 1: " << pt1 << "\n";
std::cout << "Part 2: " << pt2 << "\n";
auto tstop = std::chrono::high_resolution_clock::now();
auto duration =
std::chrono::duration_cast<std::chrono::microseconds>(tstop - tstart);
std::cout << "Time: " << (static_cast<double>(duration.count()) / 1000.0)
<< " ms"
<< "\n";
return EXIT_SUCCESS;
}