Completed day 8
This commit is contained in:
parent
974083b721
commit
691a3e351c
@ -3,18 +3,15 @@
|
|||||||
using AdventOfCodeLibrary._2022;
|
using AdventOfCodeLibrary._2022;
|
||||||
using AdventOfCodeLibrary.Shared;
|
using AdventOfCodeLibrary.Shared;
|
||||||
|
|
||||||
string _demoData = @"R 4
|
string _demoData = @"30373
|
||||||
U 4
|
25512
|
||||||
L 3
|
65332
|
||||||
D 1
|
33549
|
||||||
R 4
|
35390";
|
||||||
D 1
|
|
||||||
L 5
|
|
||||||
R 2";
|
|
||||||
|
|
||||||
Answerable answerable = new Day09Part1();
|
Answerable answerable = new Day08Part2();
|
||||||
byte[] dataArray = File.ReadAllBytes(answerable.DefaultInputFile);
|
byte[] dataArray = File.ReadAllBytes(answerable.DefaultInputFile);
|
||||||
dataArray = Encoding.UTF8.GetBytes(_demoData);
|
//dataArray = Encoding.UTF8.GetBytes(_demoData);
|
||||||
|
|
||||||
Console.WriteLine($"Max value: {answerable.GetAnswer(dataArray)}");
|
Console.WriteLine($"Answer: {answerable.GetAnswer(dataArray)}");
|
||||||
Console.ReadKey(true);
|
Console.ReadKey(true);
|
||||||
@ -1,6 +1,5 @@
|
|||||||
namespace AdventOfCodeLibrary._2022
|
namespace AdventOfCodeLibrary._2022
|
||||||
{
|
{
|
||||||
using AdventOfCodeLibrary._2022.Day_08;
|
|
||||||
using AdventOfCodeLibrary.Shared;
|
using AdventOfCodeLibrary.Shared;
|
||||||
|
|
||||||
public class Day08Part1 : Answerable
|
public class Day08Part1 : Answerable
|
||||||
@ -13,8 +12,22 @@
|
|||||||
{
|
{
|
||||||
string[] treeGrid = GetAsStringArray(data);
|
string[] treeGrid = GetAsStringArray(data);
|
||||||
|
|
||||||
TreeChecker checker = new TreeChecker();
|
int visibleTreeCount = 0;
|
||||||
return checker.VisibleTrees(treeGrid).ToString();
|
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();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -10,14 +10,47 @@
|
|||||||
|
|
||||||
public override string GetAnswer(byte[] data)
|
public override string GetAnswer(byte[] data)
|
||||||
{
|
{
|
||||||
int markerLength = 14;
|
string[] treeGrid = GetAsStringArray(data);
|
||||||
for (int skip = 0; skip < data.Length - 1 - markerLength; skip++)
|
|
||||||
|
int highestScenicScore = 0;
|
||||||
|
for (int row = 0; row < treeGrid.Length; row++)
|
||||||
{
|
{
|
||||||
if (data.Skip(skip).Take(markerLength).Distinct().Count() == markerLength)
|
for (int col = 0; col < treeGrid[row].Length; col++)
|
||||||
return (skip + markerLength).ToString();
|
{
|
||||||
|
if (row == 0 || col == 0 || row == treeGrid.Length - 1 || col == treeGrid[row].Length - 1)
|
||||||
|
{
|
||||||
|
// skip the edges
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
// get the height of the current tree
|
||||||
|
char treeHeight = treeGrid[row][col];
|
||||||
|
|
||||||
|
List<char> leftTrees = treeGrid[row].Take(col).ToList();
|
||||||
|
int firstLeftBlockingTreeIdx = leftTrees.FindLastIndex(height => height >= treeHeight);
|
||||||
|
int viewCountFromLeft = firstLeftBlockingTreeIdx == -1 ? leftTrees.Count : col - firstLeftBlockingTreeIdx;
|
||||||
|
|
||||||
|
List<char> rightTrees = treeGrid[row].Skip(col + 1).Take(treeGrid[row].Length - col - 1).Reverse().ToList();
|
||||||
|
int firstRightBlockingTreeIdx = rightTrees.FindLastIndex(height => height >= treeHeight);
|
||||||
|
int viewCountFromRight = firstRightBlockingTreeIdx == -1 ? rightTrees.Count : treeGrid[row].Length - col - firstRightBlockingTreeIdx - 1;
|
||||||
|
|
||||||
|
List<char> topTrees = treeGrid.Skip(0).Take(row).Select(ints => ints[col]).ToList();
|
||||||
|
int firstTopBlockingTreeIdx = topTrees.FindLastIndex(height => height >= treeHeight);
|
||||||
|
int viewCountFromTop = firstTopBlockingTreeIdx == -1 ? topTrees.Count : row - firstTopBlockingTreeIdx;
|
||||||
|
|
||||||
|
List<char> bottomTrees = treeGrid.Skip(row + 1).Take(treeGrid.Length - row - 1).Select(ints => ints[col]).Reverse().ToList();
|
||||||
|
int firstBottomBlockingTreeIdx = bottomTrees.FindLastIndex(height => height >= treeHeight);
|
||||||
|
int viewCountFromBottom = firstBottomBlockingTreeIdx == -1 ? bottomTrees.Count : treeGrid.Length - row - firstBottomBlockingTreeIdx - 1;
|
||||||
|
|
||||||
|
int scenicScore = viewCountFromLeft * viewCountFromRight * viewCountFromTop * viewCountFromBottom;
|
||||||
|
if (scenicScore > highestScenicScore)
|
||||||
|
{
|
||||||
|
highestScenicScore = scenicScore;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return "nothing found";
|
return highestScenicScore.ToString();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,118 +0,0 @@
|
|||||||
namespace AdventOfCodeLibrary._2022.Day_08
|
|
||||||
{
|
|
||||||
internal struct Tree
|
|
||||||
{
|
|
||||||
internal int X;
|
|
||||||
internal int Y;
|
|
||||||
internal char Height;
|
|
||||||
internal bool IsVisible;
|
|
||||||
|
|
||||||
internal Tree(int x, int y, char height)
|
|
||||||
{
|
|
||||||
X = x;
|
|
||||||
Y = y;
|
|
||||||
Height = height;
|
|
||||||
IsVisible = false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
internal class TreeChecker
|
|
||||||
{
|
|
||||||
private Tree[][] treeGrid;
|
|
||||||
|
|
||||||
private Tree[][] rotatedGrid;
|
|
||||||
|
|
||||||
internal int VisibleTrees(string[] treeArray)
|
|
||||||
{
|
|
||||||
treeGrid = new Tree[treeArray.Length][];
|
|
||||||
rotatedGrid = new Tree[treeArray[0].Length][];
|
|
||||||
|
|
||||||
for (int verticalIndex = 0; verticalIndex < treeArray.Length; verticalIndex++)
|
|
||||||
{
|
|
||||||
bool firstVertial = true;
|
|
||||||
for (int horizontalIndex = 0; horizontalIndex < treeArray[0].Length; horizontalIndex++)
|
|
||||||
{
|
|
||||||
if (firstVertial)
|
|
||||||
{
|
|
||||||
treeGrid[verticalIndex] = new Tree[treeArray[0].Length];
|
|
||||||
|
|
||||||
firstVertial = false;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (verticalIndex == 0)
|
|
||||||
{
|
|
||||||
rotatedGrid[horizontalIndex] = new Tree[treeArray.Length];
|
|
||||||
}
|
|
||||||
|
|
||||||
Tree tree = new Tree(verticalIndex, horizontalIndex, treeArray[verticalIndex][horizontalIndex]);
|
|
||||||
|
|
||||||
//Console.WriteLine($"Adding Tree to [{verticalIndex}][{horizontalIndex}] and [{horizontalIndex}][{verticalIndex}]");
|
|
||||||
|
|
||||||
treeGrid[verticalIndex][horizontalIndex] = tree;
|
|
||||||
rotatedGrid[horizontalIndex][verticalIndex] = tree;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
int visible = 0;
|
|
||||||
|
|
||||||
// start from the top
|
|
||||||
Tree[] currentHighest = treeGrid[0];
|
|
||||||
|
|
||||||
// from top to bottom
|
|
||||||
for (int treeIndex = 1; treeIndex < treeArray.Length - 2; treeIndex++)
|
|
||||||
{
|
|
||||||
int tmp = CheckVisibleTrees(ref currentHighest, treeGrid[treeIndex]);
|
|
||||||
visible += tmp;
|
|
||||||
}
|
|
||||||
|
|
||||||
currentHighest = treeGrid[^1];
|
|
||||||
// from bottom to top
|
|
||||||
for (int treeIndex = treeArray.Length - 1; treeIndex > 1 ; treeIndex--)
|
|
||||||
{
|
|
||||||
int tmp = CheckVisibleTrees(ref currentHighest, treeGrid[treeIndex]);
|
|
||||||
visible += tmp;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
currentHighest = rotatedGrid[0];
|
|
||||||
// from top to bottom with the rotated array
|
|
||||||
for (int treeIndex = rotatedGrid.Length - 1; treeIndex > 1; treeIndex--)
|
|
||||||
{
|
|
||||||
int tmp = CheckVisibleTrees(ref currentHighest, rotatedGrid[treeIndex]);
|
|
||||||
visible += tmp;
|
|
||||||
}
|
|
||||||
|
|
||||||
currentHighest = rotatedGrid[^1];
|
|
||||||
// from bottom to top with the rotated array
|
|
||||||
for (int treeIndex = rotatedGrid.Length - 1; treeIndex > 1; treeIndex--)
|
|
||||||
{
|
|
||||||
int tmp = CheckVisibleTrees(ref currentHighest, rotatedGrid[treeIndex]);
|
|
||||||
visible += tmp;
|
|
||||||
}
|
|
||||||
|
|
||||||
// add the outside trees
|
|
||||||
int edgeTrees = (2 * treeGrid.Length) + (2 * (treeGrid[0].Length - 2));
|
|
||||||
|
|
||||||
// return the result
|
|
||||||
return visible + edgeTrees;
|
|
||||||
}
|
|
||||||
|
|
||||||
private int CheckVisibleTrees(ref Tree[] currentHighest, Tree[] treeLineToCheck)
|
|
||||||
{
|
|
||||||
int visible = 0;
|
|
||||||
for (int treeIndex = 1; treeIndex < treeLineToCheck.Length - 1; treeIndex++)
|
|
||||||
{
|
|
||||||
Tree toCheck = treeLineToCheck[treeIndex];
|
|
||||||
int diff = toCheck.Height - currentHighest[treeIndex].Height;
|
|
||||||
if (diff > 0 && !treeGrid[toCheck.X][toCheck.Y].IsVisible)
|
|
||||||
{
|
|
||||||
visible++;
|
|
||||||
treeGrid[toCheck.X][toCheck.Y].IsVisible = true;
|
|
||||||
currentHighest[treeIndex] = treeLineToCheck[treeIndex];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return visible;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Loading…
Reference in New Issue
Block a user