2024 day 7

This commit is contained in:
eriedaberrie 2024-12-06 21:28:50 -08:00
parent 06b7ac26cf
commit a311e1d88a
2 changed files with 98 additions and 0 deletions

46
2024/7/main-1.cpp Normal file
View file

@ -0,0 +1,46 @@
#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';
}

52
2024/7/main-2.cpp Normal file
View file

@ -0,0 +1,52 @@
#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';
}