aoc

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

day05a.cpp (991B)


      1 #include <algorithm>
      2 #include <iostream>
      3 #include <sstream>
      4 #include <string>
      5 #include <string_view>
      6 #include <vector>
      7 using namespace std;
      8 
      9 vector<int> readints(string &s) {
     10 	vector<int> v;
     11 	stringstream sin(s);
     12 	int x;
     13 
     14 	while (sin >> x)
     15 		v.push_back(x);
     16 
     17 	return v;
     18 }
     19 
     20 bool valid(vector<int> &v, vector<pair<int, int>> &rules) {
     21 	for (unsigned i = 0; i < v.size(); i++)
     22 		for (unsigned j = i+1; j < v.size(); j++)
     23 			for (auto r : rules)
     24 				if (v[i] == r.second && v[j] == r.first)
     25 					return false;
     26 	return true;
     27 }
     28 
     29 int main() {
     30 	string line;
     31 	vector<pair<int, int>> rules;
     32 	while (getline(cin, line)) {
     33 		if (line == "")
     34 			break;
     35 
     36 		replace(line.begin(), line.end(), '|', ' ');
     37 		auto v = readints(line);
     38 		int x = v[0];
     39 		int y = v[1];
     40 		rules.push_back(make_pair(x, y));
     41 	}
     42 
     43 
     44 	int tot = 0;
     45 	while (getline(cin, line)) {
     46 		replace(line.begin(), line.end(), ',', ' ');
     47 		auto v = readints(line);
     48 		if (valid(v, rules))
     49 			tot += v[v.size()/2];
     50 	}
     51 	cout << tot << endl;
     52 	return 0;
     53 }