diff options
| author | Sebastiano Tronto <sebastiano@tronto.net> | 2024-12-08 07:49:46 +0100 |
|---|---|---|
| committer | Sebastiano Tronto <sebastiano@tronto.net> | 2024-12-08 07:49:46 +0100 |
| commit | a299e1723f0a700a892720badab2e06a202d1eda (patch) | |
| tree | 474675a9361b5c7ee805d60972f140fb81380366 /2024/08/day08a.cpp | |
| parent | b3231ba7503f551f09b588355dd9f8145282709c (diff) | |
| download | aoc-a299e1723f0a700a892720badab2e06a202d1eda.tar.gz aoc-a299e1723f0a700a892720badab2e06a202d1eda.zip | |
Day 8 2024
Diffstat (limited to '')
| -rw-r--r-- | 2024/08/day08a.cpp | 49 |
1 files changed, 49 insertions, 0 deletions
diff --git a/2024/08/day08a.cpp b/2024/08/day08a.cpp new file mode 100644 index 0000000..7a18ded --- /dev/null +++ b/2024/08/day08a.cpp | |||
| @@ -0,0 +1,49 @@ | |||
| 1 | #include <algorithm> | ||
| 2 | #include <iostream> | ||
| 3 | #include <map> | ||
| 4 | #include <set> | ||
| 5 | #include <sstream> | ||
| 6 | #include <string> | ||
| 7 | #include <string_view> | ||
| 8 | #include <vector> | ||
| 9 | using namespace std; | ||
| 10 | |||
| 11 | pair<int, int> antinode(pair<int, int> p, pair<int, int> q) { | ||
| 12 | auto [xp, yp] = p; | ||
| 13 | auto [xq, yq] = q; | ||
| 14 | return make_pair(2*xp-xq, 2*yp-yq); | ||
| 15 | } | ||
| 16 | |||
| 17 | void add_antinodes(set<pair<int, int>>& points, | ||
| 18 | pair<int, int> p, set<pair<int, int>>& antinodes) { | ||
| 19 | for (auto q : points) { | ||
| 20 | antinodes.insert(antinode(p, q)); | ||
| 21 | antinodes.insert(antinode(q, p)); | ||
| 22 | } | ||
| 23 | } | ||
| 24 | |||
| 25 | int main() { | ||
| 26 | string line; | ||
| 27 | map<char, set<pair<int, int>>> d; | ||
| 28 | set<pair<int, int>> antinodes; | ||
| 29 | int i, j; | ||
| 30 | for (i = 0; getline(cin, line); i++) { | ||
| 31 | stringstream s(line); | ||
| 32 | char c; | ||
| 33 | for (j = 0; s >> c; j++) { | ||
| 34 | if (c != '.') { | ||
| 35 | pair<int, int> p(i, j); | ||
| 36 | add_antinodes(d[c], p, antinodes); | ||
| 37 | d[c].insert(p); | ||
| 38 | } | ||
| 39 | } | ||
| 40 | } | ||
| 41 | |||
| 42 | int tot = 0; | ||
| 43 | for (auto [x, y] : antinodes) | ||
| 44 | if (x >= 0 && x < i && y >= 0 && y < j) | ||
| 45 | tot++; | ||
| 46 | |||
| 47 | cout << tot << endl; | ||
| 48 | return 0; | ||
| 49 | } | ||
