Solve day 4 part 1
This commit is contained in:
parent
1fce163767
commit
7b0846eb58
52
day_4.cpp
Normal file
52
day_4.cpp
Normal file
@ -0,0 +1,52 @@
|
|||||||
|
#include <fstream>
|
||||||
|
#include <iostream>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
struct position
|
||||||
|
{
|
||||||
|
int x;
|
||||||
|
int y;
|
||||||
|
};
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
std::ifstream file("input.txt");
|
||||||
|
|
||||||
|
std::vector<std::string> 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;
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue
Block a user