advent-of-code/2024/day04/part1.cc

47 lines
1.4 KiB
C++
Raw Normal View History

2024-12-14 07:11:44 +00:00
#include <cstdint>
#include <iostream>
#include <vector>
int main() {
const std::uint32_t forward = 'X' << 24 | 'M' << 16 | 'A' << 8 | 'S';
const std::uint32_t reverse = 'S' << 24 | 'A' << 16 | 'M' << 8 | 'X';
std::uint32_t horizontal = 0;
std::vector<std::uint32_t> vertical, pos_diag, neg_diag;
int count = 0;
int line = 0;
int pos = 0;
int width = 0;
char c;
std::cin >> std::noskipws;
while (std::cin.get(c)) {
horizontal = horizontal << 8 | c;
count += horizontal == forward || horizontal == reverse;
if (c == '\n') {
width = pos;
pos = 0;
horizontal = 0;
pos_diag[line] = 0;
neg_diag[width - 1 - line] = 0;
line++;
continue;
} else if (line == 0) {
vertical.emplace_back(c);
pos_diag.emplace_back(c);
neg_diag.emplace_back(c);
} else {
vertical[pos] = vertical[pos] << 8 | c;
count += vertical[pos] == forward || vertical[pos] == reverse;
auto &pos_diag_elem = pos_diag[(pos + line) % width];
pos_diag_elem = pos_diag_elem << 8 | c;
count += pos_diag_elem == forward || pos_diag_elem == reverse;
auto &neg_diag_elem = neg_diag[((pos - line) % width + width) % width];
neg_diag_elem = neg_diag_elem << 8 | c;
count += neg_diag_elem == forward || neg_diag_elem == reverse;
}
pos++;
}
std::cout << count << std::endl;
return EXIT_SUCCESS;
}