From 93f9d02e978d10b4ff02ac029b6a6eff3d3193cf Mon Sep 17 00:00:00 2001 From: Sebastiano Tronto Date: Mon, 9 Dec 2024 06:36:25 +0100 Subject: Day 9 2024 --- 2024/09/day09a.cpp | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 2024/09/day09a.cpp (limited to '2024/09/day09a.cpp') 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 @@ +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; + +int main() { + vector a; + char c; + for (int id = 0; cin >> c; id++) + for (int j = 0; j < c-'0'; j++) + a.push_back(id % 2 == 0 ? id/2 : -1); // -1 = space + + int j = 0; + for (int i = a.size()-1; i >= 0; i--) { + while (a[j] >= 0) j++; + if (a[j] == -2) break; + a[j] = a[i]; + a[i] = -2; // -2 = freed up + } + + long long checksum = 0; + for (long long i = 0; i < (long long)a.size() && a[i] != -2; i++) + checksum += i*a[i]; + cout << checksum << endl; + + return 0; +} -- cgit v1.3