diff --git a/day_4.cpp b/day_4.cpp new file mode 100644 index 0000000..6d0b7a8 --- /dev/null +++ b/day_4.cpp @@ -0,0 +1,52 @@ +#include +#include +#include + +struct position +{ + int x; + int y; +}; + +int main() +{ + 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 }, + { 1, -1 }, + { -1, 0 }, + { 1, 0 }, + { -1, 1 }, + { 0, 1 }, + { 1, 1 }, + }; + + int result = 0; + + for (int y = 0; y < lines.size(); y++) { + for (int x = 0; x < lines[y].size(); x++) { + if (lines[y][x] == '.') 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()) + continue; + + if (lines[check_y][check_x] == '@') amount++; + } + + if (amount < 4) result++; + } + } + + std::cout << result << std::endl; +}