AdventOfCode/AdventOfCode.Solutions/2022/Day 08/Day08Part1.cs
Rob 3846b42b7e Massive code base change
partial completion of day 3
2023-12-03 19:09:26 +01:00

32 lines
1.4 KiB
C#

namespace AdventOfCode.Solutions._2022
{
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();
}
}
}