From b1f1eaca861f64c3e8c29a1c2fcc1bb53a262bcd Mon Sep 17 00:00:00 2001 From: PoliEcho Date: Fri, 13 Dec 2024 18:26:35 +0100 Subject: [PATCH] i want to die --- day5/day.cpp | 101 ++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 99 insertions(+), 2 deletions(-) diff --git a/day5/day.cpp b/day5/day.cpp index 3ff82c1..ab1848f 100644 --- a/day5/day.cpp +++ b/day5/day.cpp @@ -1,8 +1,42 @@ +#include +#include +#include +#include #include #include +#include +#include #include + +std::vector splitString(const std::string &str) { + std::vector words; + std::string word; + std::istringstream iss(str); + + // Use std::getline with ',' as the delimiter + while (std::getline(iss, word, ',')) { + // Trim whitespace from the beginning and end of the word + size_t start = word.find_first_not_of(" \t"); + size_t end = word.find_last_not_of(" \t"); + if (start != std::string::npos && end != std::string::npos) { + words.push_back(word.substr(start, end - start + 1)); + } + } + + return words; +} + +void MoveElementByIndex(std::vector &vec, int index, int newIndex) { + if (index == newIndex) { + return; + } + int num = vec[index]; + vec.erase(vec.begin() + index); + vec.insert(vec.begin() + newIndex, num); +} + int main() { - std::ifstream inputfile("input"); + std::ifstream inputfile("example"); std::string input; int res = 0; @@ -11,8 +45,71 @@ int main() { return ENOENT; } std::string line; - std::vector> wordsearch; + std::vector> rules; + bool RuleMode = true; + std::array RuleTmp; + std::vector lineSplited; + std::vector lineSplitedInt; + + std::vector *> RulesThatMatch; while (std::getline(inputfile, line)) { + if (line.empty()) { + RuleMode = false; + continue; + } + if (RuleMode) { + sscanf(line.c_str(), "%d|%d", &RuleTmp[0], &RuleTmp[1]); + rules.push_back(RuleTmp); + } else { + lineSplited = splitString(line); + // transform strings to ints + std::transform(lineSplited.begin(), lineSplited.end(), + std::back_inserter(lineSplitedInt), + [](const std::string &s) { return std::stoi(s); }); + + for (int i = 0; i < rules.size(); i++) { + for (int j = 0; j < lineSplitedInt.size(); j++) { + if (rules[i][0] == lineSplitedInt[j]) { + for (int k = 0; k < lineSplitedInt.size(); k++) { + if (rules[i][1] == lineSplitedInt[k]) { + RulesThatMatch.push_back(&rules[i]); + } + } + } + } + } + + bool moved = false; + while (true) { + for (int i = 0; i < lineSplitedInt.size(); i++) { + for (int j = 0; j < RulesThatMatch.size(); j++) { + if (lineSplitedInt[i] == (*RulesThatMatch[j])[1]) { + auto it = + std::find(lineSplitedInt.begin(), lineSplitedInt.begin() + i, + (*RulesThatMatch[0])[j]); + if (it == lineSplitedInt.begin() + i) { + MoveElementByIndex(lineSplitedInt, + std::distance(lineSplitedInt.begin(), it), + 0); + moved = true; + } + } + } + } + if (!moved) { + break; + } + moved = false; + } + for (int num : lineSplitedInt) { + std::cout << num << " "; + } + std::cout << std::endl; + } + } + + for (auto &rule : rules) { + std::cout << rule[0] << " " << rule[1] << std::endl; } } \ No newline at end of file