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