-
Notifications
You must be signed in to change notification settings - Fork 0
/
2021_15.cs
185 lines (158 loc) · 5.95 KB
/
2021_15.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
namespace AOC.CSharp;
public static class AOC2021_15
{
public static long Solve1(string[] lines)
{
// Build the map, which is just the array cells (x,y) with the cost of moving there
Cell[,] map = ParseMap(lines);
return FindShortestPath(map);
}
public static long Solve2(string[] lines)
{
// Build the map, which is just the array cells (x,y) with the cost of moving there
Cell[,] map = ParseMap2(lines);
return FindShortestPath(map);
}
private static long FindShortestPath(Cell[,] map)
{
Cell curr = map[0, 0];
Cell end = map[map.GetLength(0) - 1, map.GetLength(1) - 1];
// Everything starts off unvisited with an infinite cost (except the start cell, which is accounted
// for as a special case)
HashSet<Cell> unvisited = MapToHashSet(map);
Dictionary<Cell, int> costs = unvisited.ToDictionary(c => c, _ => int.MaxValue);
costs[curr] = 0;
// A set of candidates to visit next. Whenever we store a new cost for an unvisited cell we want to consider
// it as the potential next place to explore. Whenever we visit a node we can remove it from this set since
// we never need to visit it again. We intend to visit the unvisited cell with the lowest cost, so just
// having this set is not enough to tell us exactly where to go.
PriorityQueue<Cell, int> nextQ = new();
// We need to fully explore the graph. It is not enough to reach the end because there might still be a better
// solution to discover
while (unvisited.Any())
{
unvisited.Remove(curr);
// Calculate the distance from the current cell to each of its neighbors. If this cost is less than the
// cost we already know about, we have found a new shortest path to that cell
var neighbors = GetNeighbors(map, curr);
foreach (var n in neighbors.Where(n => unvisited.Contains(n)))
{
int newCost = costs[curr] + n.Weight;
costs[n] = Math.Min(costs[n], newCost);
nextQ.Enqueue(n, costs[n]);
}
// Pick the lowest cost unvisited node to visit next
if (nextQ.TryDequeue(out curr, out _))
{
while (curr != null && !unvisited.Contains(curr))
{
nextQ.TryDequeue(out curr, out _);
}
}
}
return costs[end];
}
private static HashSet<Cell> MapToHashSet(Cell[,] map)
{
// Flatten the 2D map into a single HashSet containing all cells
HashSet<Cell> results = new();
for (int y = 0; y < map.GetLength(0); y++)
{
for (int x = 0; x < map.GetLength(1); x++)
{
results.Add(map[x, y]);
}
}
return results;
}
private static Cell[,] ParseMap(string[] lines)
{
Cell[,] map = new Cell[lines[0].Length, lines.Length];
for (int y = 0; y < lines.Length; y++)
{
string line = lines[y];
for (int x = 0; x < line.Length; x++)
{
map[x, y] = new Cell(x, y, line[x] - '0');
}
}
return map;
}
private static Cell[,] ParseMap2(string[] lines)
{
// Variation on map parsing that reads the input and then scales the map to be 5x larger with a change to the
// weights of cells in blocks beyond the initial grid. This is for part 2
int width = lines[0].Length;
int height = lines.Length;
Cell[,] map = new Cell[width * 5, height * 5];
for (int y = 0; y < height; y++)
{
string line = lines[y];
for (int x = 0; x < width; x++)
{
map[x, y] = new Cell(x, y, line[x] - '0');
}
}
void SetNewWeight(int x, int y, int comparisonX, int comparisonY)
{
if (map[x, y] == null)
{
int comparisonWeight = map[comparisonX, comparisonY].Weight;
// The new weight is one greater than the weight of the corresponding cell (10 to the left or
// up). The weight wraps back to 1 after 9
int newWeight = comparisonWeight + 1;
if (newWeight == 10)
{
newWeight = 1;
}
map[x, y] = new Cell(x, y, newWeight);
}
}
for (int y = height; y < height * 5; y++)
{
for (int x = 0; x < width; x++)
{
if (map[x, y] == null)
{
int comparisonY = y - height >= 0 ? y - height : y;
SetNewWeight(x, y, x, comparisonY);
}
}
}
for (int y = 0; y < height * 5; y++)
{
for (int x = 0; x < width * 5; x++)
{
if (map[x, y] == null)
{
int comparisonX = x - width >= 0 ? x - width : x;
SetNewWeight(x, y, comparisonX, y);
}
}
}
return map;
}
private static List<Cell> GetNeighbors(Cell[,] map, Cell curr)
{
List<Cell> results = new();
List<Tuple<int, int>> neighbors =
new()
{
Tuple.Create(curr.X, curr.Y - 1), // N
Tuple.Create(curr.X + 1, curr.Y), // E
Tuple.Create(curr.X, curr.Y + 1), // S
Tuple.Create(curr.X - 1, curr.Y), // W
};
foreach (Tuple<int, int> neighbor in neighbors)
{
int x = neighbor.Item1;
int y = neighbor.Item2;
if (x >= 0 && x < map.GetLength(1) && y >= 0 && y < map.GetLength(0))
{
results.Add(map[x, y]);
}
}
return results;
}
private record Cell(int X, int Y, int Weight);
}