Compare commits

...

3 Commits

Author SHA1 Message Date
b9bed47c73 WTF 2024-12-15 17:51:23 +01:00
d8d2bcc379 day8 1 and 2 done + refactoring 2024-12-15 14:56:10 +01:00
f830923804 I am an idiot 2024-12-15 13:06:29 +01:00
8 changed files with 284 additions and 50 deletions

View File

@ -3,35 +3,6 @@
#include <iostream>
#include <vector>
// this is defenitely my code
void heapPermutation(std::vector<char> &a, int size,
std::vector<std::vector<char>> &results) {
if (size == 1) {
results.push_back(a); // Store the current permutation
return;
}
for (int i = 0; i < size; i++) {
heapPermutation(a, size - 1, results); // Recur with reduced size
// If size is odd, swap the first element with the last element
if (size % 2 == 1) {
std::swap(a[0], a[size - 1]);
} else { // If size is even, swap the current element with the last element
std::swap(a[i], a[size - 1]);
}
}
}
std::vector<std::vector<char>>
generatePermutations(const std::vector<char> &input) {
std::vector<char> chars = input; // Create a mutable copy of input
std::vector<std::vector<char>> results; // To store all permutations
heapPermutation(chars, chars.size(), results); // Generate permutations
return results; // Return all permutations
}
// end of defenitely my code
int main() {
std::ifstream inputfile("input");
std::string input;
@ -93,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) {
@ -107,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);
}
}

View File

@ -39,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) {
@ -53,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;

View File

@ -71,8 +71,8 @@ int main() {
TmpRes = lineSplitedInt[0];
// cicle from one since TmpRes is set to the first number
for (int i = 1; i < lineSplitedInt.size(); i++) {
TmpRes = conditional_operation_ull(
TmpRes, static_cast<unsigned long long>(lineSplitedInt[i]),
TmpRes = condop_ull(TmpRes,
static_cast<unsigned long long>(lineSplitedInt[i]),
permutation[i - 1]);
}
PossibleResults.push_back(TmpRes);

View File

@ -71,8 +71,8 @@ int main() {
TmpRes = lineSplitedInt[0];
// cicle from one since TmpRes is set to the first number
for (int i = 1; i < lineSplitedInt.size(); i++) {
TmpRes = conditional_operation_ull(
TmpRes, static_cast<unsigned long long>(lineSplitedInt[i]),
TmpRes = condop_ull(TmpRes,
static_cast<unsigned long long>(lineSplitedInt[i]),
permutation[i - 1]);
}
PossibleResults.push_back(TmpRes);

88
day8/day.cpp Normal file
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
day8/second.cpp Normal file
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";
}

72
day9/day.cpp Normal file
View File

@ -0,0 +1,72 @@
#include "../include/color.h"
#include "../include/useful_funcs.h"
#include <cstdlib>
#include <fstream>
#include <iostream>
#include <vector>
void printDiskMap(std::vector<char> &diskmap) {
std::string numC = BOLDGREEN;
for (int i = 0; i < diskmap.size(); i++) {
if (diskmap[i] == '.') {
numC = BOLDYELLOW;
std::clog << RED "." << RESET;
} else {
std::clog << numC << diskmap[i] << RESET;
}
}
std::cout << 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<char> 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 + '0');
} else {
diskmap.push_back('.');
}
}
if (i % 2 == 0) {
id++;
}
}
}
printDiskMap(diskmap);
{
int firstDot;
while (std::find(diskmap.begin(), diskmap.end(), '.') != diskmap.end()) {
firstDot =
std::find(diskmap.begin(), diskmap.end(), '.') - diskmap.begin();
diskmap[firstDot] = diskmap.back();
diskmap.pop_back();
printDiskMap(diskmap);
}
}
std::vector<int> diskmapInt;
std::transform(diskmap.begin(), diskmap.end(), std::back_inserter(diskmapInt),
[](const char &s) { return s - '0'; });
for (int i = 0; i < diskmapInt.size(); i++) {
res += i * diskmapInt[i];
}
std::cout << CYAN "\nfilesystem checksum is: " << BOLDYELLOW << res << RESET
<< std::endl;
}

View File

@ -31,7 +31,7 @@ bool AreWeInBounds(int x, int y, std::vector<std::vector<char>> &TwoDCharVec) {
return true;
}
int conditional_operation(int a, int b, char op) {
int condop(int a, int b, char op) {
switch (op) {
case '+':
return a + b;
@ -48,8 +48,8 @@ int conditional_operation(int a, int b, char op) {
}
}
unsigned long long conditional_operation_ull(unsigned long long a,
unsigned long long b, char op) {
unsigned long long condop_ull(unsigned long long a, unsigned long long b,
char op) {
switch (op) {
case '+':
return a + b;