34 lines
1.4 KiB
C#
34 lines
1.4 KiB
C#
namespace AdventOfCodeLibrary._2022
|
|
{
|
|
using AdventOfCodeLibrary.Shared;
|
|
|
|
public class Day08Part1 : Answerable
|
|
{
|
|
public override int Year { get; set; } = 2022;
|
|
public override int Day { get; set; } = 8;
|
|
public override int Part { get; set; } = 1;
|
|
|
|
public override string GetAnswer(byte[] data)
|
|
{
|
|
string[] treeGrid = GetAsStringArray(data);
|
|
|
|
int visibleTreeCount = 0;
|
|
for (int row = 0; row < treeGrid.Length; row++)
|
|
{
|
|
for (int col = 0; col < treeGrid[row].Length; col++)
|
|
{
|
|
char treeHeight = treeGrid[row][col];
|
|
bool visibleFromLeft = !Enumerable.Range(0, col).Any(leftIndex => treeGrid[row][leftIndex] >= treeHeight);
|
|
bool visibleFromRight = !Enumerable.Range(col + 1, treeGrid[row].Length - col - 1).Any(rightIndex => treeGrid[row][rightIndex] >= treeHeight);
|
|
bool visibleFromTop = !Enumerable.Range(0, row).Any(topIndex => treeGrid[topIndex][col] >= treeHeight);
|
|
bool visibleFromBottom = !Enumerable.Range(row + 1, treeGrid.Length - row - 1).Any(bottomIndex => treeGrid[bottomIndex][col] >= treeHeight);
|
|
|
|
if (visibleFromBottom || visibleFromLeft || visibleFromRight || visibleFromTop) visibleTreeCount++;
|
|
}
|
|
}
|
|
|
|
return visibleTreeCount.ToString();
|
|
}
|
|
}
|
|
}
|