2024-12-04 13:30:30 +02:00
|
|
|
|
var lines = File.ReadAllLines("input.txt");
|
|
|
|
|
|
var charArray = new char[lines.Length, lines[0].Length];
|
|
|
|
|
|
|
|
|
|
|
|
for (var i = 0; i < charArray.GetLength(0); i++)
|
|
|
|
|
|
for (var j = 0; j < charArray.GetLength(1); j++)
|
|
|
|
|
|
{
|
|
|
|
|
|
charArray[i, j] = lines[i][j];
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Console.WriteLine($"Part1: {FindXmas(charArray)}\nPart2: {FindMas(charArray)}");
|
|
|
|
|
|
|
|
|
|
|
|
int FindMas(char[,] grid)
|
|
|
|
|
|
{
|
|
|
|
|
|
var rows = grid.GetLength(0);
|
|
|
|
|
|
var cols = grid.GetLength(1);
|
|
|
|
|
|
|
2024-12-04 13:35:57 +02:00
|
|
|
|
var searchDirections = new[,]
|
2024-12-04 13:30:30 +02:00
|
|
|
|
{
|
|
|
|
|
|
{ -1, 1 }, // diagonal up right
|
|
|
|
|
|
{ 1, 1 }, // diagonal down right
|
|
|
|
|
|
{ -1, -1 }, // diagonal up left
|
|
|
|
|
|
{ 1, -1 } // diagonal down left
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
List<string> masCenterCoordinates = new();
|
|
|
|
|
|
|
2024-12-04 13:35:57 +02:00
|
|
|
|
for (var row = 0; row < rows; row++)
|
2024-12-04 13:30:30 +02:00
|
|
|
|
{
|
2024-12-04 13:35:57 +02:00
|
|
|
|
for (var column = 0; column < cols; column++)
|
2024-12-04 13:30:30 +02:00
|
|
|
|
{
|
2024-12-04 13:35:57 +02:00
|
|
|
|
for (var direction = 0; direction < searchDirections.GetLength(0); direction++)
|
2024-12-04 13:30:30 +02:00
|
|
|
|
{
|
2024-12-04 13:35:57 +02:00
|
|
|
|
var masCoordinates = CheckWord(row, column, searchDirections[direction, 0], searchDirections[direction, 1], grid, "MAS");
|
2024-12-04 13:30:30 +02:00
|
|
|
|
if (masCoordinates != null)
|
|
|
|
|
|
{
|
|
|
|
|
|
masCenterCoordinates.Add($"{masCoordinates[1][0]},{masCoordinates[1][1]}");
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
var masCountDict = masCenterCoordinates.GroupBy(item => item).ToDictionary(group => group.Key, group => group.Count()).Where(x => x.Value > 1);
|
|
|
|
|
|
|
|
|
|
|
|
return masCountDict.Count();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int FindXmas(char[,] grid)
|
|
|
|
|
|
{
|
|
|
|
|
|
var rows = grid.GetLength(0);
|
|
|
|
|
|
var cols = grid.GetLength(1);
|
|
|
|
|
|
var xmasCount = 0;
|
|
|
|
|
|
|
2024-12-04 13:35:57 +02:00
|
|
|
|
var searchDirections = new[,]
|
2024-12-04 13:30:30 +02:00
|
|
|
|
{
|
|
|
|
|
|
{ -1, 0 }, // up
|
|
|
|
|
|
{ 0, 1 }, // right
|
|
|
|
|
|
{ 0, -1 }, // left
|
|
|
|
|
|
{ 1, 0 }, // down
|
|
|
|
|
|
{ -1, 1 }, // diagonal up right
|
|
|
|
|
|
{ 1, 1 }, // diagonal down right
|
|
|
|
|
|
{ -1, -1 }, // diagonal up left
|
|
|
|
|
|
{ 1, -1 } // diagonal down left
|
|
|
|
|
|
};
|
|
|
|
|
|
|
2024-12-04 13:35:57 +02:00
|
|
|
|
for (var row = 0; row < rows; row++)
|
2024-12-04 13:30:30 +02:00
|
|
|
|
{
|
2024-12-04 13:35:57 +02:00
|
|
|
|
for (var column = 0; column < cols; column++)
|
2024-12-04 13:30:30 +02:00
|
|
|
|
{
|
2024-12-04 13:35:57 +02:00
|
|
|
|
for (var direction = 0; direction < searchDirections.GetLength(0); direction++)
|
2024-12-04 13:30:30 +02:00
|
|
|
|
{
|
2024-12-04 13:35:57 +02:00
|
|
|
|
if (CheckWord(row, column, searchDirections[direction, 0], searchDirections[direction, 1], grid, "XMAS") != null)
|
2024-12-04 13:30:30 +02:00
|
|
|
|
{
|
|
|
|
|
|
xmasCount++;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return xmasCount;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-12-04 13:35:57 +02:00
|
|
|
|
List<int[]>? CheckWord(int row, int column, int directionRow, int directionColumn, char[,] grid, string word)
|
2024-12-04 13:30:30 +02:00
|
|
|
|
{
|
|
|
|
|
|
var characterCoordinates = new List<int[]>();
|
|
|
|
|
|
|
|
|
|
|
|
for (var i = 0; i < word.Length; i++)
|
|
|
|
|
|
{
|
2024-12-04 13:35:57 +02:00
|
|
|
|
var newRow = row + i * directionRow;
|
|
|
|
|
|
var newColumn = column + i * directionColumn;
|
|
|
|
|
|
if (!IsValid(newRow, newColumn, grid) || grid[newRow, newColumn] != word[i]) return null;
|
2024-12-04 13:30:30 +02:00
|
|
|
|
|
2024-12-04 13:35:57 +02:00
|
|
|
|
characterCoordinates.Add([newRow, newColumn]);
|
2024-12-04 13:30:30 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return characterCoordinates;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-12-04 13:35:57 +02:00
|
|
|
|
bool IsValid(int row, int column, char[,] grid) => row >= 0 && row < grid.GetLength(0) && column >= 0 && column < grid.GetLength(1);
|