diff options
Diffstat (limited to '2024/src')
| -rw-r--r-- | 2024/src/day08.cpp | 112 |
1 files changed, 112 insertions, 0 deletions
diff --git a/2024/src/day08.cpp b/2024/src/day08.cpp new file mode 100644 index 0000000..87e318d --- /dev/null +++ b/2024/src/day08.cpp | |||
| @@ -0,0 +1,112 @@ | |||
| 1 | #include <fstream> | ||
| 2 | #include <iostream> | ||
| 3 | #include <map> | ||
| 4 | #include <set> | ||
| 5 | #include <string> | ||
| 6 | #include <tuple> | ||
| 7 | using namespace std; | ||
| 8 | |||
| 9 | using pos_type = tuple<size_t, size_t>; | ||
| 10 | |||
| 11 | map<pos_type, char> | ||
| 12 | read_file(string_view filename) | ||
| 13 | { | ||
| 14 | fstream input{ filename }; | ||
| 15 | map<pos_type, char> data; | ||
| 16 | |||
| 17 | size_t yPos = 0; | ||
| 18 | for ( string line; getline(input, line); ) { | ||
| 19 | for ( size_t xPos = 0; xPos != line.size(); ++xPos ) { | ||
| 20 | data[{ xPos, yPos }] = line[xPos]; | ||
| 21 | } | ||
| 22 | ++yPos; | ||
| 23 | } | ||
| 24 | |||
| 25 | return data; | ||
| 26 | } | ||
| 27 | |||
| 28 | void | ||
| 29 | part1(const map<pos_type, char>& data) | ||
| 30 | { | ||
| 31 | map<char, vector<pos_type>> unique_frequencies; | ||
| 32 | for ( const auto& [position, frequency]: data ) { | ||
| 33 | if ( frequency == '.' ) { | ||
| 34 | continue; | ||
| 35 | } | ||
| 36 | unique_frequencies[frequency].emplace_back(position); | ||
| 37 | } | ||
| 38 | |||
| 39 | set<pos_type> antinodes; | ||
| 40 | for ( const auto& [frequency, positions]: unique_frequencies ) { | ||
| 41 | for ( size_t i = 0; i != positions.size(); ++i ) { | ||
| 42 | for ( size_t j = i + 1; j != positions.size(); ++j ) { | ||
| 43 | const auto [lhs_x, lhs_y] = positions[i]; | ||
| 44 | const auto [rhs_x, rhs_y] = positions[j]; | ||
| 45 | |||
| 46 | const auto delta_x = rhs_x - lhs_x; | ||
| 47 | const auto delta_y = rhs_y - lhs_y; | ||
| 48 | |||
| 49 | pos_type new_position1{ lhs_x - delta_x, lhs_y - delta_y }; | ||
| 50 | if ( data.contains(new_position1) ) { | ||
| 51 | antinodes.emplace(new_position1); | ||
| 52 | } | ||
| 53 | |||
| 54 | pos_type new_position2{ rhs_x + delta_x, rhs_y + delta_y }; | ||
| 55 | if ( data.contains(new_position2) ) { | ||
| 56 | antinodes.emplace(new_position2); | ||
| 57 | } | ||
| 58 | } | ||
| 59 | } | ||
| 60 | } | ||
| 61 | cout << antinodes.size() << endl; | ||
| 62 | } | ||
| 63 | |||
| 64 | void | ||
| 65 | part2(const map<pos_type, char>& data) | ||
| 66 | { | ||
| 67 | map<char, vector<pos_type>> unique_frequencies; | ||
| 68 | for ( const auto& [position, frequency]: data ) { | ||
| 69 | if ( frequency == '.' ) { | ||
| 70 | continue; | ||
| 71 | } | ||
| 72 | unique_frequencies[frequency].emplace_back(position); | ||
| 73 | } | ||
| 74 | |||
| 75 | set<pos_type> antinodes; | ||
| 76 | for ( const auto& [frequency, positions]: unique_frequencies ) { | ||
| 77 | for ( size_t i = 0; i != positions.size(); ++i ) { | ||
| 78 | for ( size_t j = i + 1; j != positions.size(); ++j ) { | ||
| 79 | const auto [lhs_x, lhs_y] = positions[i]; | ||
| 80 | const auto [rhs_x, rhs_y] = positions[j]; | ||
| 81 | |||
| 82 | const auto delta_x = rhs_x - lhs_x; | ||
| 83 | const auto delta_y = rhs_y - lhs_y; | ||
| 84 | |||
| 85 | for ( size_t count = 0;; ++count ) { | ||
| 86 | pos_type new_position{ lhs_x - count * delta_x, lhs_y - count * delta_y }; | ||
| 87 | if ( !data.contains(new_position) ) { | ||
| 88 | break; | ||
| 89 | } | ||
| 90 | antinodes.emplace(new_position); | ||
| 91 | } | ||
| 92 | |||
| 93 | for ( size_t count = 0;; ++count ) { | ||
| 94 | pos_type new_position{ rhs_x + count * delta_x, rhs_y + count * delta_y }; | ||
| 95 | if ( !data.contains(new_position) ) { | ||
| 96 | break; | ||
| 97 | } | ||
| 98 | antinodes.emplace(new_position); | ||
| 99 | } | ||
| 100 | } | ||
| 101 | } | ||
| 102 | } | ||
| 103 | cout << antinodes.size() << endl; | ||
| 104 | } | ||
| 105 | |||
| 106 | int | ||
| 107 | main() | ||
| 108 | { | ||
| 109 | auto data = read_file("data/day08.txt"); | ||
| 110 | part1(data); | ||
| 111 | part2(data); | ||
| 112 | } | ||
