diff --git a/2022/5/main.cpp b/2022/5/main.cpp index 0900caf..e275798 100644 --- a/2022/5/main.cpp +++ b/2022/5/main.cpp @@ -1,6 +1,7 @@ #include #include #include +#include #include #include @@ -41,6 +42,7 @@ int main() } std::vector> stacks; + std::vector> stacks2; // Step 1: Parse Starting Position for(std::string line; std::getline(ifs, line); ) @@ -64,6 +66,7 @@ int main() } // DebugOutput(stacks); + stacks2 = stacks; // Step 3: Parse Actions for(std::string line; std::getline(ifs, line); ) @@ -82,11 +85,25 @@ int main() long from = std::atoi(actions[3].c_str()) - 1; long to = std::atoi(actions[5].c_str()) - 1; + // Part 1: for(auto i = 0; i < amt; ++i) { stacks[to].push_back(stacks[from].back()); stacks[from].pop_back(); } + + // Part 2: + std::stack temp; + for(auto i = 0; i < amt; ++i) + { + temp.push(stacks2[from].back()); + stacks2[from].pop_back(); + } + for(auto i = 0; i < amt; ++i) + { + stacks2[to].push_back(temp.top()); + temp.pop(); + } } // DebugOutput(stacks); @@ -94,6 +111,9 @@ int main() for(const auto &st : stacks) std::cout << st.back(); std::cout << std::endl; + for(const auto &st : stacks2) + std::cout << st.back(); + std::cout << std::endl; return 0; }