advent-of-code/2024/7/main-2.cpp
2024-12-06 21:28:50 -08:00

53 lines
784 B
C++

#include <bits/stdc++.h>
using namespace std;
typedef unsigned long long ull;
inline ull testn(vector<int> &ns, ull b) {
ull v = ns[0];
for (int i = 1; i < ns.size(); i++) {
switch (b % 3) {
case 0:
v += ns[i];
break;
case 1:
v *= ns[i];
break;
case 2:
v *= pow(10, (int)log10(ns[i]) + 1);
v += ns[i];
}
b /= 3;
}
return v;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
ull sum = 0;
string line;
while (getline(cin, line) && !line.empty()) {
istringstream l(line);
ull n;
int a;
vector<int> ns;
l >> n;
l.ignore();
while (l >> a) {
ns.push_back(a);
}
ull btot = pow(3, (ns.size() - 1));
for (ull b = 0; b <= btot; b++) {
if (testn(ns, b) == n) {
sum += n;
break;
}
}
}
cout << sum << '\n';
}