Compare commits

...

10 Commits

Author SHA1 Message Date
PoliEcho 3c46d64137 I hate SIGSEGV 2024-12-18 21:35:38 +01:00
PoliEcho 75f8b35464 fuck this 2024-12-18 14:19:43 +01:00
PoliEcho 73ce54256e change cout to clog 2024-12-15 21:37:43 +01:00
PoliEcho be92e980fd fixed that stupid char overflow day9 part 1 done 2024-12-15 21:33:43 +01:00
PoliEcho b9bed47c73 WTF 2024-12-15 17:51:23 +01:00
PoliEcho d8d2bcc379 day8 1 and 2 done + refactoring 2024-12-15 14:56:10 +01:00
PoliEcho f830923804 I am an idiot 2024-12-15 13:06:29 +01:00
PoliEcho 97cb3b6639 day7 1 and 2 done 2024-12-15 12:57:12 +01:00
PoliEcho ff0b63dbdd make more clear function name 2024-12-14 22:16:23 +01:00
PoliEcho dd628868f1 refactoring 2024-12-14 22:15:16 +01:00
18 changed files with 751 additions and 296 deletions
+5 -22
View File
@@ -1,25 +1,11 @@
#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");
@@ -41,8 +27,7 @@ int main() {
// Close the file stream once all lines have been
// read.
inputfile.close();
}
else {
} else {
// Print an error message to the standard error
// stream if the file cannot be opened.
std::cerr << "Unable to open file!" << std::endl;
@@ -73,6 +58,4 @@ int main() {
res += std::abs(min_l - min_r);
}
std::cout << "\n\n" << res << "\n\n";
}
+5 -23
View File
@@ -1,25 +1,11 @@
#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");
@@ -41,8 +27,7 @@ int main() {
// Close the file stream once all lines have been
// read.
inputfile.close();
}
else {
} else {
// Print an error message to the standard error
// stream if the file cannot be opened.
std::cerr << "Unable to open file!" << std::endl;
@@ -57,10 +42,7 @@ int main() {
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
View File
@@ -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
View File
@@ -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
View File
@@ -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
View File
@@ -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;
+4 -23
View File
@@ -1,3 +1,4 @@
#include "../include/useful_funcs.h"
#include <algorithm>
#include <array>
#include <codecvt>
@@ -8,24 +9,6 @@
#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;
@@ -64,11 +47,9 @@ int main() {
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); });
lineSplited = splitStringByChar(line, ',');
ConvertStringVectorToIntVector(lineSplited, lineSplitedInt);
for (int i = 0; i < rules.size(); i++) {
for (int j = 0; j < lineSplitedInt.size(); j++) {
+3 -22
View File
@@ -1,3 +1,4 @@
#include "../include/useful_funcs.h"
#include <algorithm>
#include <array>
#include <codecvt>
@@ -8,24 +9,6 @@
#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;
@@ -65,10 +48,8 @@ int main() {
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); });
ConvertStringVectorToIntVector(lineSplited, lineSplitedInt);
for (int i = 0; i < rules.size(); i++) {
for (int j = 0; j < lineSplitedInt.size(); j++) {
+5 -27
View File
@@ -1,28 +1,8 @@
#include "../include/useful_funcs.h"
#include <fstream>
#include <iostream>
#include <vector>
bool AreWeInBounds(int x, int y, std::vector<std::vector<char>> &map) {
if (x < 0 || y < 0) {
return false;
}
if (x >= map.size() || y >= map[x].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 main() {
std::ifstream inputfile("input");
std::string input;
@@ -84,13 +64,11 @@ int main() {
exit(255);
break;
}
if (!AreWeInBounds(conditional_operation(posX, 1, op_x),
conditional_operation(posY, 1, op_y), map)) {
if (!AreWeInBounds(condop(posX, 1, op_x), condop(posY, 1, op_y), map)) {
break;
}
switch (map[conditional_operation(posX, 1, op_x)]
[conditional_operation(posY, 1, op_y)]) {
switch (map[condop(posX, 1, op_x)][condop(posY, 1, op_y)]) {
case '#':
direction++;
if (direction > 3) {
@@ -98,8 +76,8 @@ int main() {
}
break;
default:
posX = conditional_operation(posX, 1, op_x);
posY = conditional_operation(posY, 1, op_y);
posX = condop(posX, 1, op_x);
posY = condop(posY, 1, op_y);
}
}
+5 -27
View File
@@ -1,29 +1,9 @@
#include "../include/useful_funcs.h"
#include <fstream>
#include <iostream>
#include <ostream>
#include <vector>
bool AreWeInBounds(int x, int y, std::vector<std::vector<char>> &map) {
if (x < 0 || y < 0) {
return false;
}
if (x >= map.size() || y >= map[x].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;
}
}
bool WillHeGetStuck(int posX, int posY, int direction, int locX, int locY,
std::vector<std::vector<char>> map) {
char op_x;
@@ -59,13 +39,11 @@ bool WillHeGetStuck(int posX, int posY, int direction, int locX, int locY,
exit(255);
break;
}
if (!AreWeInBounds(conditional_operation(posX, 1, op_x),
conditional_operation(posY, 1, op_y), map)) {
if (!AreWeInBounds(condop(posX, 1, op_x), condop(posY, 1, op_y), map)) {
break;
}
switch (map[conditional_operation(posX, 1, op_x)]
[conditional_operation(posY, 1, op_y)]) {
switch (map[condop(posX, 1, op_x)][condop(posY, 1, op_y)]) {
case '#':
direction++;
if (direction > 3) {
@@ -73,8 +51,8 @@ bool WillHeGetStuck(int posX, int posY, int direction, int locX, int locY,
}
break;
default:
posX = conditional_operation(posX, 1, op_x);
posY = conditional_operation(posY, 1, op_y);
posX = condop(posX, 1, op_x);
posY = condop(posY, 1, op_y);
}
}
return false;
+90
View File
@@ -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";
}
+89
View File
@@ -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";
}
+88
View File
@@ -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
View File
@@ -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";
}
+68
View File
@@ -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;
}
+95
View File
@@ -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;
}
+17
View File
@@ -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 */
+85
View File
@@ -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;
}