Add 2017 day 6 puzzles

This commit is contained in:
2021-12-13 17:25:46 +00:00
parent 204f9fc02a
commit 32598f64a9
2 changed files with 130 additions and 0 deletions

58
2017/puzzle-06-01.cc Normal file
View File

@@ -0,0 +1,58 @@
#include <iostream>
#include <string>
#include <vector>
using UInt = unsigned long;
using LoadingVector = std::vector<UInt>;
auto split(std::string const& line, char c) -> LoadingVector
{
LoadingVector result;
std::size_t idx{0};
while (idx != std::string::npos) {
auto next{line.find(c, idx)};
if (next == std::string::npos) {
result.push_back(std::stoul(line.substr(idx)));
idx = next;
}
else {
result.push_back(std::stoul(line.substr(idx, next - idx)));
idx = next + 1;
}
}
return result;
}
auto main() -> int
{
std::string line;
if (!std::getline(std::cin, line)) {
std::cerr << "Unable ot read line\n";
return 1;
}
LoadingVector load{split(line, '\t')};
std::vector<LoadingVector> states;
do {
states.push_back(load);
auto it{std::max_element(load.begin(), load.end())};
auto amt{*it};
*it = 0;
for (auto& it2 : load) {
it2 += amt / load.size();
}
amt %= load.size();
while (amt != 0) {
++it;
if (it == load.end()) {
it = load.begin();
}
++*it;
--amt;
}
} while (std::find(states.begin(), states.end(), load) == states.end());
std::cout << "Count: " << states.size() << '\n';
return 0;
}

72
2017/puzzle-06-02.cc Normal file
View File

@@ -0,0 +1,72 @@
#include <iostream>
#include <string>
#include <vector>
using UInt = unsigned long;
using LoadingVector = std::vector<UInt>;
auto split(std::string const& line, char c) -> LoadingVector
{
LoadingVector result;
std::size_t idx{0};
while (idx != std::string::npos) {
auto next{line.find(c, idx)};
if (next == std::string::npos) {
result.push_back(std::stoul(line.substr(idx)));
idx = next;
}
else {
result.push_back(std::stoul(line.substr(idx, next - idx)));
idx = next + 1;
}
}
return result;
}
void redistribute(LoadingVector& load)
{
auto it{std::max_element(load.begin(), load.end())};
auto amt{*it};
*it = 0;
for (auto& it2 : load) {
it2 += amt / load.size();
}
amt %= load.size();
while (amt != 0) {
++it;
if (it == load.end()) {
it = load.begin();
}
++*it;
--amt;
}
}
auto main() -> int
{
std::string line;
if (!std::getline(std::cin, line)) {
std::cerr << "Unable ot read line\n";
return 1;
}
LoadingVector load{split(line, '\t')};
std::vector<LoadingVector> states;
do {
states.push_back(load);
redistribute(load);
} while (std::find(states.begin(), states.end(), load) == states.end());
std::cout << "Count: " << states.size() << '\n';
auto count{0};
auto load2{load};
do {
redistribute(load2);
++count;
} while (load != load2);
std::cout << "Loop size: " << count << '\n';
return 0;
}