aoc

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

day25a.cpp (970B)


      1 #include <algorithm>
      2 #include <cstdint>
      3 #include <iostream>
      4 #include <map>
      5 #include <queue>
      6 #include <ranges>
      7 #include <set>
      8 #include <sstream>
      9 #include <string>
     10 #include <string_view>
     11 #include <vector>
     12 using namespace std;
     13 
     14 class KeyLock {
     15 public:
     16 	KeyLock() { fill(a, a+5, 0); }
     17 
     18 	void addrow(const string& s) {
     19 		for (int i = 0; i < 5; i++)
     20 			a[i] += s[i] == '#';
     21 	}
     22 
     23 	bool operator&(const KeyLock& other) const {
     24 		for (int i = 0; i < 5; i++)
     25 			if (a[i] + other.a[i] > 5)
     26 				return false;
     27 		return true;
     28 	}
     29 private:
     30 	int a[5];
     31 };
     32 
     33 int main() {
     34 	vector<KeyLock> keys, locks;
     35 	string line;
     36 	while (getline(cin, line)) {
     37 		KeyLock k;
     38 		bool key = line[0] == '.';
     39 		for (int i = 0; i < 5; i++) {
     40 			getline(cin, line);
     41 			k.addrow(line);
     42 		}
     43 		if (key) keys.push_back(k);
     44 		else locks.push_back(k);
     45 		getline(cin, line);
     46 		getline(cin, line);
     47 	}
     48 
     49 	int tot = 0;
     50 	for (auto k : keys)
     51 		for (auto l : locks)
     52 			tot += (k & l) ? 1 : 0;
     53 	cout << tot << endl;
     54 	return 0;
     55 }