Advent of Code 2025, done in C++

day[05]: cleaned up the solution

bpavuk.neocities.org da863200 007ebb14

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