aboutsummaryrefslogtreecommitdiff
path: root/2024/09
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
parenta299e1723f0a700a892720badab2e06a202d1eda (diff)
downloadaoc-93f9d02e978d10b4ff02ac029b6a6eff3d3193cf.tar.gz
aoc-93f9d02e978d10b4ff02ac029b6a6eff3d3193cf.zip
Day 9 2024
Diffstat (limited to '2024/09')
-rw-r--r--2024/09/Makefile24
-rw-r--r--2024/09/day09a.cpp32
-rw-r--r--2024/09/day09b.cpp46
3 files changed, 102 insertions, 0 deletions
diff --git a/2024/09/Makefile b/2024/09/Makefile
new file mode 100644
index 0000000..89cd1b9
--- /dev/null
+++ b/2024/09/Makefile
@@ -0,0 +1,24 @@
1CC=g++ -std=c++20 -g -Wall
2
3a:
4 ${CC} -o a.out day09a.cpp
5
6b:
7 ${CC} -o b.out day09b.cpp
8
9clean:
10 rm -f a b
11
12atest: a
13 ./a.out
14
15btest: b
16 ./b.out
17
18arun: a
19 ./a.out < input
20
21brun: b
22 ./b.out < input
23
24.PHONY: a b clean atest btest arun brun
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}
diff --git a/2024/09/day09b.cpp b/2024/09/day09b.cpp
new file mode 100644
index 0000000..3b1ac21
--- /dev/null
+++ b/2024/09/day09b.cpp
@@ -0,0 +1,46 @@
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 vector<pair<int, int>> freesp, file;
14 char c;
15 for (int id = 0; cin >> c; id++) {
16 if (id % 2 == 1)
17 freesp.push_back(make_pair(a.size(), c-'0'));
18 else
19 file.push_back(make_pair(a.size(), c-'0'));
20 for (int j = 0; j < c-'0'; j++)
21 a.push_back(id % 2 == 0 ? id/2 : -1); // -1 = space
22 }
23
24 for (int i = file.size()-1; i >= 0; i--) {
25 for (auto& f : freesp) {
26 if (f.first >= file[i].first) break;
27 if (f.second >= file[i].second) {
28 for (int k = 0; k < file[i].second; k++) {
29 a[f.first+k] = a[file[i].first+k];
30 a[file[i].first+k] = -2;
31 }
32 f.first += file[i].second;
33 f.second -= file[i].second;
34 break;
35 }
36 }
37 }
38
39 long long checksum = 0;
40 for (long long i = 0; i < (long long)a.size(); i++)
41 if (a[i] >= 0)
42 checksum += i*a[i];
43 cout << checksum << endl;
44
45 return 0;
46}

Generated with cgit - Back to sebastiano.tronto.net