-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path04.cpp
121 lines (106 loc) · 2.91 KB
/
04.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#include <iostream>
#include <vector>
#include "FileReader.h"
class Day04
{
public:
Day04() {}
~Day04() {}
void readInput(std::string filename)
{
FileReader file(filename);
m_lines = file.getLines();
}
bool findWord(int y, int x, int offset_y, int offset_x)
{
if (offset_y == -1)
if (y < m_target.size()-1)
return false;
if (offset_y == 1)
if (y > m_lines.size()-m_target.size())
return false;
if (offset_x == -1)
if (x < m_target.size()-1)
return false;
if (offset_x == 1)
if (x > m_lines[y].size()-m_target.size())
return false;
for (int i = 0; i < m_target.size(); i++)
if (m_lines[y + offset_y*i][x + offset_x*i] != m_target[i])
return false;
return true;
}
int checkCoordinate(int y, int x)
{
int xmas = 0;
xmas += findWord(y, x, -1, -1) ? 1 : 0;
xmas += findWord(y, x, -1, 0) ? 1 : 0;
xmas += findWord(y, x, -1, 1) ? 1 : 0;
xmas += findWord(y, x, 0, -1) ? 1 : 0;
xmas += findWord(y, x, 0, 1) ? 1 : 0;
xmas += findWord(y, x, 1, -1) ? 1 : 0;
xmas += findWord(y, x, 1, 0) ? 1 : 0;
xmas += findWord(y, x, 1, 1) ? 1 : 0;
return xmas;
}
bool checkXMas(int y, int x)
{
if (y <= 0 || y >= m_lines.size()-1)
{
return false;
}
if (x <= 0 || x >= m_lines[y].size())
{
return false;
}
if (!(m_lines[y-1][x-1] == 'M' && m_lines[y+1][x+1] == 'S'))
{
if (!(m_lines[y-1][x-1] == 'S' && m_lines[y+1][x+1] == 'M'))
{
return false;
}
}
if (!(m_lines[y+1][x-1] == 'M' && m_lines[y-1][x+1] == 'S'))
{
if (!(m_lines[y+1][x-1] == 'S' && m_lines[y-1][x+1] == 'M'))
{
return false;
}
}
return true;
}
void countXmas()
{
int total_xmas = 0;
int total_x_mas = 0;
for (int y = 0; y < m_lines.size(); y++)
{
for (int x = 0; x < m_lines[y].size(); x++)
{
if (m_lines[y][x] == 'X')
{
total_xmas += checkCoordinate(y, x);
}
if (m_lines[y][x] == 'A')
{
if (checkXMas(y, x))
{
total_x_mas++;
}
}
}
}
std::cout << "XMAS: " << total_xmas << std::endl;
std::cout << "X-MAS: " << total_x_mas << std::endl;
}
private:
std::vector<std::string> m_lines;
std::string m_target = "XMAS";
};
int main()
{
Day04 day04;
day04.readInput("04_input.txt");
day04.countXmas();
return 0;
}