mirror of
https://github.com/SebastianStork/advent-of-code.git
synced 2026-01-21 19:11:34 +01:00
97 lines
2.7 KiB
C++
97 lines
2.7 KiB
C++
#include <iostream>
|
|
#include <vector>
|
|
|
|
using namespace std;
|
|
|
|
vector<string> readInput()
|
|
{
|
|
vector<string> lines;
|
|
string line;
|
|
while (getline(cin, line)) {
|
|
lines.push_back(line);
|
|
}
|
|
return lines;
|
|
}
|
|
|
|
bool wordIsXmas(const string &word)
|
|
{
|
|
return word == "XMAS" || word == "SAMX";
|
|
}
|
|
|
|
bool wordIsMas(const string &word)
|
|
{
|
|
return word == "MAS" || word == "SAM";
|
|
}
|
|
|
|
int xmasCountAt(const vector<string> &matrix, const size_t rowIndex, const size_t colIndex)
|
|
{
|
|
const size_t numberOfRows = matrix.size();
|
|
const size_t numberOfCols = matrix[0].size();
|
|
|
|
int xmasCount = 0;
|
|
|
|
if (colIndex + 3 < numberOfCols) {
|
|
string horizontalWord = matrix[rowIndex].substr(colIndex, 4);
|
|
xmasCount += wordIsXmas(horizontalWord);
|
|
}
|
|
|
|
if (rowIndex + 3 < numberOfRows) {
|
|
string verticalWord;
|
|
for (size_t i = 0; i <= 3; i++) {
|
|
verticalWord += matrix[rowIndex + i][colIndex];
|
|
}
|
|
xmasCount += wordIsXmas(verticalWord);
|
|
}
|
|
|
|
if (rowIndex + 3 < numberOfRows && colIndex + 3 < numberOfCols) {
|
|
string downDiagonalWord, upDiagonalWord;
|
|
for (size_t i = 0; i <= 3; i++) {
|
|
downDiagonalWord += matrix[rowIndex + i][colIndex + i];
|
|
upDiagonalWord += matrix[rowIndex + (3 - i)][colIndex + i];
|
|
}
|
|
xmasCount += wordIsXmas(downDiagonalWord) + wordIsXmas(upDiagonalWord);
|
|
}
|
|
|
|
return xmasCount;
|
|
}
|
|
|
|
bool crossMasExistsAt(const vector<string> &matrix, const size_t rowIndex, const size_t colIndex)
|
|
{
|
|
const size_t numberOfRows = matrix.size();
|
|
const size_t numberOfCols = matrix[0].size();
|
|
|
|
if (rowIndex + 2 >= numberOfRows || colIndex + 2 >= numberOfCols) {
|
|
return false;
|
|
}
|
|
|
|
string downDiagonalWord, upDiagonalWord;
|
|
for (size_t i = 0; i <= 2; i++) {
|
|
downDiagonalWord += matrix[rowIndex + i][colIndex + i];
|
|
upDiagonalWord += matrix[rowIndex + (2 - i)][colIndex + i];
|
|
}
|
|
|
|
return wordIsMas(downDiagonalWord) && wordIsMas(upDiagonalWord);
|
|
}
|
|
|
|
int main()
|
|
{
|
|
const vector<string> matrix = readInput();
|
|
const size_t numberOfRows = matrix.size();
|
|
const size_t numberOfCols = matrix[0].size();
|
|
|
|
int numberOfXmasOccurrences = 0;
|
|
int numberOfCrossMasOccurrences = 0;
|
|
|
|
for (size_t rowIndex = 0; rowIndex < numberOfRows; rowIndex++) {
|
|
for (size_t colIndex = 0; colIndex < numberOfCols; colIndex++) {
|
|
numberOfXmasOccurrences += xmasCountAt(matrix, rowIndex, colIndex);
|
|
numberOfCrossMasOccurrences += crossMasExistsAt(matrix, rowIndex, colIndex);
|
|
}
|
|
}
|
|
|
|
// Part one
|
|
cout << "Number of XMAS occurrences: " << numberOfXmasOccurrences << endl;
|
|
|
|
// Part two
|
|
cout << "Number of X-MAS occurrences: " << numberOfCrossMasOccurrences << endl;
|
|
}
|