-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday4.cpp
More file actions
51 lines (47 loc) · 1010 Bytes
/
day4.cpp
File metadata and controls
51 lines (47 loc) · 1010 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
using namespace std;
int r, c;
const int dx[8] = {1, 1, 0, -1, -1, -1, 0, 1};
const int dy[8] = {0, 1, 1, 1, 0, -1, -1, -1};
const string order = "XMAS";
int check(vector<string>& puzzle, int x, int y)
{
int ret = 0;
if (puzzle[x][y] != 'X') return 0;
for (int i = 0; i < 8; i++)
{
bool good = true;
for (int j = 1; j < 4; j++)
{
int nx = x+j*dx[i];
int ny = y+j*dy[i];
if (nx < 0 || nx >= r || ny < 0 || ny >= c || puzzle[nx][ny] != order[j])
good = false;
if (!good) break;
}
if (good) ret++;
}
return ret;
}
int main()
{
vector<string> puzzle;
ifstream input_file("input.txt");
string line;
while (getline(input_file, line))
puzzle.push_back(line);
r = puzzle.size();
c = puzzle[0].size();
int ans = 0;
for (int i = 0; i < r; i++)
{
for (int j = 0; j < c; j++)
{
ans += check(puzzle, i, j);
}
}
cout << ans << endl;
}