Advent of Code 2025, done in C++

day[05]: cleaned up the solution

bpavuk.neocities.org da863200 007ebb14

verified
Changed files
+13 -32
src
+13 -32
src/05/solution.cxx
··· 1 1 #include "common/getinputpath.h" 2 2 #include <algorithm> 3 - #include <cstddef> 4 3 #include <filesystem> 5 4 #include <fstream> 6 5 #include <print> 7 - #include <set> 6 + #include <ranges> 8 7 #include <sstream> 9 8 #include <string> 10 9 #include <vector> ··· 29 28 } 30 29 } 31 30 32 - auto all_values() { 33 - auto real_from = std::min(from, to); 34 - auto real_to = std::max(from, to); 35 - 36 - std::set<long long> out{}; 37 - if (inclusive) { 38 - for (auto i = real_from; i <= real_to; ++i) { 39 - out.insert(i); 40 - } 41 - } 42 - 43 - return out; 44 - } 45 - 46 31 long long size() { 47 32 auto real_from = std::min(from, to); 48 33 auto real_to = std::max(from, to); ··· 85 70 // burning the CPU in attempts to occupy as less RAM at peak as possible 86 71 // fuck OpenAI 87 72 bool skip = false; 88 - for (size_t i = 0; i < ranges.size(); ++i) { 89 - auto range = ranges[i]; 90 - 73 + for (auto [i, range] : std::ranges::views::enumerate(ranges)) { 91 74 bool left_in_range = left >= range.from && left <= range.to; 92 75 bool right_in_range = right >= range.from && right <= range.to; 93 76 if (left_in_range && right_in_range) { ··· 123 106 std::sort(ranges.begin(), ranges.end(), 124 107 [](Range one, Range other) { return one.from < other.from; }); 125 108 126 - std::vector<Range> merged_ranges{}; 127 - merged_ranges.push_back(ranges[0]); 128 - for (size_t i = 1; i < ranges.size(); ++i) { 129 - auto &last = merged_ranges.back(); 130 - auto &curr = ranges[i]; 109 + std::vector<Range> merged_ranges = std::ranges::fold_left( 110 + ranges, std::vector<Range>(), [](std::vector<Range> acc, Range range) { 111 + if (acc.empty() || acc.back().to < range.from) { 112 + acc.push_back(range); 113 + } else { 114 + acc.back().to = std::max(range.to, acc.back().to); 115 + } 131 116 132 - if (curr.from <= last.to) { 133 - last.to = std::max(last.to, curr.to); 134 - } else { 135 - merged_ranges.push_back(curr); 136 - } 137 - } 117 + return acc; 118 + }); 138 119 139 120 std::vector<long long> ids{}; 140 121 { ··· 147 128 148 129 long long password_part_1 = 0; 149 130 150 - for (auto id : ids) { 131 + std::ranges::for_each(ids, [&merged_ranges, &password_part_1](long long id) { 151 132 bool is_fresh = false; // guilty until proven innocent! 152 133 for (auto range : merged_ranges) { 153 134 is_fresh = range.contains(id); ··· 158 139 if (is_fresh) { 159 140 password_part_1 += 1; 160 141 } 161 - } 142 + }); 162 143 163 144 long long password_part_2 = 0; 164 145