108 lines
2.7 KiB
C++

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