aoc

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

day07a.cpp (666B)


      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 bool possible(long long x, vector<long long>& a, int i) {
     10 	if (i == 0)
     11 		return a[0] == x;
     12 	if (a[i] != 0 && x % a[i] == 0)
     13 		return possible(x/a[i], a, i-1) || possible(x-a[i], a, i-1);
     14 	return possible(x-a[i], a, i-1);
     15 }
     16 
     17 int main() {
     18 	string line;
     19 	long long tot = 0;
     20 	while (getline(cin, line)) {
     21 		long long x, i;
     22 		vector<long long> a;
     23 		stringstream s(line);
     24 		s >> x;
     25 		char colon;
     26 		s >> colon;
     27 		while (s >> i)
     28 			a.push_back(i);
     29 		if (possible(x, a, a.size()-1))
     30 			tot += x;
     31 	}
     32 	cout << tot << endl;
     33 	return 0;
     34 }