From d8d2bcc379c96f88051b97c37dad44aeea913c27 Mon Sep 17 00:00:00 2001 From: PoliEcho Date: Sun, 15 Dec 2024 14:56:10 +0100 Subject: [PATCH] day8 1 and 2 done + refactoring --- day6/day.cpp | 10 ++-- day6/second.cpp | 10 ++-- day7/day.cpp | 6 +-- day7/second.cpp | 6 +-- day8/day.cpp | 88 +++++++++++++++++++++++++++++++++ day8/second.cpp | 107 +++++++++++++++++++++++++++++++++++++++++ include/useful_funcs.h | 6 +-- 7 files changed, 212 insertions(+), 21 deletions(-) create mode 100644 day8/second.cpp diff --git a/day6/day.cpp b/day6/day.cpp index a300061..3cac162 100644 --- a/day6/day.cpp +++ b/day6/day.cpp @@ -64,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) { @@ -78,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); } } diff --git a/day6/second.cpp b/day6/second.cpp index 84ef77d..9ff8bd2 100644 --- a/day6/second.cpp +++ b/day6/second.cpp @@ -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; diff --git a/day7/day.cpp b/day7/day.cpp index 7af1732..3543a3d 100644 --- a/day7/day.cpp +++ b/day7/day.cpp @@ -71,9 +71,9 @@ 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(lineSplitedInt[i]), - permutation[i - 1]); + TmpRes = condop_ull(TmpRes, + static_cast(lineSplitedInt[i]), + permutation[i - 1]); } PossibleResults.push_back(TmpRes); } diff --git a/day7/second.cpp b/day7/second.cpp index 82157ed..f8cd633 100644 --- a/day7/second.cpp +++ b/day7/second.cpp @@ -71,9 +71,9 @@ 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(lineSplitedInt[i]), - permutation[i - 1]); + TmpRes = condop_ull(TmpRes, + static_cast(lineSplitedInt[i]), + permutation[i - 1]); } PossibleResults.push_back(TmpRes); } diff --git a/day8/day.cpp b/day8/day.cpp index e69de29..3af9b50 100644 --- a/day8/day.cpp +++ b/day8/day.cpp @@ -0,0 +1,88 @@ +#include "../include/color.h" +#include "../include/useful_funcs.h" +#include +#include +#include +#include + +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> map; + std::vector> antiNodeMap; + std::vector fregs; + + while (std::getline(inputfile, line)) { + map.push_back(std::vector(line.begin(), line.end())); + } + antiNodeMap.resize(map.size(), std::vector(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> 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"; +} diff --git a/day8/second.cpp b/day8/second.cpp new file mode 100644 index 0000000..743de0a --- /dev/null +++ b/day8/second.cpp @@ -0,0 +1,107 @@ +#include "../include/color.h" +#include "../include/useful_funcs.h" +#include +#include +#include +#include + +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> map; + std::vector> antiNodeMap; + std::vector fregs; + + while (std::getline(inputfile, line)) { + map.push_back(std::vector(line.begin(), line.end())); + } + antiNodeMap.resize(map.size(), std::vector(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> 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"; +} diff --git a/include/useful_funcs.h b/include/useful_funcs.h index 2751515..5a8a44b 100644 --- a/include/useful_funcs.h +++ b/include/useful_funcs.h @@ -31,7 +31,7 @@ bool AreWeInBounds(int x, int y, std::vector> &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;