|
1 | 1 | using AdventOfCode.Common; |
| 2 | +using Coor = AdventOfCode.Common.Coor<int>; |
2 | 3 |
|
3 | | -var lines = Resources.GetInputFileLines(); |
| 4 | +var map = Resources.GetInputFileLines() |
| 5 | + .ParseAsArray(); |
4 | 6 |
|
5 | | -Console.WriteLine($"Part 1: {""}"); |
| 7 | +var start = map.AllCoordinates().First(c => map.Get(c) == 'S'); |
| 8 | +var corner = new[] |
| 9 | +{ |
| 10 | + (Coor.Up, Coor.Left), |
| 11 | + (Coor.Up, Coor.Right), |
| 12 | + (Coor.Down, Coor.Left), |
| 13 | + (Coor.Down, Coor.Right), |
| 14 | +}; |
| 15 | +var end = map.AllCoordinates().First(c => map.Get(c) == 'E'); |
| 16 | +var direction = Coor.Right; |
| 17 | + |
| 18 | +// Places where we can change direction |
| 19 | +var crossroads = map.AllCoordinates() |
| 20 | + .Where(c => map.Get(c) == '.' && corner.Any(d => map.Get(c + d.Item1) == '.' && map.Get(c + d.Item2) == '.')) |
| 21 | + .Append(start) |
| 22 | + .Append(end) |
| 23 | + .ToHashSet(); |
| 24 | + |
| 25 | +var neighbours = new Dictionary<Coor, List<(Coor Neighbour, int Distance)>>(); |
| 26 | + |
| 27 | +foreach (var c in crossroads) |
| 28 | +{ |
| 29 | + foreach (var dir in Coor.FourWayNeighbours) |
| 30 | + { |
| 31 | + var current = c; |
| 32 | + var dist = 0; |
| 33 | + do |
| 34 | + { |
| 35 | + current += dir; |
| 36 | + dist++; |
| 37 | + |
| 38 | + if (crossroads.Contains(current)) |
| 39 | + { |
| 40 | + if (!neighbours.TryGetValue(c, out var list)) |
| 41 | + { |
| 42 | + list = []; |
| 43 | + neighbours[c] = list; |
| 44 | + } |
| 45 | + list.Add((current, dist)); |
| 46 | + break; |
| 47 | + } |
| 48 | + } while (map.Get(current) == '.'); |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +var scores = Resources.GetInputFileLines() |
| 53 | + .ParseAsArray(c => c switch |
| 54 | + { |
| 55 | + 'S' => 0, |
| 56 | + '#' => int.MinValue, |
| 57 | + _ => int.MaxValue, |
| 58 | + }); |
| 59 | + |
| 60 | +var shortestPaths = new Dictionary<Coor, List<Coor>>(); |
| 61 | +var queue = new PriorityQueue<State, int>(); |
| 62 | +queue.Enqueue(new(start, Coor.Right, []), 0); |
| 63 | + |
| 64 | +while (queue.TryDequeue(out var current, out var score)) |
| 65 | +{ |
| 66 | + if (scores.Get(current.Position) < score) continue; |
| 67 | + |
| 68 | + scores.Set(current.Position, score); |
| 69 | + shortestPaths[current.Position] = current.Path; |
| 70 | + |
| 71 | + if (!neighbours.TryGetValue(current.Position, out var currentNeighbours)) |
| 72 | + { |
| 73 | + continue; |
| 74 | + } |
| 75 | + |
| 76 | + foreach (var nc in currentNeighbours.Where(n => n.Neighbour != current.Position)) |
| 77 | + { |
| 78 | + var newDirection = (current.Position - nc.Neighbour).Normalize(); |
| 79 | + var newScore = score + nc.Distance + (newDirection == current.Direction ? 0 : 1000); |
| 80 | + if (scores.Get(nc.Neighbour) > newScore) |
| 81 | + { |
| 82 | + queue.Enqueue(new(nc.Neighbour, newDirection, [..current.Path, nc.Neighbour]), newScore); |
| 83 | + } |
| 84 | + } |
| 85 | +} |
| 86 | + |
| 87 | +var shortestPath = shortestPaths[end]; |
| 88 | +map.Print(c => shortestPath.Contains(c) ? ('*', ConsoleColor.Cyan) : (map.Get(c), ConsoleColor.White)); |
| 89 | + |
| 90 | +Console.WriteLine($"Part 1: {scores.Get(end)}"); |
6 | 91 | Console.WriteLine($"Part 2: {""}"); |
| 92 | + |
| 93 | +file record struct State(Coor Position, Coor Direction, List<Coor> Path); |
0 commit comments