|
| 1 | +#include <algorithm> |
| 2 | +#include <cmath> |
| 3 | +#include <fstream> |
| 4 | +#include <iostream> |
| 5 | +#include <sstream> |
| 6 | +#include <string> |
| 7 | +#include <unordered_map> |
| 8 | +#include <vector> |
| 9 | + |
| 10 | +struct GridSize { |
| 11 | + int rows; |
| 12 | + int cols; |
| 13 | +}; |
| 14 | + |
| 15 | +class Antenna { |
| 16 | +public: |
| 17 | + Antenna(const std::string &content) { |
| 18 | + int rows = std::count(content.begin(), content.end(), '\n'); |
| 19 | + int cols = content.find('\n'); |
| 20 | + if (cols != std::string::npos) |
| 21 | + cols = cols; |
| 22 | + this->grid = {rows - 1, cols - 1}; |
| 23 | + |
| 24 | + std::istringstream stream(content); |
| 25 | + std::string line; |
| 26 | + int row_idx = 0; |
| 27 | + |
| 28 | + while (std::getline(stream, line)) { |
| 29 | + for (int col_idx = 0; col_idx < line.size(); ++col_idx) { |
| 30 | + char ch = line[col_idx]; |
| 31 | + if (std::isalnum(ch)) { |
| 32 | + frequencies[ch].emplace_back(row_idx, col_idx); |
| 33 | + } |
| 34 | + } |
| 35 | + ++row_idx; |
| 36 | + } |
| 37 | + } |
| 38 | + |
| 39 | + static std::pair<std::pair<int, int>, std::pair<int, int>> |
| 40 | + new_antenna(const std::pair<int, int> ¤t_antenna, |
| 41 | + const std::pair<int, int> &other_antenna, |
| 42 | + const std::pair<int, int> &distance) { |
| 43 | + |
| 44 | + int y = (current_antenna.first < other_antenna.first) |
| 45 | + ? current_antenna.first - distance.first |
| 46 | + : current_antenna.first + distance.first; |
| 47 | + int x = (current_antenna.second < other_antenna.second) |
| 48 | + ? current_antenna.second - distance.second |
| 49 | + : current_antenna.second + distance.second; |
| 50 | + |
| 51 | + int dy = y - current_antenna.first; |
| 52 | + int dx = x - current_antenna.second; |
| 53 | + |
| 54 | + return {{y, x}, {dy, dx}}; |
| 55 | + } |
| 56 | + |
| 57 | + std::pair<bool, bool> can_add_antenna( |
| 58 | + const std::pair<int, int> &antenna, |
| 59 | + const std::vector<std::pair<int, int>> &antenna_positions) const { |
| 60 | + |
| 61 | + if (antenna.first >= 0 && antenna.second >= 0 && |
| 62 | + antenna.second <= grid.cols && antenna.first <= grid.rows) { |
| 63 | + if (std::find(antenna_positions.begin(), antenna_positions.end(), |
| 64 | + antenna) != antenna_positions.end()) { |
| 65 | + return {false, false}; |
| 66 | + } |
| 67 | + return {false, true}; |
| 68 | + } |
| 69 | + return {true, false}; |
| 70 | + } |
| 71 | + |
| 72 | + size_t count_antennas(bool harmony_frequencies) { |
| 73 | + std::vector<std::pair<int, int>> antenna_positions; |
| 74 | + |
| 75 | + for (const auto &[_, positions] : frequencies) { |
| 76 | + for (size_t i = 0; i < positions.size(); ++i) { |
| 77 | + const auto &pos_i = positions[i]; |
| 78 | + for (size_t j = i + 1; j < positions.size(); ++j) { |
| 79 | + const auto &pos_j = positions[j]; |
| 80 | + auto distance = std::make_pair(std::abs(pos_i.first - pos_j.first), |
| 81 | + std::abs(pos_i.second - pos_j.second)); |
| 82 | + |
| 83 | + if (harmony_frequencies) { |
| 84 | + for (const auto &pos : {pos_i, pos_j}) { |
| 85 | + auto [is_out, can_add] = can_add_antenna(pos, antenna_positions); |
| 86 | + if (is_out) { |
| 87 | + break; |
| 88 | + } |
| 89 | + if (can_add) { |
| 90 | + antenna_positions.push_back(pos); |
| 91 | + } |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + auto [first_antenna, distance_1] = |
| 96 | + new_antenna(pos_i, pos_j, distance); |
| 97 | + auto [second_antenna, distance_2] = |
| 98 | + new_antenna(pos_j, pos_i, distance); |
| 99 | + |
| 100 | + while (true) { |
| 101 | + auto [is_out, can_add] = |
| 102 | + can_add_antenna(first_antenna, antenna_positions); |
| 103 | + if (is_out) { |
| 104 | + break; |
| 105 | + } |
| 106 | + if (can_add) { |
| 107 | + antenna_positions.push_back(first_antenna); |
| 108 | + } |
| 109 | + if (!harmony_frequencies) { |
| 110 | + break; |
| 111 | + } |
| 112 | + first_antenna.first += distance_1.first; |
| 113 | + first_antenna.second += distance_1.second; |
| 114 | + } |
| 115 | + |
| 116 | + while (true) { |
| 117 | + auto [is_out, can_add] = |
| 118 | + can_add_antenna(second_antenna, antenna_positions); |
| 119 | + if (is_out) { |
| 120 | + break; |
| 121 | + } |
| 122 | + if (can_add) { |
| 123 | + antenna_positions.push_back(second_antenna); |
| 124 | + } |
| 125 | + if (!harmony_frequencies) { |
| 126 | + break; |
| 127 | + } |
| 128 | + second_antenna.first += distance_2.first; |
| 129 | + second_antenna.second += distance_2.second; |
| 130 | + } |
| 131 | + } |
| 132 | + } |
| 133 | + } |
| 134 | + |
| 135 | + return antenna_positions.size(); |
| 136 | + } |
| 137 | + |
| 138 | +private: |
| 139 | + std::unordered_map<char, std::vector<std::pair<int, int>>> frequencies; |
| 140 | + GridSize grid; |
| 141 | +}; |
| 142 | + |
| 143 | +std::string read_file(const std::string &filename) { |
| 144 | + std::ifstream file(filename); |
| 145 | + if (!file) { |
| 146 | + throw std::runtime_error("Could not open the file"); |
| 147 | + } |
| 148 | + std::stringstream buffer; |
| 149 | + buffer << file.rdbuf(); |
| 150 | + return buffer.str(); |
| 151 | +} |
| 152 | + |
| 153 | +int main() { |
| 154 | + try { |
| 155 | + std::string message = read_file("crates/day08/input.txt"); |
| 156 | + |
| 157 | + // --- Part One --- |
| 158 | + Antenna checker(message); |
| 159 | + std::cout << "Part One solution: " << checker.count_antennas(false) |
| 160 | + << std::endl; |
| 161 | + |
| 162 | + // --- Part Two --- |
| 163 | + std::cout << "Part Two solution: " << checker.count_antennas(true) |
| 164 | + << std::endl; |
| 165 | + } catch (const std::exception &e) { |
| 166 | + std::cerr << e.what() << std::endl; |
| 167 | + } |
| 168 | + |
| 169 | + return 0; |
| 170 | +} |
0 commit comments