ha day5 done

This commit is contained in:
PoliEcho 2024-12-13 22:12:02 +01:00
parent b1f1eaca86
commit 7a0f35cda1
2 changed files with 146 additions and 13 deletions

View File

@ -36,7 +36,7 @@ void MoveElementByIndex(std::vector<int> &vec, int index, int newIndex) {
}
int main() {
std::ifstream inputfile("example");
std::ifstream inputfile("input");
std::string input;
int res = 0;
@ -54,6 +54,8 @@ int main() {
std::vector<std::array<int, 2> *> RulesThatMatch;
while (std::getline(inputfile, line)) {
lineSplited.clear();
lineSplitedInt.clear();
if (line.empty()) {
RuleMode = false;
continue;
@ -85,18 +87,20 @@ int main() {
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;
for (int k = i; k < lineSplitedInt.size(); k++) {
if (lineSplitedInt[k] == (*RulesThatMatch[j])[0]) {
// i just realized that i only need to add the correctly
// sorted lists so here you have goto
// xd
goto badlist;
MoveElementByIndex(lineSplitedInt, k, 0);
moved = true;
}
}
}
}
}
if (!moved) {
break;
}
@ -105,11 +109,16 @@ int main() {
for (int num : lineSplitedInt) {
std::cout << num << " ";
}
if (lineSplitedInt.size() % 2 == 0) {
std::cerr << "Error: even line lenght" << std::endl;
return EIO;
}
res += lineSplitedInt[(lineSplitedInt.size() - 1) / 2];
std::cout << "added: " << lineSplitedInt[(lineSplitedInt.size() - 1) / 2]
<< std::endl;
std::cout << std::endl;
}
badlist:
}
for (auto &rule : rules) {
std::cout << rule[0] << " " << rule[1] << std::endl;
}
std::cout << "Result: " << res << std::endl;
}

124
day5/second.cpp Normal file
View File

@ -0,0 +1,124 @@
#include <algorithm>
#include <array>
#include <codecvt>
#include <errno.h>
#include <fstream>
#include <iostream>
#include <sstream>
#include <string>
#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() {
std::ifstream inputfile("input");
std::string input;
int res = 0;
if (!inputfile.is_open()) {
std::cerr << "Could not open the file" << std::endl;
return ENOENT;
}
std::string line;
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)) {
lineSplited.clear();
lineSplitedInt.clear();
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 badlist = false;
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]) {
for (int k = i; k < lineSplitedInt.size(); k++) {
if (lineSplitedInt[k] == (*RulesThatMatch[j])[0]) {
badlist = true;
MoveElementByIndex(lineSplitedInt, k, 0);
moved = true;
}
}
}
}
}
if (!moved) {
break;
}
moved = false;
}
for (int num : lineSplitedInt) {
std::cout << num << " ";
}
if (lineSplitedInt.size() % 2 == 0) {
std::cerr << "Error: even line lenght" << std::endl;
return EIO;
}
if (badlist) {
res += lineSplitedInt[(lineSplitedInt.size() - 1) / 2];
std::cout << "added: "
<< lineSplitedInt[(lineSplitedInt.size() - 1) / 2]
<< std::endl;
std::cout << std::endl;
}
}
badlist:
}
std::cout << "Result: " << res << std::endl;
}