diff options
Diffstat (limited to '2024/07/day07a.cpp')
| -rw-r--r-- | 2024/07/day07a.cpp | 34 |
1 files changed, 34 insertions, 0 deletions
diff --git a/2024/07/day07a.cpp b/2024/07/day07a.cpp new file mode 100644 index 0000000..3b56dea --- /dev/null +++ b/2024/07/day07a.cpp | |||
| @@ -0,0 +1,34 @@ | |||
| 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 | } | ||
