aoc

My solutions for the Advent of Code
git clone https://git.tronto.net/aoc
Download | Log | Files | Refs | README

day08a.cpp (981B)


      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 }