diff options
| author | Orfeas <38209077+0xfea5@users.noreply.github.com> | 2025-08-30 12:53:06 +0300 |
|---|---|---|
| committer | Orfeas <38209077+0xfea5@users.noreply.github.com> | 2025-09-01 04:51:37 +0300 |
| commit | f9004ec636659fc6fa5a977248a22cf74e3d35bc (patch) | |
| tree | 394fca114c6b81dbf319166eb8bd0dd3eb81209f | |
| parent | aoc.mk: use g++15 and C++26 (diff) | |
| download | aoc24-f9004ec636659fc6fa5a977248a22cf74e3d35bc.tar.gz aoc24-f9004ec636659fc6fa5a977248a22cf74e3d35bc.zip | |
day01: use structure binding in range for loop
| -rw-r--r-- | day01/solution.cpp | 9 |
1 files changed, 5 insertions, 4 deletions
diff --git a/day01/solution.cpp b/day01/solution.cpp index 78535a3..86d4efb 100644 --- a/day01/solution.cpp +++ b/day01/solution.cpp | |||
| @@ -9,6 +9,7 @@ | |||
| 9 | #include <utility> | 9 | #include <utility> |
| 10 | 10 | ||
| 11 | namespace ranges = std::ranges; | 11 | namespace ranges = std::ranges; |
| 12 | namespace views = std::views; | ||
| 12 | 13 | ||
| 13 | const auto parse_input() { | 14 | const auto parse_input() { |
| 14 | std::pair<std::vector<int>, std::vector<int>> result; | 15 | std::pair<std::vector<int>, std::vector<int>> result; |
| @@ -28,8 +29,8 @@ void part1(auto input) { | |||
| 28 | ranges::sort(B); | 29 | ranges::sort(B); |
| 29 | 30 | ||
| 30 | int sum = 0; | 31 | int sum = 0; |
| 31 | for (auto [x, y] : std::views::zip(A, B)) { | 32 | for (const auto& [a, b] : views::zip(A, B)) { |
| 32 | sum += std::abs(x - y); | 33 | sum += std::abs(a - b); |
| 33 | } | 34 | } |
| 34 | 35 | ||
| 35 | std::println("{}", sum); | 36 | std::println("{}", sum); |
| @@ -47,8 +48,8 @@ void part2(auto input) { | |||
| 47 | } | 48 | } |
| 48 | 49 | ||
| 49 | int sum = 0; | 50 | int sum = 0; |
| 50 | for (auto a : freq_A) { | 51 | for (const auto& [a, b] : freq_A) { |
| 51 | sum += a.first * a.second * freq_B[a.first]; | 52 | sum += a * b * freq_B[a]; |
| 52 | } | 53 | } |
| 53 | 54 | ||
| 54 | std::println("{}", sum); | 55 | std::println("{}", sum); |
