89 lines
2.4 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]);
}
}
}
}
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";
}