i want to die

This commit is contained in:
PoliEcho 2024-12-13 18:26:35 +01:00
parent ca88560dc8
commit b1f1eaca86

View File

@ -1,8 +1,42 @@
#include <algorithm>
#include <array>
#include <codecvt>
#include <errno.h>
#include <fstream> #include <fstream>
#include <iostream> #include <iostream>
#include <sstream>
#include <string>
#include <vector> #include <vector>
std::vector<std::string> splitString(const std::string &str) {
std::vector<std::string> 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<int> &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() { int main() {
std::ifstream inputfile("input"); std::ifstream inputfile("example");
std::string input; std::string input;
int res = 0; int res = 0;
@ -11,8 +45,71 @@ int main() {
return ENOENT; return ENOENT;
} }
std::string line; std::string line;
std::vector<std::vector<char>> wordsearch; std::vector<std::array<int, 2>> rules;
bool RuleMode = true;
std::array<int, 2> RuleTmp;
std::vector<std::string> lineSplited;
std::vector<int> lineSplitedInt;
std::vector<std::array<int, 2> *> RulesThatMatch;
while (std::getline(inputfile, line)) { 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;
} }
} }