Compare commits

..

28 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
PoliEcho e7a7ab3cdf Merge remote-tracking branch 'origin/main' 2024-12-14 21:33:19 +01:00
PoliEcho f654c7452c bruteforce forever 2024-12-14 21:32:20 +01:00
PoliEcho 6d94a27d97 why doesn't it work 2024-12-14 21:32:20 +01:00
PoliEcho 455b5b4c88 day6 part 1 done 2024-12-14 21:32:20 +01:00
PoliEcho 7a0f35cda1 ha day5 done 2024-12-14 21:32:20 +01:00
PoliEcho b1f1eaca86 i want to die 2024-12-14 21:32:20 +01:00
PoliEcho ca88560dc8 add readme + added day5 2024-12-14 21:32:20 +01:00
PoliEcho 598de252fe it is sometimes hard to understand what they ment 2024-12-14 21:32:20 +01:00
PoliEcho 8ae76ad885 day4 part 1 done, part two will kill me 2024-12-14 21:32:20 +01:00
PoliEcho 3ef1b59e2b ha day5 done 2024-12-13 22:12:02 +01:00
PoliEcho bb01662c4e i want to die 2024-12-13 18:26:35 +01:00
PoliEcho 7a72812532 add readme + added day5 2024-12-13 15:04:22 +01:00
PoliEcho ab105027d4 it is sometimes hard to understand what they ment 2024-12-13 14:45:46 +01:00
PoliEcho 5fb4213ff1 day4 part 1 done, part two wil kill me 2024-12-13 13:52:16 +01:00
PoliEcho 07af61effb day3 done 2024-12-12 22:11:26 +01:00
PoliEcho 63a8929425 second also done chatgpt helped 2024-12-12 21:32:28 +01:00
PoliEcho 7f4d2d5acb day2 part one done 2024-12-12 21:31:11 +01:00
PoliEcho deafcf3662 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa 2024-12-12 17:29:05 +01:00
23 changed files with 1547 additions and 207 deletions
+4
View File
@@ -1 +1,5 @@
*
!*.*
!*/
.vscode
*.csv
+51 -68
View File
@@ -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::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::ifstream inputfile("input");
std::string input;
// 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;
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";
// 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];
}
}
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";
}
+38 -56
View File
@@ -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::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::ifstream inputfile("input");
std::string input;
// 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";
}
+55 -83
View File
@@ -1,95 +1,67 @@
#include "../include/useful_funcs.h"
#include <algorithm>
#include <cstddef>
#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("example");
std::string input;
int res = 0;
if (inputfile.is_open()) {
std::ifstream inputfile("input");
std::string input;
// Read each line from the file and store it in the
// 'line' variable.
std::string line;
std::vector<std::string> num_s;
std::vector<int> num;
while (std::getline(inputfile, line)) {
num_s = splitString(line);
num.clear();
std::transform(num_s.begin(), num_s.end(), std::back_inserter(num), [](const std::string& s) {
return std::stoi(s);
});
bool safe = true;
int old_n = -1;
for (int n : num) {
if(old_n == -1) {
old_n = n;
continue;
}
if ((old_n - n) > 2 || (old_n - n) > 0) {
safe = false;
}
old_n = n;
}
if (safe) {
res++;
continue;
}
safe = true;
old_n = -1;
for (int n : num) {
if(old_n == -1) {
old_n = n;
continue;
}
if ((old_n - n) <= -3 && (old_n - n) < 0) {
safe = false;
}
old_n = n;
}
if (safe) {
res++;
}
}
int res = 0;
if (inputfile.is_open()) {
std::string line;
std::vector<std::string> num_s;
std::vector<int> num;
while (std::getline(inputfile, line)) {
num_s = splitString(line);
num.clear();
// 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;
ConvertStringVectorToIntVector(num_s, num);
// decreasing
bool safe = true;
for (size_t i = 1; i < num.size(); ++i) {
int diff = num[i - 1] - num[i];
if (diff < 1 || diff > 3) {
safe = false;
break;
}
}
if (safe) {
res++;
std::clog << line << std::endl;
continue;
}
// increasing
safe = true;
for (size_t i = 1; i < num.size(); ++i) {
int diff = num[i] - num[i - 1];
if (diff < 1 || diff > 3) {
safe = false;
break;
}
}
if (safe) {
std::clog << line << std::endl;
res++;
}
}
std::cout << "\n\n" << res << "\n\n";
return 0;
}
inputfile.close();
} else {
std::cerr << "Unable to open file!" << std::endl;
return ENOENT;
}
std::cout << res << std::endl;
return 0;
}
+70
View File
@@ -0,0 +1,70 @@
#include "../include/useful_funcs.h"
#include <algorithm>
#include <cstddef>
#include <errno.h>
#include <fstream>
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
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]);
if (diff < 1 || diff > 3) {
return false;
}
}
return true;
}
bool isSafeWithDampener(const std::vector<int> &num) {
// Check if already safe
if (isSafe(num, false) || isSafe(num, true)) {
return true;
}
// Try removing each level and check for safety
for (size_t i = 0; i < num.size(); ++i) {
std::vector<int> temp = num;
temp.erase(temp.begin() + i); // Remove the current level
if (isSafe(temp, false) || isSafe(temp, true)) {
return true;
}
}
return false;
}
int main() {
std::ifstream inputfile("input");
std::string input;
int res = 0; // Counter for safe reports
if (inputfile.is_open()) {
std::string line;
std::vector<std::string> num_s;
std::vector<int> num;
while (std::getline(inputfile, line)) {
num_s = splitString(line);
num.clear();
ConvertStringVectorToIntVector(num_s, num);
// Check safety with Problem Dampener support
if (isSafeWithDampener(num)) {
res++;
std::clog << line << std::endl;
}
}
inputfile.close();
} else {
std::cerr << "Unable to open file!" << std::endl;
return ENOENT;
}
std::cout << res << std::endl;
return 0;
}
+23
View File
@@ -0,0 +1,23 @@
res = 0
write = True
def mul(a, b):
global write
if write:
global res
res += (a * b)
def do():
global write
write = True
def dont():
global write
write = False
with open('parsed_sec', 'r') as file:
# Read each line in the file
for line in file:
# Print each line
exec(line)
print(res)
+2
View File
@@ -0,0 +1,2 @@
#!/bin/bash
grep -o "mul([0-9]*[0-9],[0-9]*[0-9])\|do()\|don't()" $1 | tr -d "'"
Executable
+2
View File
@@ -0,0 +1,2 @@
#!/bin/bash
grep -o 'mul([0-9]*[0-9],[0-9]*[0-9])' $1
+129
View File
@@ -0,0 +1,129 @@
#include "../include/useful_funcs.h"
#include <asm-generic/errno.h>
#include <cstdlib>
#include <errno.h>
#include <fstream>
#include <iostream>
#include <vector>
int Check(std::vector<std::vector<char>> &wordsearch, int i, int j) {
int xmases = 0;
char op_i;
char op_j;
for (int w = 0; w < 8; w++) {
switch (w) {
case 0:
// search right
op_i = 'n';
op_j = '+';
if (!AreWeInBounds(i, j + 3, wordsearch)) {
continue;
}
break;
case 1:
// search left
op_i = 'n';
op_j = '-';
if (!AreWeInBounds(i, j - 3, wordsearch)) {
continue;
}
break;
case 2:
// search up
op_i = '-';
op_j = 'n';
if (!AreWeInBounds(i - 3, j, wordsearch)) {
continue;
}
break;
case 3:
// search down
op_i = '+';
op_j = 'n';
if (!AreWeInBounds(i + 3, j, wordsearch)) {
continue;
}
break;
case 4:
// search up-right
op_i = '-';
op_j = '+';
if (!AreWeInBounds(i - 3, j + 3, wordsearch)) {
continue;
}
break;
case 5:
// search up-left
op_i = '-';
op_j = '-';
if (!AreWeInBounds(i - 3, j - 3, wordsearch)) {
continue;
}
break;
case 6:
// search down-right
op_i = '+';
op_j = '+';
if (!AreWeInBounds(i + 3, j + 3, wordsearch)) {
continue;
}
break;
case 7:
// search down-left
op_i = '+';
op_j = '-';
if (!AreWeInBounds(i + 3, j - 3, wordsearch)) {
continue;
}
break;
default:
std::cerr << "How did we get here?" << std::endl;
exit(255);
break;
}
if (wordsearch[i][j] == 'X') {
if (wordsearch[conditional_operation(i, 1, op_i)]
[conditional_operation(j, 1, op_j)] == 'M') {
if (wordsearch[conditional_operation(i, 2, op_i)]
[conditional_operation(j, 2, op_j)] == 'A') {
if (wordsearch[conditional_operation(i, 3, op_i)]
[conditional_operation(j, 3, op_j)] == 'S') {
xmases++;
}
}
}
}
}
return xmases;
}
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>> wordsearch;
while (std::getline(inputfile, line)) {
wordsearch.push_back(std::vector<char>(line.begin(), line.end()));
}
for (int i = 0; i < wordsearch.size(); i++) {
for (int j = 0; j < wordsearch[i].size(); j++) {
if (wordsearch[i][j] == 'X') {
res += Check(wordsearch, i, j);
}
}
}
std::cout << res << std::endl;
}
+117
View File
@@ -0,0 +1,117 @@
#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;
int Check(std::vector<std::vector<char>> &wordsearch, int i, int j) {
int xmases = 0;
char op_i;
char op_j;
// array to store the 4 directions
/*
0 1
a
2 3
*/
char xarray[4] = {'\0', '\0', '\0', '\0'};
for (int w = 0; w < 4; w++) {
switch (w) {
case 0:
// search up-left
op_i = '-';
op_j = '-';
if (!AreWeInBounds(i - RANGE, j - RANGE, wordsearch)) {
continue;
}
break;
case 1:
// search up-right
op_i = '-';
op_j = '+';
if (!AreWeInBounds(i - RANGE, j + RANGE, wordsearch)) {
continue;
}
break;
case 2:
// search down-left
op_i = '+';
op_j = '-';
if (!AreWeInBounds(i + RANGE, j - RANGE, wordsearch)) {
continue;
}
break;
case 3:
// search down-right
op_i = '+';
op_j = '+';
if (!AreWeInBounds(i + RANGE, j + RANGE, wordsearch)) {
continue;
}
break;
default:
std::cerr << "How did we get here?" << std::endl;
exit(255);
break;
}
if (wordsearch[i][j] == 'A') {
if (wordsearch[conditional_operation(i, 1, op_i)]
[conditional_operation(j, 1, op_j)] == 'M' ||
wordsearch[conditional_operation(i, 1, op_i)]
[conditional_operation(j, 1, op_j)] == 'S') {
xarray[w] = wordsearch[conditional_operation(i, 1, op_i)]
[conditional_operation(j, 1, op_j)];
}
}
}
int ofset = 3;
for (int w = 0; w < 2; w++) {
if (xarray[w] == 'M' && xarray[w + ofset] == 'S') {
xmases++;
} else if (xarray[w] == 'S' && xarray[w + ofset] == 'M') {
xmases++;
}
ofset -= 2;
}
if (xmases == 2) {
return 1;
} else {
return 0;
}
}
int main() {
std::ifstream inputfile("input");
std::string input;
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>> wordsearch;
while (std::getline(inputfile, line)) {
wordsearch.push_back(std::vector<char>(line.begin(), line.end()));
}
for (int i = 0; i < wordsearch.size(); i++) {
for (int j = 0; j < wordsearch[i].size(); j++) {
if (wordsearch[i][j] == 'A') {
res += Check(wordsearch, i, j);
}
}
}
std::cout << res << std::endl;
}
+105
View File
@@ -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 = 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
View File
@@ -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;
}
+94
View File
@@ -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
View File
@@ -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;
}
+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;
}
+4
View File
@@ -0,0 +1,4 @@
# My advent of code 2024 solutions
> [!NOTE]\
> If something does not work try removing empty line at the end of the input