aboutsummaryrefslogtreecommitdiff
path: root/2024/09/day09a.cpp
diff options
context:
space:
mode:
authorSebastiano Tronto <sebastiano@tronto.net>2024-12-09 06:36:25 +0100
committerSebastiano Tronto <sebastiano@tronto.net>2024-12-09 06:36:25 +0100
commit93f9d02e978d10b4ff02ac029b6a6eff3d3193cf (patch)
tree9ba15fe751f8ff4d94d7995191b76286e0d83343 /2024/09/day09a.cpp
parenta299e1723f0a700a892720badab2e06a202d1eda (diff)
downloadaoc-93f9d02e978d10b4ff02ac029b6a6eff3d3193cf.tar.gz
aoc-93f9d02e978d10b4ff02ac029b6a6eff3d3193cf.zip
Day 9 2024
Diffstat (limited to '2024/09/day09a.cpp')
-rw-r--r--2024/09/day09a.cpp32
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>
9using namespace std;
10
11int 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}

Generated with cgit - Back to sebastiano.tronto.net