From 0b06899b68b52bbf8762a13bdb7c92b5786b35cb Mon Sep 17 00:00:00 2001 From: Matthew Gretton-Dann Date: Mon, 5 Dec 2022 07:35:09 +0000 Subject: [PATCH] Add 2022 Day 5. --- 2022/puzzle-05-01.cc | 67 ++++++++++++++++++++++++++++++++++++++++++++ 2022/puzzle-05-02.cc | 66 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 133 insertions(+) create mode 100644 2022/puzzle-05-01.cc create mode 100644 2022/puzzle-05-02.cc diff --git a/2022/puzzle-05-01.cc b/2022/puzzle-05-01.cc new file mode 100644 index 0000000..b20c1ce --- /dev/null +++ b/2022/puzzle-05-01.cc @@ -0,0 +1,67 @@ +// +// Created by Matthew Gretton-Dann on 05/12/2021. +// + +#include +#include +#include +#include + +using UInt = std::uint64_t; + +auto main() -> int +{ + std::string line; + std::vector stacks(11, ""); + while (std::getline(std::cin, line) && !line.empty()) { + UInt col{1}; + UInt c4{0}; + for (auto c : line) { + ++c4; + if (c4 == 4) { + ++col; + c4 = 0; + } + else if (std::isupper(c) == 1) { + stacks[col].push_back(c); + } + } + } + + std::cout << "Initial stacks:\n"; + for (auto const& s : stacks) { + std::cout << s << '\n'; + } + + while (std::getline(std::cin, line)) { + static std::regex const re{R"(move (\d+) from (\d+) to (\d+))"}; + std::smatch m; + if (!std::regex_search(line, m, re)) { + std::cerr << "Unable to interpret: " << line << "\n"; + return EXIT_FAILURE; + } + UInt const num{std::stoull(m.str(1))}; + UInt const from{std::stoull(m.str(2))}; + UInt const to{std::stoull(m.str(3))}; + assert(stacks[from].size() >= num); + auto s = stacks[from].substr(0, num); + std::reverse(s.begin(), s.end()); + stacks[from] = stacks[from].substr(num); + stacks[to] = s + stacks[to]; + } + + std::cout << "Final stacks:\n"; + for (auto const& s : stacks) { + std::cout << s << '\n'; + } + + std::cout << "Result: "; + for (auto s : stacks) { + if (!s.empty()) { + std::cout << s.front(); + } + } + std::cout << '\n'; + + return 0; +} diff --git a/2022/puzzle-05-02.cc b/2022/puzzle-05-02.cc new file mode 100644 index 0000000..abd98a4 --- /dev/null +++ b/2022/puzzle-05-02.cc @@ -0,0 +1,66 @@ +// +// Created by Matthew Gretton-Dann on 05/12/2021. +// + +#include +#include +#include +#include + +using UInt = std::uint64_t; + +auto main() -> int +{ + std::string line; + std::vector stacks(11, ""); + while (std::getline(std::cin, line) && !line.empty()) { + UInt col{1}; + UInt c4{0}; + for (auto c : line) { + ++c4; + if (c4 == 4) { + ++col; + c4 = 0; + } + else if (std::isupper(c) == 1) { + stacks[col].push_back(c); + } + } + } + + std::cout << "Initial stacks:\n"; + for (auto const& s : stacks) { + std::cout << s << '\n'; + } + + while (std::getline(std::cin, line)) { + static std::regex const re{R"(move (\d+) from (\d+) to (\d+))"}; + std::smatch m; + if (!std::regex_search(line, m, re)) { + std::cerr << "Unable to interpret: " << line << "\n"; + return EXIT_FAILURE; + } + UInt const num{std::stoull(m.str(1))}; + UInt const from{std::stoull(m.str(2))}; + UInt const to{std::stoull(m.str(3))}; + assert(stacks[from].size() >= num); + auto s = stacks[from].substr(0, num); + stacks[from] = stacks[from].substr(num); + stacks[to] = s + stacks[to]; + } + + std::cout << "Final stacks:\n"; + for (auto const& s : stacks) { + std::cout << s << '\n'; + } + + std::cout << "Result: "; + for (auto s : stacks) { + if (!s.empty()) { + std::cout << s.front(); + } + } + std::cout << '\n'; + + return 0; +}