Completed Day5 - Horrible parsing function but still works :)

This commit is contained in:
2022-12-05 21:56:22 +00:00
parent 7d0be1fec7
commit fb41918b20
5 changed files with 663 additions and 110 deletions
+125 -3
View File
@@ -1,11 +1,133 @@
#include "../aoc2022.h"
enum EMoveType {
eManual,
eCrateMover9001
};
template <typename Output>
struct SDay05 : public SDay<Output> {
//Class constructor
using SDay<Output>::SDay;
std::vector<std::stack<char>> stacks;
//Solutions to both days
Output part1() override final { return Output(); }
Output part2() override final { return Output(); }
};
Output part1() override final {
parseStacks();
runInstructions(eManual);
std::string ans = getTops();
return 0;
}
Output part2() override final {
parseStacks();
runInstructions(eCrateMover9001);
std::string ans = getTops();
return 0;
}
void parseStacks() {
stacks.clear();
this->input.clear();
this->input.seekg(0, std::ios::beg);
std::stack<std::string> stackStrings;
int stackCount{ 0 };
std::string line{ "" };
while (std::getline(this->input, line))
{
if (line == "")
break; //We have finished collecting stack strings
stackStrings.push(line);
}
//Get the number of stacks needed
line = stackStrings.top();
stackCount = line[line.size()-2]-48;
stackStrings.pop();
for (int i = 0; i < stackCount; i++)
stacks.push_back(std::stack<char>());
int index = 1;
while (!stackStrings.empty())
{
line = stackStrings.top();
int index = 1;
for (int i = 0; i < stackCount; i++)
{
if (line[index] != ' ')
stacks[i].push(line[index]);
index += 4;
}
stackStrings.pop();
}
}
void runInstructions(EMoveType moveType) {
std::string line{ "" };
while (std::getline(this->input, line))
{
std::stringstream ss;
std::vector<int> nums;
/* Storing the whole string into string stream */
ss << line;
/* Running loop till the end of the stream */
std::string temp;
int found;
while (!ss.eof()) {
/* extracting word by word from stream */
ss >> temp;
/* Checking the given word is integer or not */
if (std::stringstream(temp) >> found)
nums.push_back(found);
/* To save from space at the end of string */
temp = "";
}
moveCrates(moveType, nums);
}
}
void moveCrates(EMoveType moveType, const std::vector<int> &nums) {
//run each step
std::stack<char> tempStack;
for (int i = 0; i < nums[0]; i++)
{
if (stacks[static_cast<size_t>(nums[1]) - 1].empty())
break;
if (moveType == eCrateMover9001)
{
tempStack.push(stacks[static_cast<size_t>(nums[1]) - 1].top());
}
else
stacks[static_cast<size_t>(nums[2]) - 1].push(stacks[static_cast<size_t>(nums[1]) - 1].top());
stacks[static_cast<size_t>(nums[1]) - 1].pop();
}
if (moveType == eCrateMover9001)
{
while (!tempStack.empty())
{
stacks[static_cast<size_t>(nums[2]) - 1].push(tempStack.top());
tempStack.pop();
}
}
}
std::string getTops() {
std::string output;
for (auto& stack : stacks)
{
output += stack.top();
}
return output;
}
};
+26 -27
View File
@@ -1,33 +1,32 @@
#include "days/days.h"
template <typename Output>
void runSolutions() {
//Heap allocate all solutions
std::vector<SDay<Output>*> solutions = {
new SDay01<Output>("day01"),
new SDay02<Output>("day02"),
new SDay03<Output>("day03"),
new SDay04<Output>("day04"),
new SDay05<Output>("day05"),
new SDay06<Output>("day06"),
new SDay07<Output>("day07"),
new SDay08<Output>("day08"),
new SDay09<Output>("day09"),
new SDay11<Output>("day11"),
new SDay12<Output>("day12"),
new SDay13<Output>("day13"),
new SDay14<Output>("day14"),
new SDay15<Output>("day15"),
new SDay16<Output>("day16"),
new SDay17<Output>("day17"),
new SDay18<Output>("day18"),
new SDay19<Output>("day19"),
new SDay20<Output>("day20"),
new SDay21<Output>("day21"),
new SDay22<Output>("day22"),
new SDay23<Output>("day23"),
new SDay24<Output>("day24"),
new SDay25<Output>("day25")
std::vector<SDay<int>*> solutions = {
new SDay01<int>("day01"),
new SDay02<int>("day02"),
new SDay03<int>("day03"),
new SDay04<int>("day04"),
new SDay05<int>("day05"),
new SDay06<int>("day06"),
new SDay07<int>("day07"),
new SDay08<int>("day08"),
new SDay09<int>("day09"),
new SDay11<int>("day11"),
new SDay12<int>("day12"),
new SDay13<int>("day13"),
new SDay14<int>("day14"),
new SDay15<int>("day15"),
new SDay16<int>("day16"),
new SDay17<int>("day17"),
new SDay18<int>("day18"),
new SDay19<int>("day19"),
new SDay20<int>("day20"),
new SDay21<int>("day21"),
new SDay22<int>("day22"),
new SDay23<int>("day23"),
new SDay24<int>("day24"),
new SDay25<int>("day25")
};
for (auto& solution : solutions)
@@ -58,5 +57,5 @@ void runSolutions() {
}
int main() {
runSolutions<int>();
runSolutions();
}