diff --git a/2017/puzzle-06-01.cc b/2017/puzzle-06-01.cc new file mode 100644 index 0000000..b7254a3 --- /dev/null +++ b/2017/puzzle-06-01.cc @@ -0,0 +1,58 @@ +#include +#include +#include + +using UInt = unsigned long; +using LoadingVector = std::vector; + +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 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; +} diff --git a/2017/puzzle-06-02.cc b/2017/puzzle-06-02.cc new file mode 100644 index 0000000..d229f92 --- /dev/null +++ b/2017/puzzle-06-02.cc @@ -0,0 +1,72 @@ +#include +#include +#include + +using UInt = unsigned long; +using LoadingVector = std::vector; + +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 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; +}