-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathday_15a.cpp
47 lines (43 loc) · 1.01 KB
/
day_15a.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
#include <algorithm>
#include <cmath>
#include <fstream>
#include <iostream>
#include <numeric>
#include <string>
#include <vector>
int calculate_hash(const std::string& line) {
int value = 0;
for (const auto c : line) {
value += int(c);
value *= 17;
value %= 256;
}
return value;
}
std::vector<std::string> parse(const std::string& s) {
std::vector<std::string> substrs;
std::size_t start = 0;
std::size_t end = s.find(',', start);
while (end != std::string::npos) {
substrs.push_back(s.substr(start, end - start));
start = end + 1;
end = s.find(',', start);
}
substrs.push_back(s.substr(start, s.size() - start));
return substrs;
}
int main(int argc, char * argv[]) {
std::string input = "../input/day_15_input";
if (argc > 1) {
input = argv[1];
}
std::string line;
std::fstream file(input);
std::getline(file, line);
std::size_t total = 0;
for (const auto& substr : parse(line)) {
total += calculate_hash(substr);
}
std::cout << total << '\n';
return 0;
}