-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathday_06b.cpp
51 lines (49 loc) · 1.48 KB
/
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include <algorithm>
#include <fstream>
#include <iostream>
#include <string>
#include <unordered_map>
#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);
}
std::size_t count = 0;
banks.emplace_back(std::stoi(line.substr(start, end - start)));
std::unordered_map<std::string, std::size_t> seen_configurations;
auto config = create_config(banks);
while(seen_configurations.insert({config, count}).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);
count++;
}
std::cout << count - seen_configurations[config] << '\n';
return 0;
}