Advent of Code 2025, done in C++

day[04]: simplified the solutions

bpavuk.neocities.org 2c12894f 44b52399

verified
Changed files
+24 -38
src
+24 -38
src/04/solution.cxx
··· 1 1 #include <filesystem> 2 2 #include <fstream> 3 3 #include <print> 4 + #include <span> 4 5 #include <sstream> 5 6 #include <string> 6 7 #include <vector> ··· 64 65 std::vector<std::string> data{}; 65 66 }; 66 67 67 - unsigned int count_removable_rolls(Matrix &data) { 68 - auto out = 0; 68 + struct Roll { 69 + unsigned long row; 70 + unsigned long col; 71 + }; 72 + 73 + std::vector<Roll> get_removable_rolls(Matrix &data) { 74 + std::vector<Roll> rolls{}; 69 75 for (unsigned long row = 0; row < data.height(); ++row) { 70 76 for (unsigned long col = 0; col < data.width(); ++col) { 71 77 if (data[row][col] != '@') { 72 - std::print("{}", data[row][col]); 73 78 continue; 74 79 } 75 80 ··· 81 86 } 82 87 } 83 88 if (count >= 4) { 84 - std::print("{}", data[row][col]); 85 89 continue; 86 90 } 87 - std::print("x"); 88 - out += 1; 91 + auto roll = Roll { 92 + .row = row, 93 + .col = col, 94 + }; 95 + rolls.push_back(roll); 89 96 } 90 - std::println(); 91 97 } 92 98 93 - return out; 99 + return rolls; 94 100 } 95 101 96 - int remove_rolls(Matrix& data) { 97 - auto removed = 0; 98 - 99 - for (unsigned long row = 0; row < data.height(); ++row) { 100 - for (unsigned long col = 0; col < data.width(); ++col) { 101 - if (data[row][col] != '@') { 102 - continue; 103 - } 104 - 105 - auto sur = data.get_surrounding(row, col); 106 - int count = 0; 107 - for (auto var : sur) { 108 - if (var == '@') { 109 - count += 1; 110 - } 111 - } 112 - if (count >= 4) { 113 - continue; 114 - } 115 - data.set(row, col, '.'); 116 - removed += 1; 117 - } 102 + int remove_rolls(Matrix &data, std::span<Roll> removables) { 103 + for (auto roll : removables) { 104 + data.set(roll.row, roll.col, '.'); 118 105 } 119 106 120 - return removed; 107 + return removables.size(); 121 108 } 122 109 123 110 int main() { ··· 134 121 file.read(input.data(), input_size); 135 122 136 123 Matrix data(input); 124 + auto removables = get_removable_rolls(data); 137 125 138 - long long password_part_1 = count_removable_rolls(data); 126 + long long password_part_1 = removables.size(); 139 127 long long password_part_2 = 0; 140 128 141 - auto removables = count_removable_rolls(data); 142 - while (removables > 0) { 143 - std::println(); 129 + while (removables.size() > 0) { 130 + password_part_2 += removables.size(); 131 + remove_rolls(data, removables); 144 132 145 - password_part_2 += remove_rolls(data); 146 - 147 - removables = count_removable_rolls(data); 133 + removables = get_removable_rolls(data); 148 134 } 149 135 150 136 std::println("Eureka! {} / {}", password_part_1, password_part_2);