-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday04.cpp
89 lines (78 loc) · 2.44 KB
/
day04.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
#include "../utils/containers.cpp"
#include "../utils/files.cpp"
#include <format>
#include <ranges>
#include <string>
#include <string_view>
#include <vector>
inline bool isXmas(char s1, char s2, char s3, char s4) {
return (s1 == 'X' && s2 == 'M' && s3 == 'A' && s4 == 'S') || (s1 == 'S' && s2 == 'A' && s3 == 'M' && s4 == 'X');
}
inline bool isSam(char s1, char s2, char s3) {
return (s1 == 'S' && s2 == 'A' && s3 == 'M') || (s1 == 'M' && s2 == 'A' && s3 == 'S');
}
int part01(std::vector<std::string> lines) {
auto const rows = std::ssize(lines);
auto const cols = std::ssize(lines.front());
auto xmasCount = 0L;
// check inside rows
for (auto y = 0; y < rows; y++) {
for (auto x = 0; x <= cols - 4; x++) {
if (isXmas(lines[y].at(x), lines[y].at(x + 1), lines[y].at(x + 2), lines[y].at(x + 3))) {
xmasCount++;
}
}
}
// check cols
for (auto x = 0; x < cols; x++) {
for (auto y = 0; y <= rows - 4; y++) {
if (isXmas(lines[y].at(x), lines[y + 1].at(x), lines[y + 2].at(x), lines[y + 3].at(x))) {
xmasCount++;
}
}
}
// diagonals
for (auto y = 0; y <= rows - 4; y++) {
for (auto x = 0; x < cols; x++) {
if (x <= cols - 4) {
if (isXmas(lines[y].at(x), lines[y + 1].at(x + 1), lines[y + 2].at(x + 2),
lines[y + 3].at(x + 3))) {
xmasCount++;
}
}
if (x >= 3) {
if (isXmas(lines[y].at(x), lines[y + 1].at(x - 1), lines[y + 2].at(x - 2),
lines[y + 3].at(x - 3))) {
xmasCount++;
}
}
}
}
return xmasCount;
}
int part02(std::vector<std::string> lines) {
auto const rows = std::ssize(lines);
auto const cols = std::ssize(lines.front());
auto samStarCount = 0L;
for (auto y = 1; y < rows - 1; y++) {
for (auto x = 1; x < cols - 1; x++) {
if (isSam(lines[y - 1].at(x - 1), lines[y].at(x), lines[y + 1].at(x + 1)) &&
isSam(lines[y - 1].at(x + 1), lines[y].at(x), lines[y + 1].at(x - 1))) {
samStarCount++;
}
}
}
return samStarCount;
}
int main() {
auto start = std::chrono::high_resolution_clock::now();
auto file = readFile("../generated/aoc/inputs/2024/day04.txt");
auto lines = file.getLines();
auto part1 = part01(lines);
auto part2 = part02(lines);
auto end = std::chrono::high_resolution_clock::now();
std::chrono::duration<double, std::milli> duration_ms = end - start;
std::cout << "Execution time: " << duration_ms.count() << " ms" << std::endl;
std::cout << "Part 01: " << (part1) << std::endl;
std::cout << "Part 02: " << (part2) << std::endl;
};