Skip to content

Commit

Permalink
day 6 part 1
Browse files Browse the repository at this point in the history
  • Loading branch information
KaterynaKateryna committed Dec 6, 2024
1 parent e4ad71f commit dec16c1
Show file tree
Hide file tree
Showing 2 changed files with 111 additions and 0 deletions.
1 change: 1 addition & 0 deletions AdventOfCode/AdventOfCode.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
<TargetFramework>net9.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<OutputType>Exe</OutputType>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
Expand Down
110 changes: 110 additions & 0 deletions AdventOfCode/Day06.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
namespace AdventOfCode;

internal class Day06 : BaseDay
{
private char[][] _map;

public Day06()
{
_map = File.ReadAllLines(InputFilePath).Select(x => x.ToCharArray()).ToArray();
}

public override ValueTask<string> Solve_1()
{
HashSet<Point> visitedLocations = new HashSet<Point>();
Point currentGuardPoint = GetGuard();
Direction currentGuardDirection = Direction.Up;
visitedLocations.Add(currentGuardPoint);

while (true)
{
try
{
(currentGuardPoint, currentGuardDirection) = GetNextMove(currentGuardPoint, currentGuardDirection);
visitedLocations.Add(currentGuardPoint);
}
catch (IndexOutOfRangeException)
{
break;
}
}


return new(visitedLocations.Count.ToString());
}

public override ValueTask<string> Solve_2()
{
return new("");
}

private Point GetGuard()
{
for (int i = 0; i < _map.Length; ++i)
{
for (int j = 0; j < _map[0].Length; ++j)
{
if (_map[i][j] == '^')
{
return new Point(i, j);
}
}
}
throw new Exception("Guard not found.");
}

private (Point point, Direction direction) GetNextMove(Point point, Direction direction)
{
switch (direction)
{
case Direction.Up:
if (_map[point.I - 1][point.J] == '#')
{
return (new Point(point.I, point.J + 1), Direction.Right);
}
else
{
return (new Point(point.I - 1, point.J), Direction.Up);
}
case Direction.Right:
if (_map[point.I][point.J + 1] == '#')
{
return (new Point(point.I + 1, point.J), Direction.Down);
}
else
{
return (new Point(point.I, point.J + 1), Direction.Right);
}
case Direction.Down:
if (_map[point.I + 1][point.J] == '#')
{
return (new Point(point.I, point.J - 1), Direction.Left);
}
else
{
return (new Point(point.I + 1, point.J), Direction.Down);
}
case Direction.Left:
if (_map[point.I][point.J - 1] == '#')
{
return (new Point(point.I - 1, point.J), Direction.Up);
}
else
{
return (new Point(point.I, point.J - 1), Direction.Left);
}
}

throw new NotImplementedException();
}

private record Point(int I, int J);

private enum Direction
{
Up,
Right,
Down,
Left
}
}

0 comments on commit dec16c1

Please sign in to comment.