advent-of-code/2024/7/main-1.cpp

47 lines
684 B
C++
Raw Permalink Normal View History

2024-12-06 21:28:50 -08:00
#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++) {
if (b & 1) {
v += ns[i];
} else {
v *= ns[i];
}
b >>= 1;
}
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 = 1 << (ns.size() - 1);
for (ull b = 0; b <= btot; b++) {
if (testn(ns, b) == n) {
sum += n;
break;
}
}
}
cout << sum << '\n';
}