advent-of-code/2024/8/main-2.cpp

51 lines
1,008 B
C++
Raw Permalink Normal View History

2024-12-07 21:52:31 -08:00
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
map<char, vector<pair<int, int>>> nodes;
string line;
vector<string> lines;
while (getline(cin, line) && !line.empty()) {
lines.push_back(line);
}
int mx = lines.size(), my = lines[0].size();
for (int x = 0; x < mx; x++) {
for (int y = 0; y < my; y++) {
char c = lines[x][y];
if (c != '.') {
nodes[c].push_back({x, y});
}
}
}
set<pair<int, int>> total;
for (auto &[c, poss] : nodes) {
for (int a = 1; a < poss.size(); a++) {
for (int b = 0; b < a; b++) {
int dx = poss[a].first - poss[b].first;
int dy = poss[a].second - poss[b].second;
int x = poss[a].first, y = poss[a].second;
while (x >= 0 && x < mx && y >= 0 && y < my) {
x -= dx;
y -= dy;
}
for (;;) {
x += dx;
y += dy;
if (!(x >= 0 && x < mx && y >= 0 && y < my)) {
break;
}
total.insert({x, y});
}
}
}
}
cout << total.size() << '\n';
}