-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathday_01b.cpp
35 lines (32 loc) · 949 Bytes
/
day_01b.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
#include <algorithm>
#include <fstream>
#include <iostream>
#include <iterator>
#include <vector>
int main() {
constexpr int total = 2020;
std::ifstream file;
file.open("../input/day_01_input");
int num = 0;
std::vector<int> entries_v{std::istream_iterator<int>{file},
{}}; // Ref: C++'s most vexing parse :D
std::sort(std::begin(entries_v), std::end(entries_v));
const size_t size = entries_v.size();
for (size_t start = 0; start < size - 2; ++start) {
size_t mid = start + 1;
size_t end = size - 1;
while (mid < end) {
const int rem =
total - entries_v[start] - entries_v[mid] - entries_v[end];
if (rem == 0) {
std::cout << entries_v[start] * entries_v[mid] * entries_v[end] << '\n';
return entries_v[start] * entries_v[mid] * entries_v[end];
} else if (rem > 0) {
++mid;
} else {
--end;
}
}
}
return 0;
}