-
Notifications
You must be signed in to change notification settings - Fork 9
/
day_04a.cpp
59 lines (53 loc) · 1.42 KB
/
day_04a.cpp
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
52
53
54
55
56
57
58
59
#include <fstream>
#include <iostream>
#include <string>
#include <utility>
#include <vector>
std::vector<std::pair<int, int>> moves = {
{1,0},
{0,1},
{-1,0},
{0,-1},
{1,1},
{1,-1},
{-1,1},
{-1,-1},
};
int check(const std::vector<std::string>& map, const int row, const int col) {
int total = 0;
for (const auto& move : moves) {
if (col + move.second * 3 >= map[0].size()) continue;
if (row + move.first * 3 >= map.size()) continue;
if (col + move.second * 3 < 0) continue;
if (row + move.first * 3 < 0) continue;
if (map[row][col] == 'X' &&
map[row + move.first][col + move.second] == 'M' &&
map[row + 2*move.first][col + 2*move.second] == 'A' &&
map[row + 3*move.first][col + 3*move.second] == 'S') {
total += 1;
}
}
return total;
}
int main(int argc, char* argv[]) {
std::string input = "../input/day_04_input";
if (argc > 1) {
input = argv[1];
}
std::ifstream file(input);
std::string line;
std::vector<std::string> map;
while(std::getline(file, line)) {
map.push_back(line);
}
int total = 0;
for (int i = 0; i < map.size(); i++) {
for (int j = 0; j < map[0].size(); j++) {
if (map[i][j] == 'X') {
total += check(map, i, j);
}
}
}
std::cout << total << '\n';
return 0;
}