-
Notifications
You must be signed in to change notification settings - Fork 9
/
day_06b.cpp
37 lines (36 loc) · 964 Bytes
/
day_06b.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
#include <array>
#include <fstream>
#include <iostream>
#include <limits>
#include <string>
#include <unordered_map>
#include <vector>
int main(int argc, char* argv[]) {
std::string input = "../input/day_06_input";
if (argc > 1) {
input = argv[1];
}
std::string line;
std::fstream file(input);
std::vector<std::unordered_map<char, int>> frequencies;
while(std::getline(file, line)) {
while(frequencies.size() < line.size()) {
frequencies.emplace_back();
}
for (std::size_t idx = 0; idx < line.size(); idx++) {
frequencies[idx][line[idx]]++;
}
}
std::string least_frequent(frequencies.size(), '0');
for (std::size_t idx = 0; idx < frequencies.size(); idx++) {
int min_f = std::numeric_limits<int>::max();
for (const auto& [c, f] : frequencies[idx]) {
if (f < min_f) {
min_f = f;
least_frequent[idx] = c;
}
}
}
std::cout << least_frequent << '\n';
return 0;
}