generated from MartinZikmund/template
-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #19 from MartinZikmund/feature/aoc2024-day18
AoC 2024 Day 18 Solutions
- Loading branch information
Showing
3 changed files
with
161 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |