From a299e1723f0a700a892720badab2e06a202d1eda Mon Sep 17 00:00:00 2001 From: Sebastiano Tronto Date: Sun, 8 Dec 2024 07:49:46 +0100 Subject: Day 8 2024 --- 2024/08/day08b.cpp | 68 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 2024/08/day08b.cpp (limited to '2024/08/day08b.cpp') diff --git a/2024/08/day08b.cpp b/2024/08/day08b.cpp new file mode 100644 index 0000000..1d1381f --- /dev/null +++ b/2024/08/day08b.cpp @@ -0,0 +1,68 @@ +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; + +#define N 50 +#define M 50 + +bool in_range(int x, int y) { + return x >= 0 && x < N && y >= 0 && y < M; +} + +vector> antinode_all(pair p, pair q) { + auto [xp, yp] = p; + auto [xq, yq] = q; + /* In theory these two coefficients must be divided by the gcd of + * of the coordinates, which leads to possibly more points, + * as far as I understood the problem statement. Apparently, this + * is not the case. */ + int gx = (xp - xq); + int gy = (yp - yq); + vector> v; + for (int i = 0; in_range(xp+i*gx, yp+i*gy); i++) + v.push_back(make_pair(xp+i*gx, yp+i*gy)); + for (int i = 0; in_range(xq-i*gx, yq-i*gy); i++) + v.push_back(make_pair(xq-i*gx, yq-i*gy)); + return v; +} + +void add_antinodes(set>& points, + pair p, set>& antinodes) { + for (auto q : points) { + auto a = antinode_all(p, q); + for (auto r : a) + antinodes.insert(r); + } +} + +int main() { + string line; + map>> d; + set> antinodes; + int i, j; + for (i = 0; getline(cin, line); i++) { + stringstream s(line); + char c; + for (j = 0; s >> c; j++) { + if (c != '.') { + pair p(i, j); + add_antinodes(d[c], p, antinodes); + d[c].insert(p); + } + } + } + + int tot = 0; + for (auto [x, y] : antinodes) + if (in_range(x, y)) + tot++; + + cout << tot << endl; + return 0; +} -- cgit v1.3