Solve day 4 part 2
This commit is contained in:
parent
7b0846eb58
commit
58d158e118
49
day_4.cpp
49
day_4.cpp
@ -8,14 +8,8 @@ struct position
|
|||||||
int y;
|
int y;
|
||||||
};
|
};
|
||||||
|
|
||||||
int main()
|
int remove_paper_rolls(std::vector<std::string> &lines)
|
||||||
{
|
{
|
||||||
std::ifstream file("input.txt");
|
|
||||||
|
|
||||||
std::vector<std::string> lines;
|
|
||||||
std::string line;
|
|
||||||
while (getline(file, line)) lines.push_back(line);
|
|
||||||
|
|
||||||
const position positions[] = {
|
const position positions[] = {
|
||||||
{ -1, -1 },
|
{ -1, -1 },
|
||||||
{ 0, -1 },
|
{ 0, -1 },
|
||||||
@ -27,26 +21,57 @@ int main()
|
|||||||
{ 1, 1 },
|
{ 1, 1 },
|
||||||
};
|
};
|
||||||
|
|
||||||
int result = 0;
|
int removed_count = 0;
|
||||||
|
|
||||||
|
std::vector<std::string> new_lines;
|
||||||
|
|
||||||
for (int y = 0; y < lines.size(); y++) {
|
for (int y = 0; y < lines.size(); y++) {
|
||||||
|
new_lines.push_back("");
|
||||||
|
|
||||||
for (int x = 0; x < lines[y].size(); x++) {
|
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;
|
int amount = 0;
|
||||||
|
|
||||||
for (position pos : positions) {
|
for (position pos : positions) {
|
||||||
int check_x = x + pos.x, check_y = y + pos.y;
|
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;
|
continue;
|
||||||
|
|
||||||
if (lines[check_y][check_x] == '@') amount++;
|
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<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;
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user