-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay12.cs
70 lines (61 loc) · 2.27 KB
/
Day12.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
namespace AdventOfCode;
public class Day12 : BaseDay
{
static List<Coord> neighbors = new List<Coord>() { new Coord(-1, 0), new Coord(1, 0), new Coord(0, -1), new Coord(0, 1) };
char[][] grid;
record struct Coord(int y, int x);
public Day12()
{
grid = File.ReadAllLines(InputFilePath).Select(x => x.ToCharArray()).ToArray();
}
public override ValueTask<string> Solve_1()
{
return new(BFS().ToString());
}
public override ValueTask<string> Solve_2()
{
var costs = grid.Select(x => x.Select(c => c == 'S' ? 1 : c == 'E' ? 26 : c - 'a' + 1).ToArray()).ToArray();
return new(BFS(costs).ToString());
}
private int BFS(int[][] costs = null)
{
var queue = GetCosts(costs);
var visited = new HashSet<Coord>();
while (queue.Any())
{
(Coord point, int cost) = queue.Dequeue();
if (!visited.Add(point))
continue;
if (grid[point.y][point.x] == 'E')
return cost;
foreach (var neighbor in neighbors)
{
var newY = point.y + neighbor.y;
var newX = point.x + neighbor.x;
if ((newY >= 0 && newY < grid.Length) && (newX >= 0 && newX < grid[0].Length))
{
if (costs == null)
{
if (grid[point.y][point.x] == 'S' ? grid[newY][newX] - 'a' <= 1 : grid[newY][newX] - grid[point.y][point.x] <= 1)
queue.Enqueue((new Coord(newY, newX), cost + 1));
}
else
{
if (costs[newY][newX] <= 1 + costs[point.y][point.x])
queue.Enqueue((new Coord(newY, newX), cost + 1));
}
}
}
}
return 0;
}
private Queue<(Coord, int cost)> GetCosts(int[][] costs)
{
var queue = new Queue<(Coord, int cost)>();
for (int y = 0; y < grid.Length - 1; y++)
for (int x = 0; x < grid[0].Length - 1; x++)
if ((costs != null && costs[y][x] == 1) || (costs == null && grid[y][x] == 'S'))
queue.Enqueue((new Coord(y, x), 0));
return queue;
}
}