From a03473f567c34ad56ca3b1da74dc447846346ddf Mon Sep 17 00:00:00 2001 From: Marcus Penate Date: Mon, 5 Dec 2022 12:37:59 -0500 Subject: [PATCH] Day5 --- Day5/part1.cpp | 91 ++++++++++++++++++++++++++++++++++++++++++++++++++ Day5/part2.cpp | 90 +++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 181 insertions(+) create mode 100644 Day5/part1.cpp create mode 100644 Day5/part2.cpp diff --git a/Day5/part1.cpp b/Day5/part1.cpp new file mode 100644 index 0000000..cd3c9c1 --- /dev/null +++ b/Day5/part1.cpp @@ -0,0 +1,91 @@ +#include +#include +#include +#include +#include + +using namespace std; + +int main(int argc, char* argv[]) +{ + bool instruction_mode = false; + string line; + unordered_map stacks; + + struct Instruction + { + int quantity; + int from; + int to; + }; + + vector instructions; + + int stack_max = 0; + + while (getline(cin, line)) + { + if (0 == line.size()) + { + instruction_mode = true; + + for (auto &it : stacks) + { + it.second = it.second.substr(0, it.second.size()-1); + } + + continue; + } + + if (instruction_mode) + { + Instruction instruction; + line = line.substr(5); + string::size_type n = line.find(' '); + instruction.quantity = stoi(line.substr(0,n)); + line = line.substr(n+6); + n = line.find(' '); + instruction.from = stoi(line.substr(0,n)); + line = line.substr(n+4); + instruction.to = stoi(line); + + instructions.push_back(instruction); + } + else + { + for (int i = 1, s = 1; i < line.size(); i+=4, s++) + { + if (line[i] == ' ') + { + continue; + } + + if (!stacks.contains(s)) + { + stacks[s] = ""; + } + + stacks[s] += line[i]; + + if (s > stack_max) + { + stack_max = s; + } + } + } + } + + for (unsigned int i = 0; i < instructions.size(); i++) + { + string taken = stacks[instructions[i].from].substr(0, instructions[i].quantity); + reverse(taken.begin(), taken.end()); + stacks[instructions[i].from] = stacks[instructions[i].from].substr(instructions[i].quantity); + stacks[instructions[i].to] = taken + stacks[instructions[i].to]; + } + + for(int s = 1; s <= stack_max; s++) + { + cout << stacks[s][0]; + } + cout << endl; +} \ No newline at end of file diff --git a/Day5/part2.cpp b/Day5/part2.cpp new file mode 100644 index 0000000..37c35f9 --- /dev/null +++ b/Day5/part2.cpp @@ -0,0 +1,90 @@ +#include +#include +#include +#include +#include + +using namespace std; + +int main(int argc, char* argv[]) +{ + bool instruction_mode = false; + string line; + unordered_map stacks; + + struct Instruction + { + int quantity; + int from; + int to; + }; + + vector instructions; + + int stack_max = 0; + + while (getline(cin, line)) + { + if (0 == line.size()) + { + instruction_mode = true; + + for (auto &it : stacks) + { + it.second = it.second.substr(0, it.second.size()-1); + } + + continue; + } + + if (instruction_mode) + { + Instruction instruction; + line = line.substr(5); + string::size_type n = line.find(' '); + instruction.quantity = stoi(line.substr(0,n)); + line = line.substr(n+6); + n = line.find(' '); + instruction.from = stoi(line.substr(0,n)); + line = line.substr(n+4); + instruction.to = stoi(line); + + instructions.push_back(instruction); + } + else + { + for (int i = 1, s = 1; i < line.size(); i+=4, s++) + { + if (line[i] == ' ') + { + continue; + } + + if (!stacks.contains(s)) + { + stacks[s] = ""; + } + + stacks[s] += line[i]; + + if (s > stack_max) + { + stack_max = s; + } + } + } + } + + for (unsigned int i = 0; i < instructions.size(); i++) + { + string taken = stacks[instructions[i].from].substr(0, instructions[i].quantity); + stacks[instructions[i].from] = stacks[instructions[i].from].substr(instructions[i].quantity); + stacks[instructions[i].to] = taken + stacks[instructions[i].to]; + } + + for(int s = 1; s <= stack_max; s++) + { + cout << stacks[s][0]; + } + cout << endl; +} \ No newline at end of file