From f9004ec636659fc6fa5a977248a22cf74e3d35bc Mon Sep 17 00:00:00 2001 From: Orfeas <38209077+0xfea5@users.noreply.github.com> Date: Sat, 30 Aug 2025 12:53:06 +0300 Subject: day01: use structure binding in range for loop --- day01/solution.cpp | 9 +++++---- 1 file 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 @@ #include namespace ranges = std::ranges; +namespace views = std::views; const auto parse_input() { std::pair, std::vector> result; @@ -28,8 +29,8 @@ void part1(auto input) { ranges::sort(B); int sum = 0; - for (auto [x, y] : std::views::zip(A, B)) { - sum += std::abs(x - y); + for (const auto& [a, b] : views::zip(A, B)) { + sum += std::abs(a - b); } std::println("{}", sum); @@ -47,8 +48,8 @@ void part2(auto input) { } int sum = 0; - for (auto a : freq_A) { - sum += a.first * a.second * freq_B[a.first]; + for (const auto& [a, b] : freq_A) { + sum += a * b * freq_B[a]; } std::println("{}", sum); -- cgit v1.2.3