diff options
Diffstat (limited to '2024/09/day09a.cpp')
| -rw-r--r-- | 2024/09/day09a.cpp | 32 |
1 files changed, 32 insertions, 0 deletions
diff --git a/2024/09/day09a.cpp b/2024/09/day09a.cpp new file mode 100644 index 0000000..c52c8f0 --- /dev/null +++ b/2024/09/day09a.cpp | |||
| @@ -0,0 +1,32 @@ | |||
| 1 | #include <algorithm> | ||
| 2 | #include <iostream> | ||
| 3 | #include <map> | ||
| 4 | #include <set> | ||
| 5 | #include <sstream> | ||
| 6 | #include <string> | ||
| 7 | #include <string_view> | ||
| 8 | #include <vector> | ||
| 9 | using namespace std; | ||
| 10 | |||
| 11 | int main() { | ||
| 12 | vector<long long> a; | ||
| 13 | char c; | ||
| 14 | for (int id = 0; cin >> c; id++) | ||
| 15 | for (int j = 0; j < c-'0'; j++) | ||
| 16 | a.push_back(id % 2 == 0 ? id/2 : -1); // -1 = space | ||
| 17 | |||
| 18 | int j = 0; | ||
| 19 | for (int i = a.size()-1; i >= 0; i--) { | ||
| 20 | while (a[j] >= 0) j++; | ||
| 21 | if (a[j] == -2) break; | ||
| 22 | a[j] = a[i]; | ||
| 23 | a[i] = -2; // -2 = freed up | ||
| 24 | } | ||
| 25 | |||
| 26 | long long checksum = 0; | ||
| 27 | for (long long i = 0; i < (long long)a.size() && a[i] != -2; i++) | ||
| 28 | checksum += i*a[i]; | ||
| 29 | cout << checksum << endl; | ||
| 30 | |||
| 31 | return 0; | ||
| 32 | } | ||
