93 lines
2.6 KiB
C#
93 lines
2.6 KiB
C#
using AdventOfCode.Core.Shared.IO;
|
|
|
|
namespace AdventOfCode.Solutions._2024
|
|
{
|
|
public class Day06 : IChallange
|
|
{
|
|
public int Year => 2024;
|
|
|
|
public int Day => 6;
|
|
|
|
private readonly IInputReader _inputReader;
|
|
|
|
public Day06(IInputReader inputReader)
|
|
{
|
|
_inputReader = inputReader;
|
|
_inputReader.SetInput(this);
|
|
//_inputReader.SetSampleInput(true);
|
|
}
|
|
|
|
// 41
|
|
// 4696
|
|
public async Task<string> GetSolutionPart1()
|
|
{
|
|
Grid<Point> map = await _inputReader.ReadToGrid<Point>();
|
|
Point current = map.DataGrid.Single(p => p.Value == '^');
|
|
HashSet<Point> points = [current];
|
|
Point move = new Point(0, -1);
|
|
|
|
do
|
|
{
|
|
// check if we are done
|
|
long nextX = current.X + move.X,
|
|
nextY = current.Y + move.Y;
|
|
if (nextX == -1 || nextX == map.Columns
|
|
|| nextY == -1 || nextY == map.Rows)
|
|
break;
|
|
|
|
// get next
|
|
Point next = map.GetNode(nextX, nextY);
|
|
if (next.Value == '#')
|
|
{
|
|
move = Turn(move);
|
|
//Console.WriteLine("turned " + move.ToString());
|
|
continue;
|
|
}
|
|
|
|
points.Add(next);
|
|
current = next;
|
|
//Console.WriteLine("moved " + current.ToString());
|
|
}
|
|
while (true);
|
|
|
|
return points.Count.ToString();
|
|
}
|
|
|
|
// 6
|
|
//
|
|
public async Task<string> GetSolutionPart2()
|
|
{
|
|
return string.Empty;
|
|
}
|
|
|
|
private Point Turn(Point currentDir)
|
|
{
|
|
if (currentDir.Y == -1 && currentDir.X == 0)
|
|
{
|
|
currentDir.Y = 0;
|
|
currentDir.X = 1;
|
|
return currentDir;
|
|
}
|
|
else if (currentDir.Y == 0 && currentDir.X == 1)
|
|
{
|
|
currentDir.Y = 1;
|
|
currentDir.X = 0;
|
|
return currentDir;
|
|
}
|
|
else if (currentDir.Y == 1 && currentDir.X == 0)
|
|
{
|
|
currentDir.Y = 0;
|
|
currentDir.X = -1;
|
|
return currentDir;
|
|
}
|
|
else if(currentDir.Y == 0 && currentDir.X == -1)
|
|
{
|
|
currentDir.Y = -1;
|
|
currentDir.X = 0;
|
|
return currentDir;
|
|
}
|
|
|
|
throw new InvalidDataException();
|
|
}
|
|
}
|
|
} |