-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDay04.cpp
More file actions
35 lines (34 loc) · 1.08 KB
/
Day04.cpp
File metadata and controls
35 lines (34 loc) · 1.08 KB
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 "Solution.hpp"
#include <algorithm>
#include <vector>
template <>
void
solve<Day04>(bool part2, std::istream& is, std::ostream& os)
{
int sum{0}, sector;
for (std::string line; std::getline(is, line); ) {
auto numLoc = std::find_if(line.begin(), line.end(), ::isdigit);
auto checkLoc = ++std::find(numLoc, line.end(), '[');
std::string input{line.begin(), numLoc - 1}, check{checkLoc, checkLoc + 5}, res;
sector = ::atoi(&*numLoc);
if (part2) {
for (char &c : input)
c = (c == '-') ? c : ((c - 'a' + sector) % 26) + 'a';
if (input.find("north") != std::string::npos)
break;
} else {
std::vector<std::pair<char, int>> s(26);
for(int i{0}; i < 26; ++i)
s[i] = {'a' + i, 0};
for (char c : input)
(c != '-') && ++s[c - 'a'].second;
std::stable_sort(s.begin(), s.end(), [](auto p1, auto p2) {
return p1.second > p2.second;
});
for (auto& p : s)
res += p.first;
sum += sector * !res.compare(0, check.size(), check);
}
}
os << (part2 ? sector : sum) << std::endl;
}