Skip to content

Commit

Permalink
Merge pull request #19 from MartinZikmund/feature/aoc2024-day18
Browse files Browse the repository at this point in the history
AoC 2024 Day 18 Solutions
  • Loading branch information
MartinZikmund authored Dec 20, 2024
2 parents b941e03 + 4c8f08a commit ef6ce44
Show file tree
Hide file tree
Showing 3 changed files with 161 additions and 0 deletions.
60 changes: 60 additions & 0 deletions src/AdventOfCode.Puzzles/2024/18/Part1/Part1.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
namespace AdventOfCode.Puzzles._2024._18.Part1;

public partial class Part1 : IPuzzleSolution
{
private const int Width = 71;
private const int Height = 71;
private const int ByteCount = 1024;

private char[,] _map = new char[Width, Height];

public async Task<string> SolveAsync(StreamReader inputReader)
{
_map = new char[Width, Height];
for (int i = 0; i < ByteCount; i++)
{
var line = await inputReader.ReadLineAsync();
var parts = line.Split(",");
var x = int.Parse(parts[0]);
var y = int.Parse(parts[1]);

_map[x, y] = '#';
}

var length = FindPath((0,0), (Width - 1, Height - 1));

return length.ToString();
}

private int FindPath(Point start, Point end)
{
var queue = new Queue<(Point position, int steps)>();
queue.Enqueue((start, 0));
var visited = new HashSet<Point>();
visited.Add(start);
while (queue.Count > 0)
{
var (position, steps) = queue.Dequeue();
if (position == end)
{
return steps;
}

foreach (var direction in Directions.WithoutDiagonals)
{
var next = position + direction;
if ((next.X < 0 || next.X >= Width || next.Y < 0 || next.Y >= Height) ||
(_map[next.X, next.Y] == '#') ||
visited.Contains(next))
{
continue;
}

visited.Add(next);
queue.Enqueue((next, steps + 1));
}
}

return -1;
}
}
76 changes: 76 additions & 0 deletions src/AdventOfCode.Puzzles/2024/18/Part2/Part2.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
namespace AdventOfCode.Puzzles._2024._18.Part2;

public partial class Part2 : IPuzzleSolution
{
private const int Width = 71;
private const int Height = 71;
private const int ByteCount = 1024;

private char[,] _map = new char[Width, Height];

public async Task<string> SolveAsync(StreamReader inputReader)
{
_map = new char[Width, Height];
HashSet<Point> lastPath = new();
for (int i = 0; ; i++)
{
var line = await inputReader.ReadLineAsync();
var parts = line.Split(",");
var x = int.Parse(parts[0]);
var y = int.Parse(parts[1]);

_map[x, y] = '#';
if (lastPath.Count == 0 || lastPath.Contains((x, y)))
{
(var length, lastPath) = FindPath((0, 0), (Width - 1, Height - 1));

if (length == -1)
{
return x + "," + y;
}
}
}
}

private (int length, HashSet<Point> points) FindPath(Point start, Point end)
{
var queue = new Queue<(Point position, int steps)>();
queue.Enqueue((start, 0));
var visited = new HashSet<Point>();
var previous = new Dictionary<Point, Point>();
visited.Add(start);
while (queue.Count > 0)
{
var (position, steps) = queue.Dequeue();
if (position == end)
{
var path = new HashSet<Point>();
var current = position;
while (current != start)
{
path.Add(current);
current = previous[current];
}

return (steps, path);
}

foreach (var direction in Directions.WithoutDiagonals)
{
var next = position + direction;
if ((next.X < 0 || next.X >= Width || next.Y < 0 || next.Y >= Height) ||
(_map[next.X, next.Y] == '#') ||
visited.Contains(next))
{
continue;
}

visited.Add(next);
previous[next] = position;
queue.Enqueue((next, steps + 1));
}
}

return (-1, null);
}
}
25 changes: 25 additions & 0 deletions src/AdventOfCode.Puzzles/2024/18/TestData.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
5,4
4,2
4,5
3,0
2,1
6,3
2,4
1,5
0,6
3,3
2,6
5,1
1,2
5,5
2,5
6,5
1,4
0,4
6,4
1,1
6,1
1,0
0,5
1,6
2,0

0 comments on commit ef6ce44

Please sign in to comment.