ha day5 done
This commit is contained in:
parent
bb01662c4e
commit
3ef1b59e2b
35
day5/day.cpp
35
day5/day.cpp
@ -36,7 +36,7 @@ void MoveElementByIndex(std::vector<int> &vec, int index, int newIndex) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
std::ifstream inputfile("example");
|
std::ifstream inputfile("input");
|
||||||
std::string input;
|
std::string input;
|
||||||
|
|
||||||
int res = 0;
|
int res = 0;
|
||||||
@ -54,6 +54,8 @@ int main() {
|
|||||||
|
|
||||||
std::vector<std::array<int, 2> *> RulesThatMatch;
|
std::vector<std::array<int, 2> *> RulesThatMatch;
|
||||||
while (std::getline(inputfile, line)) {
|
while (std::getline(inputfile, line)) {
|
||||||
|
lineSplited.clear();
|
||||||
|
lineSplitedInt.clear();
|
||||||
if (line.empty()) {
|
if (line.empty()) {
|
||||||
RuleMode = false;
|
RuleMode = false;
|
||||||
continue;
|
continue;
|
||||||
@ -85,18 +87,20 @@ int main() {
|
|||||||
for (int i = 0; i < lineSplitedInt.size(); i++) {
|
for (int i = 0; i < lineSplitedInt.size(); i++) {
|
||||||
for (int j = 0; j < RulesThatMatch.size(); j++) {
|
for (int j = 0; j < RulesThatMatch.size(); j++) {
|
||||||
if (lineSplitedInt[i] == (*RulesThatMatch[j])[1]) {
|
if (lineSplitedInt[i] == (*RulesThatMatch[j])[1]) {
|
||||||
auto it =
|
for (int k = i; k < lineSplitedInt.size(); k++) {
|
||||||
std::find(lineSplitedInt.begin(), lineSplitedInt.begin() + i,
|
if (lineSplitedInt[k] == (*RulesThatMatch[j])[0]) {
|
||||||
(*RulesThatMatch[0])[j]);
|
// i just realized that i only need to add the correctly
|
||||||
if (it == lineSplitedInt.begin() + i) {
|
// sorted lists so here you have goto
|
||||||
MoveElementByIndex(lineSplitedInt,
|
// xd
|
||||||
std::distance(lineSplitedInt.begin(), it),
|
goto badlist;
|
||||||
0);
|
MoveElementByIndex(lineSplitedInt, k, 0);
|
||||||
moved = true;
|
moved = true;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!moved) {
|
if (!moved) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@ -105,11 +109,16 @@ int main() {
|
|||||||
for (int num : lineSplitedInt) {
|
for (int num : lineSplitedInt) {
|
||||||
std::cout << num << " ";
|
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;
|
std::cout << std::endl;
|
||||||
}
|
}
|
||||||
|
badlist:
|
||||||
}
|
}
|
||||||
|
std::cout << "Result: " << res << std::endl;
|
||||||
for (auto &rule : rules) {
|
|
||||||
std::cout << rule[0] << " " << rule[1] << std::endl;
|
|
||||||
}
|
|
||||||
}
|
}
|
124
day5/second.cpp
Normal file
124
day5/second.cpp
Normal 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;
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user