diff options
| author | Sebastiano Tronto <sebastiano@tronto.net> | 2024-12-07 07:51:09 +0100 |
|---|---|---|
| committer | Sebastiano Tronto <sebastiano@tronto.net> | 2024-12-07 07:51:09 +0100 |
| commit | 79b905aea4bbe0c15c448f0af3f8cc733f763617 (patch) | |
| tree | 4cee2342949b7723328bb6fc2c2d079a2a9164cc /2024/07/day07a.cpp | |
| parent | c2b969794f3e981d46852d7070cc64802be99e0d (diff) | |
| download | aoc-79b905aea4bbe0c15c448f0af3f8cc733f763617.tar.gz aoc-79b905aea4bbe0c15c448f0af3f8cc733f763617.zip | |
Day 7 2024
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 | } | ||
