generated from mazharenko/aoc-agent-template-multipleyears
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay16.cs
107 lines (95 loc) · 2.39 KB
/
Day16.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
using aoc.Common;
using aoc.Common.Search;
using MoreLinq;
namespace aoc.Year2024;
internal partial class Day16
{
private readonly Example example = new(
"""
###############
#.......#....E#
#.#.###.#.###.#
#.....#.#...#.#
#.###.#####.#.#
#.#.#.......#.#
#.#.#####.###.#
#...........#.#
###.#.#####.#.#
#...#.....#.#.#
#.#.#.###.#.#.#
#.....#...#.#.#
#.###.#.#.#.#.#
#S..#.....#...#
###############
""");
private readonly Example example1 = new(
"""
#################
#...#...#...#..E#
#.#.#.#.#.#.#.#.#
#.#.#.#...#...#.#
#.#.#.#.###.#.#.#
#...#.#.#.....#.#
#.#.#.#.#.#####.#
#.#...#.#.#.....#
#.#.#####.#.###.#
#.#.#.......#...#
#.#.###.#####.###
#.#.#...#.....#.#
#.#.#.#####.###.#
#.#.#.........#.#
#.#.#.#########.#
#S#.............#
#################
""");
public char[,] Parse(string input)
{
// todo: make it easy to create a map, so that not to filter out new lines?
return Character.In('S', '.', '#', 'E').Map().Parse(input);
}
IEnumerable<((V<int> point, V<int> direction) newState, int weight)> Adjacency((V<int> point, V<int> direction) arg, char[,] input)
{
yield return ((arg.point, arg.direction.RotateCw()), 1000);
yield return ((arg.point, arg.direction.RotateCcw()), 1000);
if (input.At(arg.point) != '#')
yield return ((arg.point + arg.direction, arg.direction), 1);
}
internal partial class Part1
{
public Part1()
{
Expect(example, 7036);
Expect(example1, 11048);
}
public int Solve(char[,] input)
{
var start = input.AsEnumerable().Single(x => x.element == 'S');
var finish = input.AsEnumerable().Single(x => x.element == 'E');
return Dijkstra.StartWith((start.point, direction: Directions.E))
.WithAdjacency(arg => Adjacency(arg, input))
.FindTarget(state => state.point == finish.point)
.Len;
}
}
internal partial class Part2
{
public Part2()
{
Expect(example, 45);
Expect(example1, 64);
}
public int Solve(char[,] input)
{
var start = input.AsEnumerable().Single(x => x.element == 'S');
var finish = input.AsEnumerable().Single(x => x.element == 'E');
return Dijkstra.StartWith((start.point, direction: Directions.E))
.WithAdjacency(x => Adjacency(x, input))
.AllShortestPaths()
.FindTarget(x => x.point == finish.point)
.SelectMany(path => path.PathList)
.Select(item => item.Item.point)
.Distinct()
.Count();
}
}
}