From 26ef423b62b8f4d8e2ee4ee327f43c3a4eea1f78 Mon Sep 17 00:00:00 2001 From: Matthew Gretton-Dann Date: Thu, 9 Dec 2021 15:05:31 +0000 Subject: [PATCH] Cleanup 2016 day 22 puzzles --- 2016/puzzle-22-01.cc | 62 ++++++++++++++++++++++++++++++++++++++++++++ 2016/puzzle-22-02.cc | 7 +++++ 2 files changed, 69 insertions(+) create mode 100644 2016/puzzle-22-01.cc create mode 100644 2016/puzzle-22-02.cc diff --git a/2016/puzzle-22-01.cc b/2016/puzzle-22-01.cc new file mode 100644 index 0000000..54f0e6d --- /dev/null +++ b/2016/puzzle-22-01.cc @@ -0,0 +1,62 @@ +#include +#include +#include +#include + +struct Node +{ + unsigned long x_; + unsigned long y_; + unsigned long size_; + unsigned long used_; + unsigned long avail_; + unsigned long percent_; +}; + +using NodeVector = std::vector; + +auto main() -> int +{ + static std::regex header_re{R"(^Filesystem\s+Size\s+Used\s+Avail\s+Use%$)"}; + static std::regex header2_re{"^root@"}; + static std::regex info_re{ + R"(^/dev/grid/node-x(\d+)-y(\d+)\s+(\d+)T\s+(\d+)T\s+(\d+)T\s+(\d+)%$)"}; + + std::string line; + NodeVector nodes; + std::smatch m; + while (std::getline(std::cin, line)) { + if (std::regex_search(line, m, header_re)) { + continue; + } + if (std::regex_search(line, m, header2_re)) { + continue; + } + if (std::regex_search(line, m, info_re)) { + nodes.push_back({std::stoul(m.str(1)), std::stoul(m.str(2)), std::stoul(m.str(3)), + std::stoul(m.str(4)), std::stoul(m.str(5)), std::stoul(m.str(6))}); + } + else { + std::cerr << "Unable to interpret: " << line << '\n'; + return 1; + } + } + + unsigned viable{0}; + for (auto it1{nodes.begin()}; it1 != nodes.end(); ++it1) { + if (it1->used_ == 0) { + continue; + } + for (auto it2{nodes.begin()}; it2 != nodes.end(); ++it2) { + if (it2 == it1) { + continue; + } + if (it1->used_ <= it2->avail_) { + ++viable; + } + } + } + + std::cout << "Viable " << viable << '\n'; + return 0; +} \ No newline at end of file diff --git a/2016/puzzle-22-02.cc b/2016/puzzle-22-02.cc new file mode 100644 index 0000000..e16f90a --- /dev/null +++ b/2016/puzzle-22-02.cc @@ -0,0 +1,7 @@ +#include + +auto main() -> int +{ + std::cout << "Worked it out by hand: 233\n"; + return 0; +}