diff --git a/2022/5/Makefile b/2022/5/Makefile new file mode 120000 index 0000000..9f7e44e --- /dev/null +++ b/2022/5/Makefile @@ -0,0 +1 @@ +../4/Makefile \ No newline at end of file diff --git a/2022/5/data.txt b/2022/5/data.txt new file mode 100644 index 0000000..42c96a4 --- /dev/null +++ b/2022/5/data.txt @@ -0,0 +1,512 @@ + [B] [L] [J] + [B] [Q] [R] [D] [T] + [G] [H] [H] [M] [N] [F] + [J] [N] [D] [F] [J] [H] [B] + [Q] [F] [W] [S] [V] [N] [F] [N] +[W] [N] [H] [M] [L] [B] [R] [T] [Q] +[L] [T] [C] [R] [R] [J] [W] [Z] [L] +[S] [J] [S] [T] [T] [M] [D] [B] [H] + 1 2 3 4 5 6 7 8 9 + +move 5 from 4 to 5 +move 2 from 5 to 8 +move 2 from 9 to 1 +move 2 from 9 to 1 +move 1 from 5 to 3 +move 10 from 5 to 8 +move 1 from 4 to 7 +move 1 from 1 to 2 +move 5 from 3 to 7 +move 1 from 2 to 8 +move 21 from 8 to 5 +move 13 from 5 to 7 +move 2 from 9 to 4 +move 1 from 7 to 4 +move 5 from 1 to 4 +move 1 from 5 to 7 +move 2 from 2 to 7 +move 1 from 3 to 2 +move 1 from 1 to 6 +move 7 from 5 to 9 +move 16 from 7 to 4 +move 7 from 9 to 3 +move 1 from 7 to 5 +move 1 from 3 to 8 +move 3 from 2 to 7 +move 1 from 8 to 9 +move 3 from 3 to 6 +move 21 from 4 to 9 +move 1 from 5 to 7 +move 4 from 4 to 9 +move 8 from 6 to 3 +move 6 from 7 to 1 +move 12 from 9 to 8 +move 6 from 7 to 2 +move 3 from 6 to 5 +move 1 from 6 to 9 +move 4 from 8 to 6 +move 3 from 8 to 5 +move 4 from 1 to 8 +move 4 from 6 to 1 +move 2 from 1 to 3 +move 1 from 5 to 8 +move 2 from 2 to 8 +move 5 from 8 to 3 +move 4 from 2 to 7 +move 5 from 8 to 1 +move 2 from 1 to 7 +move 1 from 8 to 2 +move 2 from 1 to 7 +move 11 from 9 to 2 +move 1 from 8 to 5 +move 2 from 9 to 4 +move 3 from 9 to 5 +move 2 from 5 to 1 +move 6 from 5 to 8 +move 2 from 4 to 2 +move 1 from 5 to 6 +move 7 from 1 to 8 +move 2 from 2 to 7 +move 13 from 8 to 1 +move 16 from 3 to 1 +move 3 from 2 to 1 +move 12 from 7 to 6 +move 15 from 1 to 8 +move 2 from 3 to 8 +move 16 from 1 to 2 +move 24 from 2 to 8 +move 1 from 1 to 5 +move 1 from 5 to 8 +move 3 from 6 to 7 +move 26 from 8 to 3 +move 20 from 3 to 9 +move 1 from 2 to 9 +move 16 from 9 to 3 +move 14 from 3 to 1 +move 13 from 1 to 6 +move 3 from 3 to 4 +move 3 from 9 to 4 +move 1 from 7 to 8 +move 5 from 8 to 2 +move 8 from 8 to 5 +move 18 from 6 to 1 +move 4 from 8 to 5 +move 6 from 4 to 1 +move 2 from 2 to 5 +move 5 from 3 to 8 +move 5 from 8 to 7 +move 2 from 5 to 8 +move 5 from 5 to 4 +move 3 from 2 to 8 +move 22 from 1 to 2 +move 1 from 1 to 2 +move 5 from 8 to 2 +move 2 from 5 to 2 +move 1 from 1 to 6 +move 5 from 5 to 2 +move 1 from 9 to 8 +move 5 from 4 to 1 +move 6 from 6 to 9 +move 3 from 1 to 9 +move 1 from 1 to 7 +move 8 from 9 to 6 +move 6 from 7 to 1 +move 5 from 6 to 5 +move 27 from 2 to 1 +move 4 from 5 to 7 +move 9 from 1 to 5 +move 1 from 9 to 1 +move 3 from 6 to 2 +move 9 from 2 to 1 +move 2 from 7 to 2 +move 1 from 8 to 7 +move 10 from 5 to 9 +move 1 from 9 to 7 +move 25 from 1 to 8 +move 6 from 7 to 4 +move 11 from 1 to 7 +move 3 from 8 to 1 +move 3 from 2 to 6 +move 3 from 8 to 9 +move 11 from 8 to 6 +move 1 from 2 to 6 +move 12 from 6 to 4 +move 13 from 4 to 5 +move 1 from 6 to 1 +move 3 from 7 to 5 +move 5 from 8 to 7 +move 1 from 7 to 1 +move 5 from 1 to 6 +move 3 from 6 to 4 +move 3 from 8 to 6 +move 2 from 5 to 2 +move 12 from 5 to 9 +move 5 from 6 to 2 +move 2 from 5 to 9 +move 6 from 4 to 9 +move 11 from 7 to 3 +move 1 from 2 to 5 +move 1 from 7 to 8 +move 1 from 5 to 7 +move 1 from 7 to 1 +move 1 from 8 to 1 +move 2 from 4 to 7 +move 2 from 6 to 8 +move 5 from 3 to 6 +move 2 from 7 to 2 +move 2 from 2 to 9 +move 1 from 2 to 9 +move 1 from 1 to 6 +move 35 from 9 to 7 +move 2 from 8 to 7 +move 3 from 3 to 8 +move 5 from 2 to 4 +move 3 from 3 to 7 +move 2 from 4 to 7 +move 4 from 6 to 5 +move 4 from 5 to 9 +move 3 from 4 to 5 +move 1 from 8 to 3 +move 4 from 9 to 8 +move 1 from 9 to 6 +move 38 from 7 to 2 +move 1 from 3 to 5 +move 1 from 1 to 7 +move 4 from 7 to 3 +move 3 from 6 to 1 +move 22 from 2 to 7 +move 1 from 5 to 8 +move 7 from 8 to 4 +move 8 from 2 to 8 +move 3 from 5 to 1 +move 4 from 3 to 9 +move 1 from 8 to 3 +move 1 from 3 to 7 +move 2 from 2 to 3 +move 5 from 8 to 9 +move 3 from 9 to 1 +move 2 from 1 to 7 +move 6 from 2 to 3 +move 6 from 3 to 1 +move 2 from 3 to 6 +move 1 from 6 to 1 +move 14 from 7 to 2 +move 4 from 1 to 6 +move 8 from 1 to 3 +move 4 from 3 to 6 +move 3 from 9 to 5 +move 1 from 8 to 6 +move 1 from 8 to 4 +move 9 from 7 to 1 +move 8 from 2 to 4 +move 4 from 2 to 9 +move 2 from 2 to 1 +move 3 from 5 to 8 +move 1 from 8 to 6 +move 1 from 7 to 8 +move 1 from 6 to 5 +move 3 from 9 to 5 +move 2 from 9 to 5 +move 4 from 3 to 9 +move 3 from 6 to 3 +move 3 from 6 to 9 +move 9 from 4 to 1 +move 1 from 9 to 8 +move 3 from 3 to 6 +move 2 from 7 to 4 +move 4 from 8 to 5 +move 7 from 5 to 6 +move 19 from 1 to 9 +move 5 from 9 to 3 +move 2 from 1 to 6 +move 1 from 4 to 6 +move 4 from 3 to 2 +move 21 from 9 to 7 +move 1 from 1 to 2 +move 1 from 9 to 1 +move 1 from 1 to 8 +move 16 from 7 to 6 +move 24 from 6 to 5 +move 7 from 4 to 5 +move 1 from 8 to 3 +move 2 from 2 to 8 +move 31 from 5 to 8 +move 1 from 4 to 6 +move 2 from 6 to 9 +move 1 from 7 to 4 +move 3 from 7 to 9 +move 1 from 4 to 8 +move 2 from 3 to 5 +move 1 from 2 to 3 +move 1 from 3 to 7 +move 1 from 7 to 9 +move 24 from 8 to 6 +move 1 from 8 to 1 +move 30 from 6 to 1 +move 2 from 5 to 2 +move 1 from 6 to 9 +move 6 from 9 to 7 +move 1 from 6 to 4 +move 1 from 4 to 6 +move 23 from 1 to 3 +move 21 from 3 to 4 +move 4 from 2 to 6 +move 3 from 6 to 1 +move 1 from 5 to 1 +move 4 from 1 to 9 +move 3 from 9 to 6 +move 8 from 1 to 6 +move 4 from 8 to 5 +move 2 from 7 to 5 +move 7 from 4 to 3 +move 3 from 4 to 9 +move 9 from 3 to 9 +move 1 from 7 to 6 +move 6 from 5 to 8 +move 14 from 6 to 2 +move 4 from 8 to 4 +move 7 from 4 to 5 +move 1 from 7 to 9 +move 6 from 4 to 3 +move 13 from 2 to 6 +move 5 from 3 to 7 +move 1 from 3 to 8 +move 1 from 8 to 2 +move 4 from 8 to 3 +move 6 from 6 to 4 +move 2 from 2 to 8 +move 5 from 4 to 7 +move 3 from 7 to 5 +move 1 from 7 to 9 +move 2 from 3 to 9 +move 3 from 7 to 3 +move 1 from 7 to 9 +move 1 from 7 to 9 +move 3 from 4 to 1 +move 6 from 6 to 1 +move 2 from 7 to 5 +move 1 from 3 to 5 +move 11 from 9 to 4 +move 9 from 4 to 5 +move 3 from 3 to 4 +move 1 from 3 to 9 +move 2 from 8 to 1 +move 9 from 1 to 8 +move 22 from 5 to 8 +move 2 from 1 to 3 +move 3 from 4 to 6 +move 14 from 8 to 9 +move 1 from 3 to 9 +move 19 from 9 to 3 +move 3 from 9 to 4 +move 2 from 7 to 2 +move 1 from 4 to 6 +move 1 from 3 to 8 +move 8 from 3 to 1 +move 2 from 9 to 6 +move 1 from 2 to 5 +move 3 from 4 to 9 +move 1 from 2 to 3 +move 20 from 8 to 3 +move 4 from 9 to 5 +move 1 from 4 to 2 +move 26 from 3 to 5 +move 1 from 8 to 3 +move 8 from 1 to 4 +move 1 from 3 to 7 +move 1 from 2 to 1 +move 1 from 1 to 6 +move 1 from 6 to 7 +move 4 from 5 to 3 +move 3 from 4 to 2 +move 5 from 5 to 3 +move 2 from 2 to 6 +move 3 from 3 to 5 +move 2 from 4 to 8 +move 5 from 3 to 9 +move 5 from 9 to 8 +move 19 from 5 to 9 +move 1 from 5 to 2 +move 2 from 7 to 1 +move 1 from 1 to 7 +move 1 from 7 to 4 +move 13 from 9 to 3 +move 8 from 6 to 2 +move 10 from 3 to 5 +move 14 from 5 to 4 +move 7 from 8 to 4 +move 1 from 6 to 2 +move 6 from 3 to 8 +move 4 from 9 to 7 +move 2 from 9 to 8 +move 1 from 7 to 1 +move 3 from 2 to 7 +move 1 from 5 to 3 +move 7 from 8 to 6 +move 5 from 6 to 2 +move 8 from 4 to 5 +move 3 from 5 to 8 +move 3 from 8 to 6 +move 5 from 7 to 9 +move 5 from 3 to 6 +move 1 from 9 to 4 +move 17 from 4 to 7 +move 1 from 8 to 1 +move 12 from 7 to 8 +move 3 from 1 to 4 +move 2 from 4 to 6 +move 8 from 6 to 1 +move 4 from 6 to 3 +move 1 from 7 to 8 +move 5 from 5 to 8 +move 4 from 7 to 1 +move 3 from 2 to 6 +move 2 from 5 to 1 +move 6 from 1 to 6 +move 4 from 3 to 5 +move 4 from 5 to 3 +move 1 from 4 to 8 +move 3 from 3 to 2 +move 17 from 8 to 4 +move 6 from 6 to 3 +move 14 from 4 to 9 +move 1 from 3 to 8 +move 1 from 7 to 4 +move 3 from 8 to 3 +move 5 from 2 to 5 +move 6 from 1 to 7 +move 2 from 6 to 4 +move 4 from 5 to 7 +move 1 from 1 to 5 +move 1 from 6 to 3 +move 10 from 7 to 4 +move 1 from 5 to 4 +move 1 from 2 to 3 +move 15 from 4 to 5 +move 3 from 3 to 1 +move 6 from 2 to 6 +move 1 from 2 to 3 +move 2 from 4 to 7 +move 2 from 7 to 8 +move 1 from 4 to 2 +move 2 from 1 to 7 +move 1 from 7 to 2 +move 12 from 9 to 1 +move 4 from 9 to 5 +move 4 from 6 to 2 +move 1 from 7 to 3 +move 6 from 2 to 4 +move 1 from 8 to 5 +move 2 from 4 to 2 +move 11 from 1 to 7 +move 3 from 1 to 4 +move 17 from 5 to 6 +move 15 from 6 to 4 +move 1 from 8 to 9 +move 10 from 4 to 1 +move 1 from 3 to 9 +move 2 from 6 to 5 +move 1 from 2 to 6 +move 4 from 5 to 6 +move 4 from 1 to 2 +move 6 from 6 to 7 +move 2 from 2 to 6 +move 9 from 4 to 9 +move 6 from 1 to 2 +move 3 from 4 to 1 +move 10 from 9 to 8 +move 4 from 2 to 1 +move 1 from 1 to 2 +move 5 from 8 to 6 +move 1 from 2 to 7 +move 1 from 9 to 4 +move 2 from 6 to 9 +move 13 from 7 to 2 +move 5 from 7 to 5 +move 2 from 5 to 2 +move 1 from 4 to 5 +move 4 from 8 to 4 +move 17 from 2 to 6 +move 3 from 4 to 6 +move 2 from 9 to 1 +move 7 from 6 to 8 +move 1 from 5 to 2 +move 1 from 4 to 1 +move 2 from 9 to 4 +move 1 from 3 to 9 +move 4 from 3 to 7 +move 2 from 8 to 5 +move 3 from 7 to 5 +move 10 from 5 to 8 +move 2 from 2 to 4 +move 6 from 1 to 2 +move 4 from 6 to 3 +move 8 from 2 to 6 +move 1 from 7 to 4 +move 5 from 4 to 5 +move 7 from 6 to 7 +move 5 from 3 to 5 +move 5 from 5 to 2 +move 4 from 8 to 1 +move 6 from 1 to 6 +move 3 from 3 to 2 +move 22 from 6 to 2 +move 1 from 9 to 7 +move 8 from 8 to 6 +move 1 from 7 to 6 +move 2 from 5 to 7 +move 4 from 8 to 5 +move 7 from 6 to 7 +move 2 from 6 to 4 +move 14 from 2 to 1 +move 7 from 1 to 3 +move 12 from 7 to 3 +move 1 from 4 to 3 +move 2 from 5 to 8 +move 2 from 8 to 1 +move 1 from 4 to 3 +move 6 from 2 to 9 +move 6 from 9 to 2 +move 2 from 2 to 7 +move 6 from 7 to 5 +move 13 from 3 to 5 +move 5 from 2 to 6 +move 5 from 6 to 1 +move 2 from 3 to 6 +move 1 from 6 to 5 +move 1 from 6 to 1 +move 3 from 1 to 9 +move 6 from 2 to 7 +move 1 from 2 to 3 +move 24 from 5 to 2 +move 7 from 3 to 7 +move 13 from 7 to 9 +move 4 from 1 to 9 +move 4 from 1 to 6 +move 1 from 5 to 6 +move 16 from 9 to 5 +move 1 from 6 to 4 +move 1 from 5 to 2 +move 5 from 1 to 3 +move 11 from 2 to 1 +move 4 from 9 to 6 +move 1 from 4 to 7 +move 2 from 3 to 4 +move 6 from 6 to 9 +move 1 from 1 to 3 +move 2 from 9 to 4 +move 1 from 7 to 9 +move 4 from 2 to 9 +move 8 from 9 to 2 +move 3 from 3 to 2 +move 1 from 9 to 4 +move 5 from 1 to 7 +move 1 from 4 to 8 +move 2 from 1 to 9 +move 1 from 8 to 7 +move 6 from 5 to 3 +move 1 from 5 to 1 +move 5 from 2 to 3 +move 4 from 1 to 5 +move 4 from 7 to 1 +move 8 from 5 to 8 diff --git a/2022/5/main.cpp b/2022/5/main.cpp new file mode 100644 index 0000000..0900caf --- /dev/null +++ b/2022/5/main.cpp @@ -0,0 +1,99 @@ +#include +#include +#include +#include +#include + +void DebugOutput(std::vector> &stacks) +{ + // DEBUG: + for(const auto &st : stacks) + { + std::cout << "Stack: " << std::endl; + for(const auto &ch : st) + std::cout << "[" << ch << "]" << std::endl; + std::cout << std::endl; + } +} + +std::vector SplitStr(std::string str, const std::string &delim) +{ + std::vector rtn; + + for(auto split = str.find(delim); split != std::string::npos; split = str.find(delim)) + { + rtn.push_back(str.substr(0, split)); + str = str.substr(split + delim.size()); // chop off beginning + } + if(str.size()) + rtn.push_back(str); + + return rtn; +} + +int main() +{ + std::ifstream ifs("data.txt"); + if(!ifs.is_open()) + { + std::cerr << "Missing data.txt." << std::endl; + return -1; + } + + std::vector> stacks; + + // Step 1: Parse Starting Position + for(std::string line; std::getline(ifs, line); ) + { + if(line == "") + break; + + stacks.resize((line.size() + 1) / 4); + for(auto idx = 0; (idx * 4) < line.size(); ++idx) + if(line[1 + idx * 4] != ' ') + stacks[idx].push_back(line[1 + idx * 4]); + } + + // Step 2: Clean up data: + for(auto &st : stacks) + { + // clear out last line of each, seeing as how they're just index numbers: + st.pop_back(); + // reverse list since we parsed top to bottom and inserted backwards... + std::reverse(st.begin(), st.end()); + } + + // DebugOutput(stacks); + + // Step 3: Parse Actions + for(std::string line; std::getline(ifs, line); ) + { + if(line == "") + continue; + + auto actions = SplitStr(line, " "); + if(actions.size() != 6) + { + std::cerr << "Data corrupt: " << line << std::endl; + exit(-1); + } + + long amt = std::atoi(actions[1].c_str()); + long from = std::atoi(actions[3].c_str()) - 1; + long to = std::atoi(actions[5].c_str()) - 1; + + for(auto i = 0; i < amt; ++i) + { + stacks[to].push_back(stacks[from].back()); + stacks[from].pop_back(); + } + } + + // DebugOutput(stacks); + + for(const auto &st : stacks) + std::cout << st.back(); + std::cout << std::endl; + + return 0; +}