diff --git a/day_4.cpp b/day_4.cpp index 6d0b7a8..df62786 100644 --- a/day_4.cpp +++ b/day_4.cpp @@ -8,14 +8,8 @@ struct position int y; }; -int main() +int remove_paper_rolls(std::vector &lines) { - std::ifstream file("input.txt"); - - std::vector lines; - std::string line; - while (getline(file, line)) lines.push_back(line); - const position positions[] = { { -1, -1 }, { 0, -1 }, @@ -27,26 +21,57 @@ int main() { 1, 1 }, }; - int result = 0; + int removed_count = 0; + + std::vector new_lines; for (int y = 0; y < lines.size(); y++) { + new_lines.push_back(""); + for (int x = 0; x < lines[y].size(); x++) { - if (lines[y][x] == '.') continue; + if (lines[y][x] == '.') { + new_lines[y].append("."); + continue; + } int amount = 0; for (position pos : positions) { int check_x = x + pos.x, check_y = y + pos.y; - if (check_x < 0 || check_y < 0 || check_x >= lines[y].size() || check_y >= line.size()) + if (check_x < 0 || check_y < 0 || check_x >= lines[y].size() || check_y >= lines.size()) continue; if (lines[check_y][check_x] == '@') amount++; } - if (amount < 4) result++; + if (amount < 4) { + new_lines[y].append("."); + removed_count++; + } else { + new_lines[y].append("@"); + } } } - std::cout << result << std::endl; + lines = new_lines; + + return removed_count; +} + +int main() +{ + std::ifstream file("input.txt"); + + std::vector lines; + std::string line; + while (getline(file, line)) lines.push_back(line); + + int removed = 0, total_removed = 0; + do { + removed = remove_paper_rolls(lines); + total_removed += removed; + } while (removed > 0); + + std::cout << total_removed << std::endl; }