From 32598f64a9ba863b90234a15147ac7d40e5a2fc9 Mon Sep 17 00:00:00 2001 From: Matthew Gretton-Dann Date: Mon, 13 Dec 2021 17:25:46 +0000 Subject: [PATCH] Add 2017 day 6 puzzles --- 2017/puzzle-06-01.cc | 58 +++++++++++++++++++++++++++++++++++ 2017/puzzle-06-02.cc | 72 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 130 insertions(+) create mode 100644 2017/puzzle-06-01.cc create mode 100644 2017/puzzle-06-02.cc 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; +}