-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathday_01b.cpp
36 lines (33 loc) · 825 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
36
#include <fstream>
#include <iostream>
#include <string>
#include <unordered_set>
#include <vector>
int main(int argc, char* argv[]) {
constexpr int start_f = 0;
std::string input = "../input/day_01_input";
if (argc > 1) {
input = argv[1];
}
long long sum = 0;
std::unordered_set<long long> seen_vals;
seen_vals.insert(start_f);
bool duplicate_found = false;
while (!duplicate_found) {
std::ifstream file(input);
std::string num;
while(std::getline(file, num)) {
if (num.substr(0, 1) == "+") {
sum += stoll(num.substr(1, num.size() - 1));
} else {
sum += stoll(num);
}
if (const auto [iter, inserted] = seen_vals.insert(sum); !inserted) {
duplicate_found = true;
break;
}
}
}
std::cout << sum << '\n';
return sum;
}