adventofcode-2025/day_4.cpp
2025-12-18 13:54:10 +01:00

78 lines
1.3 KiB
C++

#include <fstream>
#include <iostream>
#include <vector>
struct position
{
int x;
int y;
};
int remove_paper_rolls(std::vector<std::string> &lines)
{
const position positions[] = {
{ -1, -1 },
{ 0, -1 },
{ 1, -1 },
{ -1, 0 },
{ 1, 0 },
{ -1, 1 },
{ 0, 1 },
{ 1, 1 },
};
int removed_count = 0;
std::vector<std::string> 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] == '.') {
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 >= lines.size())
continue;
if (lines[check_y][check_x] == '@') amount++;
}
if (amount < 4) {
new_lines[y].append(".");
removed_count++;
} else {
new_lines[y].append("@");
}
}
}
lines = new_lines;
return removed_count;
}
int main()
{
std::ifstream file("input.txt");
std::vector<std::string> 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;
}