day8 1 and 2 done + refactoring

This commit is contained in:
PoliEcho 2024-12-15 14:56:10 +01:00
parent f830923804
commit d8d2bcc379
7 changed files with 212 additions and 21 deletions

View File

@ -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);
}
}

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,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<unsigned long long>(lineSplitedInt[i]),
permutation[i - 1]);
TmpRes = condop_ull(TmpRes,
static_cast<unsigned long long>(lineSplitedInt[i]),
permutation[i - 1]);
}
PossibleResults.push_back(TmpRes);
}

View File

@ -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<unsigned long long>(lineSplitedInt[i]),
permutation[i - 1]);
TmpRes = condop_ull(TmpRes,
static_cast<unsigned long long>(lineSplitedInt[i]),
permutation[i - 1]);
}
PossibleResults.push_back(TmpRes);
}

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";
}

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;