105 lines
2.8 KiB
C++
105 lines
2.8 KiB
C++
#include "../include/useful_funcs.h"
|
|
#include <algorithm>
|
|
#include <array>
|
|
#include <codecvt>
|
|
#include <errno.h>
|
|
#include <fstream>
|
|
#include <iostream>
|
|
#include <sstream>
|
|
#include <string>
|
|
#include <vector>
|
|
|
|
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);
|
|
|
|
ConvertStringVectorToIntVector(lineSplited, lineSplitedInt);
|
|
|
|
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;
|
|
} |