diff options
| -rw-r--r-- | 2024/09/day09a.cpp | 7 | ||||
| -rw-r--r-- | 2024/09/day09b.cpp | 17 | ||||
| -rw-r--r-- | 2024/learned.txt | 1 | ||||
| -rw-r--r-- | 2024/template.cpp | 1 |
4 files changed, 15 insertions, 11 deletions
diff --git a/2024/09/day09a.cpp b/2024/09/day09a.cpp index c52c8f0..80732d7 100644 --- a/2024/09/day09a.cpp +++ b/2024/09/day09a.cpp | |||
| @@ -1,6 +1,7 @@ | |||
| 1 | #include <algorithm> | 1 | #include <algorithm> |
| 2 | #include <iostream> | 2 | #include <iostream> |
| 3 | #include <map> | 3 | #include <map> |
| 4 | #include <ranges> | ||
| 4 | #include <set> | 5 | #include <set> |
| 5 | #include <sstream> | 6 | #include <sstream> |
| 6 | #include <string> | 7 | #include <string> |
| @@ -16,11 +17,11 @@ int main() { | |||
| 16 | a.push_back(id % 2 == 0 ? id/2 : -1); // -1 = space | 17 | a.push_back(id % 2 == 0 ? id/2 : -1); // -1 = space |
| 17 | 18 | ||
| 18 | int j = 0; | 19 | int j = 0; |
| 19 | for (int i = a.size()-1; i >= 0; i--) { | 20 | for (auto& x : a | views::reverse) { |
| 20 | while (a[j] >= 0) j++; | 21 | while (a[j] >= 0) j++; |
| 21 | if (a[j] == -2) break; | 22 | if (a[j] == -2) break; |
| 22 | a[j] = a[i]; | 23 | a[j] = x; |
| 23 | a[i] = -2; // -2 = freed up | 24 | x = -2; // -2 = freed up |
| 24 | } | 25 | } |
| 25 | 26 | ||
| 26 | long long checksum = 0; | 27 | long long checksum = 0; |
diff --git a/2024/09/day09b.cpp b/2024/09/day09b.cpp index 3b1ac21..c304ed4 100644 --- a/2024/09/day09b.cpp +++ b/2024/09/day09b.cpp | |||
| @@ -1,6 +1,7 @@ | |||
| 1 | #include <algorithm> | 1 | #include <algorithm> |
| 2 | #include <iostream> | 2 | #include <iostream> |
| 3 | #include <map> | 3 | #include <map> |
| 4 | #include <ranges> | ||
| 4 | #include <set> | 5 | #include <set> |
| 5 | #include <sstream> | 6 | #include <sstream> |
| 6 | #include <string> | 7 | #include <string> |
| @@ -21,16 +22,16 @@ int main() { | |||
| 21 | a.push_back(id % 2 == 0 ? id/2 : -1); // -1 = space | 22 | a.push_back(id % 2 == 0 ? id/2 : -1); // -1 = space |
| 22 | } | 23 | } |
| 23 | 24 | ||
| 24 | for (int i = file.size()-1; i >= 0; i--) { | 25 | for (auto& p : file | views::reverse) { |
| 25 | for (auto& f : freesp) { | 26 | for (auto& f : freesp) { |
| 26 | if (f.first >= file[i].first) break; | 27 | if (f.first >= p.first) break; |
| 27 | if (f.second >= file[i].second) { | 28 | if (f.second >= p.second) { |
| 28 | for (int k = 0; k < file[i].second; k++) { | 29 | for (int k = 0; k < p.second; k++) { |
| 29 | a[f.first+k] = a[file[i].first+k]; | 30 | a[f.first+k] = a[p.first+k]; |
| 30 | a[file[i].first+k] = -2; | 31 | a[p.first+k] = -2; |
| 31 | } | 32 | } |
| 32 | f.first += file[i].second; | 33 | f.first += p.second; |
| 33 | f.second -= file[i].second; | 34 | f.second -= p.second; |
| 34 | break; | 35 | break; |
| 35 | } | 36 | } |
| 36 | } | 37 | } |
diff --git a/2024/learned.txt b/2024/learned.txt index 86d32ff..56fa515 100644 --- a/2024/learned.txt +++ b/2024/learned.txt | |||
| @@ -7,3 +7,4 @@ List of things I learned (or refreshed) with this year's AoC. | |||
| 7 | to assign two variables at once with a pair of references pair<int&, int&> | 7 | to assign two variables at once with a pair of references pair<int&, int&> |
| 8 | * Day 5: std::find and std::replace | 8 | * Day 5: std::find and std::replace |
| 9 | * Day 8: set::insert_range(), but it is from C++23 only | 9 | * Day 8: set::insert_range(), but it is from C++23 only |
| 10 | * Day 9: std::views | ||
diff --git a/2024/template.cpp b/2024/template.cpp index b2fd4ec..2475ae7 100644 --- a/2024/template.cpp +++ b/2024/template.cpp | |||
| @@ -1,6 +1,7 @@ | |||
| 1 | #include <algorithm> | 1 | #include <algorithm> |
| 2 | #include <iostream> | 2 | #include <iostream> |
| 3 | #include <map> | 3 | #include <map> |
| 4 | #include <ranges> | ||
| 4 | #include <set> | 5 | #include <set> |
| 5 | #include <sstream> | 6 | #include <sstream> |
| 6 | #include <string> | 7 | #include <string> |
