Compare commits
21 Commits
7a72812532
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
| 3c46d64137 | |||
| 75f8b35464 | |||
| 73ce54256e | |||
| be92e980fd | |||
| b9bed47c73 | |||
| d8d2bcc379 | |||
| f830923804 | |||
| 97cb3b6639 | |||
| ff0b63dbdd | |||
| dd628868f1 | |||
| e7a7ab3cdf | |||
| f654c7452c | |||
| 6d94a27d97 | |||
| 455b5b4c88 | |||
| 7a0f35cda1 | |||
| b1f1eaca86 | |||
| ca88560dc8 | |||
| 598de252fe | |||
| 8ae76ad885 | |||
| 3ef1b59e2b | |||
| bb01662c4e |
@@ -1 +1,5 @@
|
||||
*
|
||||
!*.*
|
||||
!*/
|
||||
.vscode
|
||||
*.csv
|
||||
+50
-67
@@ -1,78 +1,61 @@
|
||||
#include "../include/useful_funcs.h"
|
||||
#include <algorithm>
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <errno.h>
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
#include <sstream>
|
||||
#include <vector>
|
||||
#include <string>
|
||||
#include <fstream>
|
||||
#include <errno.h>
|
||||
|
||||
std::vector<std::string> splitString(const std::string& str) {
|
||||
std::istringstream iss(str);
|
||||
std::vector<std::string> words;
|
||||
std::string word;
|
||||
|
||||
while (iss >> word) {
|
||||
words.push_back(word);
|
||||
}
|
||||
|
||||
return words;
|
||||
}
|
||||
#include <vector>
|
||||
|
||||
int main() {
|
||||
std::ifstream inputfile("input");
|
||||
std::string input;
|
||||
std::ifstream inputfile("input");
|
||||
std::string input;
|
||||
|
||||
std::vector<int> l_sl;
|
||||
std::vector<int> r_sl;
|
||||
if (inputfile.is_open()) {
|
||||
// Read each line from the file and store it in the
|
||||
// 'line' variable.
|
||||
std::string line;
|
||||
std::vector<std::string> num_s;
|
||||
while (std::getline(inputfile, line)) {
|
||||
num_s = splitString(line);
|
||||
l_sl.push_back(std::stoi(num_s[0]));
|
||||
r_sl.push_back(std::stoi(num_s[1]));
|
||||
}
|
||||
std::vector<int> l_sl;
|
||||
std::vector<int> r_sl;
|
||||
if (inputfile.is_open()) {
|
||||
// Read each line from the file and store it in the
|
||||
// 'line' variable.
|
||||
std::string line;
|
||||
std::vector<std::string> num_s;
|
||||
while (std::getline(inputfile, line)) {
|
||||
num_s = splitString(line);
|
||||
l_sl.push_back(std::stoi(num_s[0]));
|
||||
r_sl.push_back(std::stoi(num_s[1]));
|
||||
}
|
||||
|
||||
// Close the file stream once all lines have been
|
||||
// read.
|
||||
inputfile.close();
|
||||
// Close the file stream once all lines have been
|
||||
// read.
|
||||
inputfile.close();
|
||||
} else {
|
||||
// Print an error message to the standard error
|
||||
// stream if the file cannot be opened.
|
||||
std::cerr << "Unable to open file!" << std::endl;
|
||||
return ENOENT;
|
||||
}
|
||||
std::cout << "right: \n";
|
||||
for (int i = 0; i < r_sl.size(); i++) {
|
||||
std::cout << i << ": " << r_sl[i] << "\n";
|
||||
}
|
||||
int min_l;
|
||||
int min_r;
|
||||
int res = 0;
|
||||
while (l_sl.size() != 0) {
|
||||
min_l = l_sl[0];
|
||||
for (int i = 0; i < l_sl.size(); i++) {
|
||||
if (l_sl[i] < min_l) {
|
||||
min_l = l_sl[i];
|
||||
}
|
||||
}
|
||||
else {
|
||||
// Print an error message to the standard error
|
||||
// stream if the file cannot be opened.
|
||||
std::cerr << "Unable to open file!" << std::endl;
|
||||
return ENOENT;
|
||||
min_r = r_sl[0];
|
||||
for (int i = 0; i < r_sl.size(); i++) {
|
||||
if (r_sl[i] < min_r) {
|
||||
min_r = r_sl[i];
|
||||
}
|
||||
}
|
||||
std::cout << "right: \n";
|
||||
for (int i = 0; i< r_sl.size(); i++) {
|
||||
std::cout << i << ": " << r_sl[i] << "\n";
|
||||
}
|
||||
int min_l;
|
||||
int min_r;
|
||||
int res = 0;
|
||||
while (l_sl.size() != 0) {
|
||||
min_l = l_sl[0];
|
||||
for(int i = 0; i < l_sl.size(); i++) {
|
||||
if(l_sl[i] < min_l) {
|
||||
min_l = l_sl[i];
|
||||
}
|
||||
}
|
||||
min_r = r_sl[0];
|
||||
for(int i = 0; i < r_sl.size(); i++) {
|
||||
if(r_sl[i] < min_r) {
|
||||
min_r = r_sl[i];
|
||||
}
|
||||
}
|
||||
l_sl.erase(find(l_sl.begin(), l_sl.end(), min_l));
|
||||
r_sl.erase(find(r_sl.begin(), r_sl.end(), min_r));
|
||||
res+= std::abs(min_l - min_r);
|
||||
}
|
||||
std::cout << "\n\n" << res << "\n\n";
|
||||
|
||||
|
||||
l_sl.erase(find(l_sl.begin(), l_sl.end(), min_l));
|
||||
r_sl.erase(find(r_sl.begin(), r_sl.end(), min_r));
|
||||
res += std::abs(min_l - min_r);
|
||||
}
|
||||
std::cout << "\n\n" << res << "\n\n";
|
||||
}
|
||||
+37
-55
@@ -1,66 +1,48 @@
|
||||
#include "../include/useful_funcs.h"
|
||||
#include <algorithm>
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <errno.h>
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
#include <sstream>
|
||||
#include <vector>
|
||||
#include <string>
|
||||
#include <fstream>
|
||||
#include <errno.h>
|
||||
|
||||
std::vector<std::string> splitString(const std::string& str) {
|
||||
std::istringstream iss(str);
|
||||
std::vector<std::string> words;
|
||||
std::string word;
|
||||
|
||||
while (iss >> word) {
|
||||
words.push_back(word);
|
||||
}
|
||||
|
||||
return words;
|
||||
}
|
||||
#include <vector>
|
||||
|
||||
int main() {
|
||||
std::ifstream inputfile("input");
|
||||
std::string input;
|
||||
std::ifstream inputfile("input");
|
||||
std::string input;
|
||||
|
||||
std::vector<int> l_sl;
|
||||
std::vector<int> r_sl;
|
||||
if (inputfile.is_open()) {
|
||||
// Read each line from the file and store it in the
|
||||
// 'line' variable.
|
||||
std::string line;
|
||||
std::vector<std::string> num_s;
|
||||
while (std::getline(inputfile, line)) {
|
||||
num_s = splitString(line);
|
||||
l_sl.push_back(std::stoi(num_s[0]));
|
||||
r_sl.push_back(std::stoi(num_s[1]));
|
||||
}
|
||||
|
||||
// Close the file stream once all lines have been
|
||||
// read.
|
||||
inputfile.close();
|
||||
std::vector<int> l_sl;
|
||||
std::vector<int> r_sl;
|
||||
if (inputfile.is_open()) {
|
||||
// Read each line from the file and store it in the
|
||||
// 'line' variable.
|
||||
std::string line;
|
||||
std::vector<std::string> num_s;
|
||||
while (std::getline(inputfile, line)) {
|
||||
num_s = splitString(line);
|
||||
l_sl.push_back(std::stoi(num_s[0]));
|
||||
r_sl.push_back(std::stoi(num_s[1]));
|
||||
}
|
||||
else {
|
||||
// Print an error message to the standard error
|
||||
// stream if the file cannot be opened.
|
||||
std::cerr << "Unable to open file!" << std::endl;
|
||||
return ENOENT;
|
||||
}
|
||||
std::cout << "right: \n";
|
||||
for (int i = 0; i< r_sl.size(); i++) {
|
||||
std::cout << i << ": " << r_sl[i] << "\n";
|
||||
}
|
||||
int min_l;
|
||||
int min_r;
|
||||
int res = 0;
|
||||
for (int i = 0; i < l_sl.size(); i++ ) {
|
||||
|
||||
|
||||
res+= l_sl[i] * std::count(r_sl.begin(), r_sl.end(), l_sl[i]);
|
||||
}
|
||||
std::cout << "\n\n" << res << "\n\n";
|
||||
|
||||
// Close the file stream once all lines have been
|
||||
// read.
|
||||
inputfile.close();
|
||||
} else {
|
||||
// Print an error message to the standard error
|
||||
// stream if the file cannot be opened.
|
||||
std::cerr << "Unable to open file!" << std::endl;
|
||||
return ENOENT;
|
||||
}
|
||||
std::cout << "right: \n";
|
||||
for (int i = 0; i < r_sl.size(); i++) {
|
||||
std::cout << i << ": " << r_sl[i] << "\n";
|
||||
}
|
||||
int min_l;
|
||||
int min_r;
|
||||
int res = 0;
|
||||
for (int i = 0; i < l_sl.size(); i++) {
|
||||
|
||||
res += l_sl[i] * std::count(r_sl.begin(), r_sl.end(), l_sl[i]);
|
||||
}
|
||||
std::cout << "\n\n" << res << "\n\n";
|
||||
}
|
||||
+2
-15
@@ -1,3 +1,4 @@
|
||||
#include "../include/useful_funcs.h"
|
||||
#include <algorithm>
|
||||
#include <cstddef>
|
||||
#include <errno.h>
|
||||
@@ -7,18 +8,6 @@
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
std::vector<std::string> splitString(const std::string &str) {
|
||||
std::istringstream iss(str);
|
||||
std::vector<std::string> words;
|
||||
std::string word;
|
||||
|
||||
while (iss >> word) {
|
||||
words.push_back(word);
|
||||
}
|
||||
|
||||
return words;
|
||||
}
|
||||
|
||||
int main() {
|
||||
std::ifstream inputfile("input");
|
||||
std::string input;
|
||||
@@ -33,9 +22,7 @@ int main() {
|
||||
num_s = splitString(line);
|
||||
num.clear();
|
||||
|
||||
// Convert strings to integers
|
||||
std::transform(num_s.begin(), num_s.end(), std::back_inserter(num),
|
||||
[](const std::string &s) { return std::stoi(s); });
|
||||
ConvertStringVectorToIntVector(num_s, num);
|
||||
|
||||
// decreasing
|
||||
bool safe = true;
|
||||
|
||||
+2
-15
@@ -1,3 +1,4 @@
|
||||
#include "../include/useful_funcs.h"
|
||||
#include <algorithm>
|
||||
#include <cstddef>
|
||||
#include <errno.h>
|
||||
@@ -7,18 +8,6 @@
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
std::vector<std::string> splitString(const std::string &str) {
|
||||
std::istringstream iss(str);
|
||||
std::vector<std::string> words;
|
||||
std::string word;
|
||||
|
||||
while (iss >> word) {
|
||||
words.push_back(word);
|
||||
}
|
||||
|
||||
return words;
|
||||
}
|
||||
|
||||
bool isSafe(const std::vector<int> &num, bool checkIncreasing) {
|
||||
for (size_t i = 1; i < num.size(); ++i) {
|
||||
int diff = checkIncreasing ? (num[i] - num[i - 1]) : (num[i - 1] - num[i]);
|
||||
@@ -61,9 +50,7 @@ int main() {
|
||||
num_s = splitString(line);
|
||||
num.clear();
|
||||
|
||||
// Convert strings to integers
|
||||
std::transform(num_s.begin(), num_s.end(), std::back_inserter(num),
|
||||
[](const std::string &s) { return std::stoi(s); });
|
||||
ConvertStringVectorToIntVector(num_s, num);
|
||||
|
||||
// Check safety with Problem Dampener support
|
||||
if (isSafeWithDampener(num)) {
|
||||
|
||||
+1
-21
@@ -1,4 +1,5 @@
|
||||
|
||||
#include "../include/useful_funcs.h"
|
||||
#include <asm-generic/errno.h>
|
||||
#include <cstdlib>
|
||||
#include <errno.h>
|
||||
@@ -6,27 +7,6 @@
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
|
||||
bool AreWeInBounds(int i, int j, std::vector<std::vector<char>> &wordsearch) {
|
||||
if (i < 0 || j < 0) {
|
||||
return false;
|
||||
}
|
||||
if (i >= wordsearch.size() || j >= wordsearch[i].size()) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
int conditional_operation(int a, int b, char op) {
|
||||
switch (op) {
|
||||
case '+':
|
||||
return a + b;
|
||||
case '-':
|
||||
return a - b;
|
||||
default:
|
||||
return a;
|
||||
}
|
||||
}
|
||||
|
||||
int Check(std::vector<std::vector<char>> &wordsearch, int i, int j) {
|
||||
int xmases = 0;
|
||||
char op_i;
|
||||
|
||||
+1
-22
@@ -1,36 +1,15 @@
|
||||
|
||||
#include "../include/useful_funcs.h"
|
||||
#include <asm-generic/errno.h>
|
||||
#include <cstdlib>
|
||||
#include <errno.h>
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
|
||||
#define RANGE 1
|
||||
|
||||
int res;
|
||||
|
||||
bool AreWeInBounds(int i, int j, std::vector<std::vector<char>> &wordsearch) {
|
||||
if (i < 0 || j < 0) {
|
||||
return false;
|
||||
}
|
||||
if (i >= wordsearch.size() || j >= wordsearch[i].size()) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
int conditional_operation(int a, int b, char op) {
|
||||
switch (op) {
|
||||
case '+':
|
||||
return a + b;
|
||||
case '-':
|
||||
return a - b;
|
||||
default:
|
||||
return a;
|
||||
}
|
||||
}
|
||||
|
||||
int Check(std::vector<std::vector<char>> &wordsearch, int i, int j) {
|
||||
int xmases = 0;
|
||||
char op_i;
|
||||
|
||||
+88
-1
@@ -1,6 +1,23 @@
|
||||
#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;
|
||||
@@ -11,8 +28,78 @@ int main() {
|
||||
return ENOENT;
|
||||
}
|
||||
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)) {
|
||||
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 = splitStringByChar(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 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]) {
|
||||
// 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;
|
||||
}
|
||||
moved = false;
|
||||
}
|
||||
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:
|
||||
}
|
||||
std::cout << "Result: " << res << std::endl;
|
||||
}
|
||||
+105
@@ -0,0 +1,105 @@
|
||||
#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;
|
||||
}
|
||||
@@ -0,0 +1,94 @@
|
||||
#include "../include/useful_funcs.h"
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
|
||||
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::vector<char>> map;
|
||||
|
||||
while (std::getline(inputfile, line)) {
|
||||
map.push_back(std::vector<char>(line.begin(), line.end()));
|
||||
}
|
||||
int posX;
|
||||
int posY;
|
||||
int direction;
|
||||
/*
|
||||
0: up
|
||||
1: right
|
||||
2: down
|
||||
3: left
|
||||
*/
|
||||
|
||||
for (int i = 0; i < map.size(); i++) {
|
||||
for (int j = 0; j < map[i].size(); j++) {
|
||||
if (map[i][j] == '^') {
|
||||
posX = i;
|
||||
posY = j;
|
||||
direction = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
char op_x;
|
||||
char op_y;
|
||||
while (true) {
|
||||
map[posX][posY] = 'X';
|
||||
|
||||
switch (direction) {
|
||||
case 0:
|
||||
op_x = '-';
|
||||
op_y = 'n';
|
||||
break;
|
||||
case 1:
|
||||
op_x = 'n';
|
||||
op_y = '+';
|
||||
break;
|
||||
case 2:
|
||||
op_x = '+';
|
||||
op_y = 'n';
|
||||
break;
|
||||
case 3:
|
||||
op_x = 'n';
|
||||
op_y = '-';
|
||||
break;
|
||||
default:
|
||||
std::cerr << "How did we get here?" << std::endl;
|
||||
exit(255);
|
||||
break;
|
||||
}
|
||||
if (!AreWeInBounds(condop(posX, 1, op_x), condop(posY, 1, op_y), map)) {
|
||||
break;
|
||||
}
|
||||
|
||||
switch (map[condop(posX, 1, op_x)][condop(posY, 1, op_y)]) {
|
||||
case '#':
|
||||
direction++;
|
||||
if (direction > 3) {
|
||||
direction = 0;
|
||||
}
|
||||
break;
|
||||
default:
|
||||
posX = condop(posX, 1, op_x);
|
||||
posY = condop(posY, 1, op_y);
|
||||
}
|
||||
}
|
||||
|
||||
for (int i = 0; i < map.size(); i++) {
|
||||
for (int j = 0; j < map[i].size(); j++) {
|
||||
if (map[i][j] == 'X') {
|
||||
res++;
|
||||
}
|
||||
std::cout << map[i][j];
|
||||
}
|
||||
std::cout << std::endl;
|
||||
}
|
||||
std::cout << "\nRes: " << res << std::endl;
|
||||
}
|
||||
+109
@@ -0,0 +1,109 @@
|
||||
#include "../include/useful_funcs.h"
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
#include <ostream>
|
||||
#include <vector>
|
||||
|
||||
bool WillHeGetStuck(int posX, int posY, int direction, int locX, int locY,
|
||||
std::vector<std::vector<char>> map) {
|
||||
char op_x;
|
||||
char op_y;
|
||||
map[locX][locY] = '#';
|
||||
int conter = 0;
|
||||
while (true) {
|
||||
map[posX][posY] = 'X';
|
||||
conter++;
|
||||
if (conter > 10000) {
|
||||
return true;
|
||||
}
|
||||
|
||||
switch (direction) {
|
||||
case 0:
|
||||
op_x = '-';
|
||||
op_y = 'n';
|
||||
break;
|
||||
case 1:
|
||||
op_x = 'n';
|
||||
op_y = '+';
|
||||
break;
|
||||
case 2:
|
||||
op_x = '+';
|
||||
op_y = 'n';
|
||||
break;
|
||||
case 3:
|
||||
op_x = 'n';
|
||||
op_y = '-';
|
||||
break;
|
||||
default:
|
||||
std::cerr << "How did we get here?" << std::endl;
|
||||
exit(255);
|
||||
break;
|
||||
}
|
||||
if (!AreWeInBounds(condop(posX, 1, op_x), condop(posY, 1, op_y), map)) {
|
||||
break;
|
||||
}
|
||||
|
||||
switch (map[condop(posX, 1, op_x)][condop(posY, 1, op_y)]) {
|
||||
case '#':
|
||||
direction++;
|
||||
if (direction > 3) {
|
||||
direction = 0;
|
||||
}
|
||||
break;
|
||||
default:
|
||||
posX = condop(posX, 1, op_x);
|
||||
posY = condop(posY, 1, op_y);
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
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::vector<char>> map;
|
||||
|
||||
while (std::getline(inputfile, line)) {
|
||||
map.push_back(std::vector<char>(line.begin(), line.end()));
|
||||
}
|
||||
int posX;
|
||||
int posY;
|
||||
int direction;
|
||||
/*
|
||||
0: up
|
||||
1: right
|
||||
2: down
|
||||
3: left
|
||||
*/
|
||||
|
||||
for (int i = 0; i < map.size(); i++) {
|
||||
for (int j = 0; j < map[i].size(); j++) {
|
||||
if (map[i][j] == '^') {
|
||||
posX = i;
|
||||
posY = j;
|
||||
direction = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for (int i = 0; i < map.size(); i++) {
|
||||
for (int j = 0; j < map[i].size(); j++) {
|
||||
if (map[i][j] == '^' || map[i][j] == '#') {
|
||||
continue;
|
||||
} else {
|
||||
if (WillHeGetStuck(posX, posY, direction, i, j, map)) {
|
||||
res++;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
std::cout << "\nRes: " << res << std::endl;
|
||||
}
|
||||
@@ -0,0 +1,90 @@
|
||||
#include "../include/color.h"
|
||||
#include "../include/useful_funcs.h"
|
||||
#include <cstdio>
|
||||
#include <errno.h>
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <sys/types.h>
|
||||
#include <vector>
|
||||
|
||||
// this is defenitely my code
|
||||
void generatePermutation(const std::vector<char> &chars,
|
||||
std::vector<char> current, int length,
|
||||
std::vector<std::vector<char>> &results) {
|
||||
// Base case: if the current vector reaches the desired length
|
||||
if (current.size() == length) {
|
||||
results.push_back(current); // Store the current vector
|
||||
return;
|
||||
}
|
||||
|
||||
// Recursive case: add each character and recurse
|
||||
for (char c : chars) {
|
||||
current.push_back(c); // Append character
|
||||
generatePermutation(chars, current, length, results); // Recurse
|
||||
current.pop_back(); // Backtrack to explore other combinations
|
||||
}
|
||||
}
|
||||
|
||||
std::vector<std::vector<char>>
|
||||
generateAllPermutations(const std::vector<char> &chars, int length) {
|
||||
std::vector<std::vector<char>> results; // To store all generated vectors
|
||||
generatePermutation(chars, {}, length,
|
||||
results); // Start the recursion with an empty vector
|
||||
return results; // Return all generated vectors
|
||||
}
|
||||
// end of defenitely my code
|
||||
|
||||
int main() {
|
||||
std::ifstream inputfile("input");
|
||||
std::string input;
|
||||
|
||||
unsigned long long res = 0;
|
||||
if (!inputfile.is_open()) {
|
||||
std::cerr << "Could not open the file" << std::endl;
|
||||
return ENOENT;
|
||||
}
|
||||
std::string line;
|
||||
std::vector<std::string> lineSplited;
|
||||
std::vector<int> lineSplitedInt;
|
||||
std::vector<std::vector<char>> AllPossiblePermutations;
|
||||
std::vector<unsigned long long> PossibleResults;
|
||||
const std::vector<char> operators = {'+', '*'};
|
||||
unsigned long long EqRes;
|
||||
unsigned long long TmpRes;
|
||||
|
||||
while (std::getline(inputfile, line)) {
|
||||
lineSplited.clear();
|
||||
lineSplitedInt.clear();
|
||||
AllPossiblePermutations.clear();
|
||||
PossibleResults.clear();
|
||||
|
||||
lineSplited = splitString(line);
|
||||
sscanf(lineSplited[0].c_str(), "%llu:", &EqRes);
|
||||
lineSplited.erase(lineSplited.begin());
|
||||
ConvertStringVectorToIntVector(lineSplited, lineSplitedInt);
|
||||
|
||||
AllPossiblePermutations =
|
||||
generateAllPermutations(operators, lineSplitedInt.size() - 1);
|
||||
|
||||
for (std::vector<char> &permutation : AllPossiblePermutations) {
|
||||
TmpRes = lineSplitedInt[0];
|
||||
// cicle from one since TmpRes is set to the first number
|
||||
for (int i = 1; i < lineSplitedInt.size(); i++) {
|
||||
TmpRes = condop_ull(TmpRes,
|
||||
static_cast<unsigned long long>(lineSplitedInt[i]),
|
||||
permutation[i - 1]);
|
||||
}
|
||||
PossibleResults.push_back(TmpRes);
|
||||
}
|
||||
for (unsigned long long &result : PossibleResults) {
|
||||
if (result == EqRes) {
|
||||
res += EqRes;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
// I added colors, because I can
|
||||
std::cout << CYAN "Sum of valid equasions is: " << YELLOW << res
|
||||
<< RESET "\n";
|
||||
}
|
||||
@@ -0,0 +1,89 @@
|
||||
#include "../include/color.h"
|
||||
#include "../include/useful_funcs.h"
|
||||
#include <cstdio>
|
||||
#include <errno.h>
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <sys/types.h>
|
||||
#include <vector>
|
||||
|
||||
// this is defenitely my code
|
||||
void generatePermutation(const std::vector<char> &chars,
|
||||
std::vector<char> current, int length,
|
||||
std::vector<std::vector<char>> &results) {
|
||||
// Base case: if the current vector reaches the desired length
|
||||
if (current.size() == length) {
|
||||
results.push_back(current); // Store the current vector
|
||||
return;
|
||||
}
|
||||
|
||||
// Recursive case: add each character and recurse
|
||||
for (char c : chars) {
|
||||
current.push_back(c); // Append character
|
||||
generatePermutation(chars, current, length, results); // Recurse
|
||||
current.pop_back(); // Backtrack to explore other combinations
|
||||
}
|
||||
}
|
||||
|
||||
std::vector<std::vector<char>>
|
||||
generateAllPermutations(const std::vector<char> &chars, int length) {
|
||||
std::vector<std::vector<char>> results; // To store all generated vectors
|
||||
generatePermutation(chars, {}, length,
|
||||
results); // Start the recursion with an empty vector
|
||||
return results; // Return all generated vectors
|
||||
}
|
||||
// end of defenitely my code
|
||||
|
||||
int main() {
|
||||
std::ifstream inputfile("input");
|
||||
std::string input;
|
||||
|
||||
unsigned long long res = 0;
|
||||
if (!inputfile.is_open()) {
|
||||
std::cerr << "Could not open the file" << std::endl;
|
||||
return ENOENT;
|
||||
}
|
||||
std::string line;
|
||||
std::vector<std::string> lineSplited;
|
||||
std::vector<int> lineSplitedInt;
|
||||
std::vector<std::vector<char>> AllPossiblePermutations;
|
||||
std::vector<unsigned long long> PossibleResults;
|
||||
const std::vector<char> operators = {'+', '*', 'c'};
|
||||
unsigned long long EqRes;
|
||||
unsigned long long TmpRes;
|
||||
|
||||
while (std::getline(inputfile, line)) {
|
||||
lineSplited.clear();
|
||||
lineSplitedInt.clear();
|
||||
AllPossiblePermutations.clear();
|
||||
PossibleResults.clear();
|
||||
|
||||
lineSplited = splitString(line);
|
||||
sscanf(lineSplited[0].c_str(), "%llu:", &EqRes);
|
||||
lineSplited.erase(lineSplited.begin());
|
||||
ConvertStringVectorToIntVector(lineSplited, lineSplitedInt);
|
||||
|
||||
AllPossiblePermutations =
|
||||
generateAllPermutations(operators, lineSplitedInt.size() - 1);
|
||||
|
||||
for (std::vector<char> &permutation : AllPossiblePermutations) {
|
||||
TmpRes = lineSplitedInt[0];
|
||||
// cicle from one since TmpRes is set to the first number
|
||||
for (int i = 1; i < lineSplitedInt.size(); i++) {
|
||||
TmpRes = condop_ull(TmpRes,
|
||||
static_cast<unsigned long long>(lineSplitedInt[i]),
|
||||
permutation[i - 1]);
|
||||
}
|
||||
PossibleResults.push_back(TmpRes);
|
||||
}
|
||||
for (unsigned long long &result : PossibleResults) {
|
||||
if (result == EqRes) {
|
||||
res += EqRes;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
std::cout << CYAN "Sum of valid equasions is: " << YELLOW << res
|
||||
<< RESET "\n";
|
||||
}
|
||||
@@ -0,0 +1,88 @@
|
||||
#include "../include/color.h"
|
||||
#include "../include/useful_funcs.h"
|
||||
#include <array>
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
|
||||
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::vector<char>> map;
|
||||
std::vector<std::vector<char>> antiNodeMap;
|
||||
std::vector<char> fregs;
|
||||
|
||||
while (std::getline(inputfile, line)) {
|
||||
map.push_back(std::vector<char>(line.begin(), line.end()));
|
||||
}
|
||||
antiNodeMap.resize(map.size(), std::vector<char>(map[0].size(), '.'));
|
||||
|
||||
for (int i = 0; i < map.size(); i++) {
|
||||
for (int j = 0; j < map[i].size(); j++) {
|
||||
if (map[i][j] != '.') {
|
||||
if (std::find(fregs.begin(), fregs.end(), map[i][j]) == fregs.end()) {
|
||||
fregs.push_back(map[i][j]);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
std::vector<std::array<int, 2>> AntenaLocations;
|
||||
int DistX;
|
||||
int DistY;
|
||||
|
||||
int antiNodeX;
|
||||
int antiNodeY;
|
||||
|
||||
for (char freg : fregs) {
|
||||
std::clog << RED "searching for: " << YELLOW << freg << RED
|
||||
<< " antinodes" RESET "\n";
|
||||
AntenaLocations.clear();
|
||||
for (int i = 0; i < map.size(); i++) {
|
||||
for (int j = 0; j < map[i].size(); j++) {
|
||||
if (map[i][j] == freg) {
|
||||
AntenaLocations.push_back({i, j});
|
||||
}
|
||||
}
|
||||
}
|
||||
for (int i = 0; i < AntenaLocations.size(); i++) {
|
||||
for (int j = 0; j < AntenaLocations.size(); j++) {
|
||||
if (i != j) {
|
||||
DistX = AntenaLocations[i][0] - AntenaLocations[j][0];
|
||||
DistY = AntenaLocations[i][1] - AntenaLocations[j][1];
|
||||
|
||||
if (AreWeInBounds(AntenaLocations[j][0] - DistX,
|
||||
AntenaLocations[j][1] - DistY, map)) {
|
||||
antiNodeMap[AntenaLocations[j][0] - DistX]
|
||||
[AntenaLocations[j][1] - DistY] = '#';
|
||||
}
|
||||
|
||||
if (AreWeInBounds(AntenaLocations[i][0] + DistX,
|
||||
AntenaLocations[i][1] + DistY, map)) {
|
||||
antiNodeMap[AntenaLocations[i][0] + DistX]
|
||||
[AntenaLocations[i][1] + DistY] = '#';
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for (int i = 0; i < antiNodeMap.size(); i++) {
|
||||
for (int j = 0; j < antiNodeMap[i].size(); j++) {
|
||||
std::cout << antiNodeMap[i][j];
|
||||
if (antiNodeMap[i][j] == '#') {
|
||||
res++;
|
||||
}
|
||||
}
|
||||
std::cout << "\n";
|
||||
}
|
||||
|
||||
std::cout << CYAN "Number of antinodes: " << YELLOW << res << RESET "\n";
|
||||
}
|
||||
+107
@@ -0,0 +1,107 @@
|
||||
#include "../include/color.h"
|
||||
#include "../include/useful_funcs.h"
|
||||
#include <array>
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
|
||||
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::vector<char>> map;
|
||||
std::vector<std::vector<char>> antiNodeMap;
|
||||
std::vector<char> fregs;
|
||||
|
||||
while (std::getline(inputfile, line)) {
|
||||
map.push_back(std::vector<char>(line.begin(), line.end()));
|
||||
}
|
||||
antiNodeMap.resize(map.size(), std::vector<char>(map[0].size(), '.'));
|
||||
|
||||
for (int i = 0; i < map.size(); i++) {
|
||||
for (int j = 0; j < map[i].size(); j++) {
|
||||
if (map[i][j] != '.') {
|
||||
if (std::find(fregs.begin(), fregs.end(), map[i][j]) == fregs.end()) {
|
||||
fregs.push_back(map[i][j]);
|
||||
}
|
||||
antiNodeMap[i][j] = '#';
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
std::vector<std::array<int, 2>> AntenaLocations;
|
||||
int DistX;
|
||||
int DistY;
|
||||
|
||||
int antiNodeX;
|
||||
int antiNodeY;
|
||||
|
||||
int LocX;
|
||||
int LocY;
|
||||
|
||||
char op;
|
||||
int *itPtr;
|
||||
|
||||
for (char freg : fregs) {
|
||||
std::clog << RED "searching for: " << YELLOW << freg << RED
|
||||
<< " antinodes" RESET "\n";
|
||||
AntenaLocations.clear();
|
||||
for (int i = 0; i < map.size(); i++) {
|
||||
for (int j = 0; j < map[i].size(); j++) {
|
||||
if (map[i][j] == freg) {
|
||||
AntenaLocations.push_back({i, j});
|
||||
}
|
||||
}
|
||||
}
|
||||
for (int i = 0; i < AntenaLocations.size(); i++) {
|
||||
for (int j = 0; j < AntenaLocations.size(); j++) {
|
||||
if (i != j) {
|
||||
DistX = AntenaLocations[i][0] - AntenaLocations[j][0];
|
||||
DistY = AntenaLocations[i][1] - AntenaLocations[j][1];
|
||||
|
||||
for (int k = 0; k < 2; k++) {
|
||||
switch (k) {
|
||||
case 0:
|
||||
op = '+';
|
||||
itPtr = &i;
|
||||
break;
|
||||
case 1:
|
||||
op = '-';
|
||||
itPtr = &j;
|
||||
break;
|
||||
default:
|
||||
std::cerr << "how did we get here?" << std::endl;
|
||||
exit(255);
|
||||
}
|
||||
LocX = condop(AntenaLocations[*itPtr][0], DistX, op);
|
||||
LocY = condop(AntenaLocations[*itPtr][1], DistY, op);
|
||||
|
||||
while (AreWeInBounds(LocX, LocY, map)) {
|
||||
antiNodeMap[LocX][LocY] = '#';
|
||||
LocX = condop(LocX, DistX, op);
|
||||
LocY = condop(LocY, DistY, op);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for (int i = 0; i < antiNodeMap.size(); i++) {
|
||||
for (int j = 0; j < antiNodeMap[i].size(); j++) {
|
||||
std::cout << antiNodeMap[i][j];
|
||||
if (antiNodeMap[i][j] == '#') {
|
||||
res++;
|
||||
}
|
||||
}
|
||||
std::cout << "\n";
|
||||
}
|
||||
|
||||
std::cout << CYAN "Number of antinodes: " << YELLOW << res << RESET "\n";
|
||||
}
|
||||
@@ -0,0 +1,68 @@
|
||||
#include "../include/color.h"
|
||||
#include "../include/useful_funcs.h"
|
||||
#include <cstdlib>
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
|
||||
void printDiskMap(std::vector<int> &diskmap) {
|
||||
std::string numC = BOLDGREEN;
|
||||
for (int i = 0; i < diskmap.size(); i++) {
|
||||
if (diskmap[i] == -1) {
|
||||
numC = BOLDYELLOW;
|
||||
std::clog << RED "." << RESET;
|
||||
} else {
|
||||
std::clog << numC << diskmap[i] << RESET;
|
||||
}
|
||||
}
|
||||
std::clog << RESET << std::endl;
|
||||
}
|
||||
int main() {
|
||||
std::ifstream inputfile("input");
|
||||
|
||||
unsigned long long res = 0;
|
||||
if (!inputfile.is_open()) {
|
||||
std::cerr << "Could not open the file" << std::endl;
|
||||
return ENOENT;
|
||||
}
|
||||
|
||||
std::vector<int> diskmap;
|
||||
|
||||
// create the disk map
|
||||
{
|
||||
std::string line;
|
||||
inputfile >> line;
|
||||
int id = 0;
|
||||
std::vector<char> lineSplited = std::vector<char>(line.begin(), line.end());
|
||||
for (int i = 0; i < lineSplited.size(); i++) {
|
||||
|
||||
for (int j = 0; j < lineSplited[i] - '0'; j++) {
|
||||
if (i % 2 == 0) {
|
||||
diskmap.push_back(id);
|
||||
} else {
|
||||
diskmap.push_back(-1);
|
||||
}
|
||||
}
|
||||
if (i % 2 == 0) {
|
||||
id++;
|
||||
}
|
||||
}
|
||||
}
|
||||
// printDiskMap(diskmap);
|
||||
{
|
||||
int firstDot;
|
||||
while (std::find(diskmap.begin(), diskmap.end(), -1) != diskmap.end()) {
|
||||
firstDot =
|
||||
std::find(diskmap.begin(), diskmap.end(), -1) - diskmap.begin();
|
||||
diskmap[firstDot] = diskmap.back();
|
||||
diskmap.pop_back();
|
||||
// printDiskMap(diskmap);
|
||||
}
|
||||
}
|
||||
|
||||
for (int i = 0; i < diskmap.size(); i++) {
|
||||
res += i * diskmap[i];
|
||||
}
|
||||
std::cout << CYAN "\nfilesystem checksum is: " << BOLDYELLOW << res << RESET
|
||||
<< std::endl;
|
||||
}
|
||||
@@ -0,0 +1,95 @@
|
||||
#include "../include/color.h"
|
||||
#include "../include/useful_funcs.h"
|
||||
#include <cstdlib>
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
|
||||
void printDiskMap(std::vector<int> &diskmap) {
|
||||
std::string numC = BOLDGREEN;
|
||||
for (int i = 0; i < diskmap.size(); i++) {
|
||||
if (diskmap[i] == -1) {
|
||||
numC = BOLDYELLOW;
|
||||
std::clog << RED "." << RESET;
|
||||
} else {
|
||||
std::clog << numC << diskmap[i] << RESET;
|
||||
}
|
||||
}
|
||||
std::clog << RESET << std::endl;
|
||||
}
|
||||
int main() {
|
||||
std::ifstream inputfile("example");
|
||||
|
||||
unsigned long long res = 0;
|
||||
if (!inputfile.is_open()) {
|
||||
std::cerr << "Could not open the file" << std::endl;
|
||||
return ENOENT;
|
||||
}
|
||||
|
||||
std::vector<int> diskmap;
|
||||
|
||||
// create the disk map
|
||||
{
|
||||
std::string line;
|
||||
inputfile >> line;
|
||||
int id = 0;
|
||||
std::vector<char> lineSplited = std::vector<char>(line.begin(), line.end());
|
||||
for (int i = 0; i < lineSplited.size(); i++) {
|
||||
|
||||
for (int j = 0; j < lineSplited[i] - '0'; j++) {
|
||||
if (i % 2 == 0) {
|
||||
diskmap.push_back(id);
|
||||
} else {
|
||||
diskmap.push_back(-1);
|
||||
}
|
||||
}
|
||||
if (i % 2 == 0) {
|
||||
id++;
|
||||
}
|
||||
}
|
||||
}
|
||||
printDiskMap(diskmap);
|
||||
|
||||
{
|
||||
int dotFound;
|
||||
auto offset = diskmap.begin();
|
||||
int block_count;
|
||||
|
||||
while (std::find(diskmap.begin(), diskmap.end(), -1) != diskmap.end()) {
|
||||
// Removes empty blocks at the end
|
||||
if (diskmap.back() == -1) {
|
||||
diskmap.pop_back();
|
||||
continue;
|
||||
}
|
||||
|
||||
// counts how many blocks are in file
|
||||
block_count = std::count(diskmap.end(), diskmap.begin(), diskmap.back());
|
||||
|
||||
while (true) {
|
||||
dotFound = std::find(offset, diskmap.end(), -1) - diskmap.begin();
|
||||
|
||||
if (std::count(diskmap.begin() + dotFound,
|
||||
diskmap.begin() + dotFound + block_count,
|
||||
-1) == block_count) {
|
||||
|
||||
std::fill(diskmap.begin() + dotFound,
|
||||
diskmap.begin() + dotFound + block_count, diskmap.back());
|
||||
for (int i = 0; i < block_count; i++) {
|
||||
diskmap.pop_back();
|
||||
}
|
||||
break;
|
||||
} else {
|
||||
offset = diskmap.begin() + dotFound + block_count;
|
||||
}
|
||||
}
|
||||
|
||||
printDiskMap(diskmap);
|
||||
}
|
||||
}
|
||||
|
||||
for (int i = 0; i < diskmap.size(); i++) {
|
||||
res += i * diskmap[i];
|
||||
}
|
||||
std::cout << CYAN "\nfilesystem checksum is: " << BOLDYELLOW << res << RESET
|
||||
<< std::endl;
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
#define RESET "\033[0m"
|
||||
#define BLACK "\033[30m" /* Black */
|
||||
#define RED "\033[31m" /* Red */
|
||||
#define GREEN "\033[32m" /* Green */
|
||||
#define YELLOW "\033[33m" /* Yellow */
|
||||
#define BLUE "\033[34m" /* Blue */
|
||||
#define MAGENTA "\033[35m" /* Magenta */
|
||||
#define CYAN "\033[36m" /* Cyan */
|
||||
#define WHITE "\033[37m" /* White */
|
||||
#define BOLDBLACK "\033[1m\033[30m" /* Bold Black */
|
||||
#define BOLDRED "\033[1m\033[31m" /* Bold Red */
|
||||
#define BOLDGREEN "\033[1m\033[32m" /* Bold Green */
|
||||
#define BOLDYELLOW "\033[1m\033[33m" /* Bold Yellow */
|
||||
#define BOLDBLUE "\033[1m\033[34m" /* Bold Blue */
|
||||
#define BOLDMAGENTA "\033[1m\033[35m" /* Bold Magenta */
|
||||
#define BOLDCYAN "\033[1m\033[36m" /* Bold Cyan */
|
||||
#define BOLDWHITE "\033[1m\033[37m" /* Bold White */
|
||||
@@ -0,0 +1,85 @@
|
||||
#include <algorithm>
|
||||
#include <sstream>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
std::vector<std::string> splitString(const std::string &str) {
|
||||
std::istringstream iss(str);
|
||||
std::vector<std::string> words;
|
||||
std::string word;
|
||||
|
||||
while (iss >> word) {
|
||||
words.push_back(word);
|
||||
}
|
||||
|
||||
return words;
|
||||
}
|
||||
|
||||
void ConvertStringVectorToIntVector(const std::vector<std::string> &strVec,
|
||||
std::vector<int> &intVec) {
|
||||
std::transform(strVec.begin(), strVec.end(), std::back_inserter(intVec),
|
||||
[](const std::string &s) { return std::stoi(s); });
|
||||
}
|
||||
|
||||
bool AreWeInBounds(int x, int y, std::vector<std::vector<char>> &TwoDCharVec) {
|
||||
if (x < 0 || y < 0) {
|
||||
return false;
|
||||
}
|
||||
if (x >= TwoDCharVec.size() || y >= TwoDCharVec[x].size()) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
int condop(int a, int b, char op) {
|
||||
switch (op) {
|
||||
case '+':
|
||||
return a + b;
|
||||
case '-':
|
||||
return a - b;
|
||||
case '*':
|
||||
return a * b;
|
||||
case '/':
|
||||
return a / b;
|
||||
case 'c':
|
||||
return std::stoi(std::to_string(a) + std::to_string(b));
|
||||
default:
|
||||
return a;
|
||||
}
|
||||
}
|
||||
|
||||
unsigned long long condop_ull(unsigned long long a, unsigned long long b,
|
||||
char op) {
|
||||
switch (op) {
|
||||
case '+':
|
||||
return a + b;
|
||||
case '-':
|
||||
return a - b;
|
||||
case '*':
|
||||
return a * b;
|
||||
case '/':
|
||||
return a / b;
|
||||
case 'c':
|
||||
return std::stoull(std::to_string(a) + std::to_string(b));
|
||||
default:
|
||||
return a;
|
||||
}
|
||||
}
|
||||
|
||||
std::vector<std::string> splitStringByChar(const std::string &str, char c) {
|
||||
std::vector<std::string> words;
|
||||
std::string word;
|
||||
std::istringstream iss(str);
|
||||
|
||||
// Use std::getline with ',' as the delimiter
|
||||
while (std::getline(iss, word, c)) {
|
||||
// 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;
|
||||
}
|
||||
Reference in New Issue
Block a user