-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathday_06a.cpp
49 lines (47 loc) · 1.42 KB
/
day_06a.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
48
49
#include <algorithm>
#include <fstream>
#include <iostream>
#include <string>
#include <unordered_set>
#include <vector>
std::string create_config(const std::vector<int>& banks) {
std::string config;
for (const auto& bank : banks) {
config += std::to_string(bank) + '-';
}
return config;
}
int main(int argc, char* argv[]) {
const std::string input = (argc > 1) ? argv[1] : "../input/day_06_input" ;
std::ifstream file(input);
std::string line;
// Using vector instead of array to allow for sample input
std::vector<int> banks;
std::getline(file, line);
std::size_t start = 0;
std::size_t end = line.find(' ', start);
while(end != std::string::npos) {
banks.emplace_back(std::stoi(line.substr(start, end - start)));
start = end + 1;
end = line.find(' ', start);
}
banks.emplace_back(std::stoi(line.substr(start, end - start)));
std::unordered_set<std::string> seen_configurations;
std::string config = create_config(banks);
while(seen_configurations.insert(config).second) {
auto index = std::distance(std::begin(banks), std::max_element(std::begin(banks), std::end(banks)));
int value = banks[index];
banks[index] = 0;
index++;
index = index % banks.size();
while (value > 0) {
banks[index]++;
value--;
index++;
index = index % banks.size();
}
config = create_config(banks);
}
std::cout << seen_configurations.size() << '\n';
return 0;
}