|
|
@ -0,0 +1,137 @@
|
|
|
|
|
|
|
|
using AdventOfCode._2022.Models;
|
|
|
|
|
|
|
|
using AdventOfCode.Common;
|
|
|
|
|
|
|
|
using AdventOfCode.Models;
|
|
|
|
|
|
|
|
using System;
|
|
|
|
|
|
|
|
using System.Linq;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
namespace AdventOfCode._2022.Day8
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
[AOC(year: 2022, day: 8)]
|
|
|
|
|
|
|
|
public class Day8 : AOCDay
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
protected override AOCResponse ExecutePartA()
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
var board = CreateBoard();
|
|
|
|
|
|
|
|
var maxIndex = board.GetLength(0) - 1;
|
|
|
|
|
|
|
|
int visibleTrees = 0;
|
|
|
|
|
|
|
|
for (int x = 0; x < board.GetLength(0); x++)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
for (int y = 0; y < board.GetLength(1); y++)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
var tree = board[x, y];
|
|
|
|
|
|
|
|
int left = Math.Abs(0 - y), right = maxIndex - y, up = Math.Abs(0 - x), down = maxIndex - x;
|
|
|
|
|
|
|
|
if (left == 0 || right == 0 || up == 0 || down == 0)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
visibleTrees++;
|
|
|
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bool isHiddenLeft = false, isHiddenRight = false, isHiddenUp = false, isHiddenDown = false;
|
|
|
|
|
|
|
|
//left
|
|
|
|
|
|
|
|
for (int i = 0; i < left; i++)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
if (board[x, y - (i + 1)] >= tree) isHiddenLeft = true;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//right
|
|
|
|
|
|
|
|
for (int i = 0; i < right; i++)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
if (board[x, y + (i + 1)] >= tree) isHiddenRight = true;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//up
|
|
|
|
|
|
|
|
for (int i = 0; i < up; i++)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
if (board[x - (i + 1), y] >= tree) isHiddenUp = true;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//down
|
|
|
|
|
|
|
|
for (int i = 0; i < down; i++)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
if (board[x + (i + 1), y] >= tree) isHiddenDown = true;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (!isHiddenLeft || !isHiddenRight || !isHiddenUp || !isHiddenDown)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
visibleTrees++;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
_response.Answer = visibleTrees;
|
|
|
|
|
|
|
|
return _response;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
protected override AOCResponse ExecutePartB()
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
var board = CreateBoard();
|
|
|
|
|
|
|
|
var maxIndex = board.GetLength(0) - 1;
|
|
|
|
|
|
|
|
_response.Answer = 0;
|
|
|
|
|
|
|
|
for (int x = 0; x < board.GetLength(0); x++)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
for (int y = 0; y < board.GetLength(1); y++)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
var tree = board[x, y];
|
|
|
|
|
|
|
|
int left = Math.Abs(0 - y), right = maxIndex - y, up = Math.Abs(0 - x), down = maxIndex - x;
|
|
|
|
|
|
|
|
if (left == 0 || right == 0 || up == 0 || down == 0)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
continue; //The tree is on the edge so viewing is 0 on one side
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int leftTree = 0;
|
|
|
|
|
|
|
|
//left
|
|
|
|
|
|
|
|
for (int i = 0; i < left; i++)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
leftTree++;
|
|
|
|
|
|
|
|
if (board[x, y - (i + 1)] >= tree) break;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int rightTree = 0;
|
|
|
|
|
|
|
|
//right
|
|
|
|
|
|
|
|
for (int i = 0; i < right; i++)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
rightTree++;
|
|
|
|
|
|
|
|
if (board[x, y + (i + 1)] >= tree) break;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int upTree = 0;
|
|
|
|
|
|
|
|
//up
|
|
|
|
|
|
|
|
for (int i = 0; i < up; i++)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
upTree++;
|
|
|
|
|
|
|
|
if (board[x - (i + 1), y] >= tree) break;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int downTree = 0;
|
|
|
|
|
|
|
|
//right
|
|
|
|
|
|
|
|
for (int i = 0; i < down; i++)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
downTree++;
|
|
|
|
|
|
|
|
if (board[x + (i + 1), y] >= tree) break;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var viewingScore = leftTree * rightTree * upTree * downTree;
|
|
|
|
|
|
|
|
Log($"({x},{y}) - {tree} - ({leftTree},{rightTree},{upTree},{downTree}) - ({viewingScore})");
|
|
|
|
|
|
|
|
_response.Answer = Math.Max(viewingScore, (int) _response.Answer);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return _response;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private int[,] CreateBoard()
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
var splitInput = GetSplitInput();
|
|
|
|
|
|
|
|
var board = new int[splitInput.Length,splitInput.Length];
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
for (int x = 0; x < splitInput.Length; x++)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
for (int y = 0; y < splitInput[x].Length; y++)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
board[x,y] = int.Parse(splitInput[x][y].ToString());
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!_request.IgnoreLogMessages)
|
|
|
|
|
|
|
|
board.PrintSquareArray();
|
|
|
|
|
|
|
|
return board;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|