generated from mazharenko/aoc-agent-template-multipleyears
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay20.cs
57 lines (47 loc) · 1.51 KB
/
Day20.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
using aoc.Common;
using aoc.Common.Search;
using mazharenko.AoCAgent.Generator;
namespace aoc.Year2024;
[BypassNoExamples]
internal partial class Day20
{
public char[,] Parse(string input)
{
return Character.In('.', '#', 'S', 'E').Map().Parse(input);
}
private static int Solve(char[,] map, int shortcutLength)
{
var shortcuts =
map.AsEnumerable().Where(x => x.element != '#')
.SelectMany(x =>
map.AsEnumerable().Where(y => y.element != '#')
.Where(y => x.point.MDist(y.point) <= shortcutLength)
.Select(y => (start: x.point, end: y.point))
)
.ToList();
var start = map.AsEnumerable().Single(x => x.element == 'S').point;
var end = map.AsEnumerable().Single(x => x.element == 'E').point;
var forward = Dijkstra.StartWith(start)
.WithAdjacency(x =>
Directions.All4().Select(d => d + x)
.Where(y => map.At(y) != '#'))
.ToDictionary(path => path.HeadItem, path => path.Len);
var backward = Dijkstra.StartWith(end)
.WithAdjacency(x =>
Directions.All4().Select(d => d + x)//todo: extension
.Where(y => map.At(y) != '#'))
.ToDictionary(path => path.HeadItem, path => path.Len);
var noShortcutsPath = forward[end];
return shortcuts.Count(shortcut =>
noShortcutsPath
- (shortcut.start.MDist(shortcut.end) + forward[shortcut.start] + backward[shortcut.end]) >= 100);
}
internal partial class Part1
{
public int Solve(char[,] input) => Solve(input, 2);
}
internal partial class Part2
{
public int Solve(char[,] input) => Solve(input, 20);
}
}