-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDay21.cs
More file actions
140 lines (117 loc) · 4.53 KB
/
Day21.cs
File metadata and controls
140 lines (117 loc) · 4.53 KB
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
using System.Collections.Immutable;
using System.Data;
using System.Numerics;
using System.Runtime.InteropServices.ComTypes;
using _2024.Utils;
namespace _2024._21;
public class Day21 : Base
{
private static readonly List<char[]> NumericPad =
[
['7', '8', '9'],
['4', '5', '6'],
['1', '2', '3'],
[' ', '0', 'A']
];
private static readonly List<char[]> DirectionalPad =
[
[' ', '^', 'A'],
['<', 'v', '>']
];
private static readonly ValueTuple<ValueTuple<int, int>, char>[] Directions =
[
((0, 1), '>'),
((0, -1), '<'),
((1, 0), 'v'),
((-1, 0), '^')
];
private static ValueTuple<int, int> NumericPadStart => (NumericPad.Count - 1, NumericPad[^1].Length - 1);
private static ValueTuple<int, int> DirectionalPadStart => (0, DirectionalPad[0].Length - 1);
private readonly string[] _codes;
private readonly Dictionary<ValueTuple<string, int>, long> _cache = new();
private int _numRobots = 2;
public Day21(bool example) : base(example)
{
Day = "21";
_codes = ReadInput();
}
private static HashSet<string> FindShortestSequences(bool isDirectional, string sequence, ValueTuple<int, int> start)
{
PriorityQueue<ValueTuple<ValueTuple<int, int>, int, string>, int> queue = new();
Dictionary<ValueTuple<ValueTuple<int, int>, int>, int> visited = new();
queue.Enqueue((start, 0, ""), 0);
List<char[]> pad = isDirectional ? DirectionalPad : NumericPad;
HashSet<string> bestSequences = [];
while (queue.TryDequeue(out ((int, int), int, string) curr, out int moves))
{
(ValueTuple<int, int> pos, int sequencePos, string buildSequence) = curr;
if (sequencePos == sequence.Length)
{
bestSequences.Add(buildSequence);
continue;
}
if (visited.TryGetValue((pos, sequencePos), out int stepsTaken) && stepsTaken < moves)
{
continue;
}
visited[(pos, sequencePos)] = moves;
if (pad.TryGetValue(pos, out char value) && value == sequence[sequencePos])
{
queue.Enqueue((pos, sequencePos + 1, buildSequence+'A'), moves + 1);
continue;
}
foreach ((ValueTuple<int, int> direction, char symbol) in Directions)
{
ValueTuple<int, int> newPos = (pos.Item1 + direction.Item1, pos.Item2 + direction.Item2);
if (!pad.TryGetValue(newPos, out value) || value == ' ')
{
continue;
}
queue.Enqueue((newPos, sequencePos, buildSequence+symbol), moves + 1);
}
}
return bestSequences;
}
private static ValueTuple<int, int> GetEnd(bool isDirectional, char code)
{
List<char[]> pad = (isDirectional ? DirectionalPad : NumericPad);
int row = pad.FindIndex(x => x.Contains(code));
return (row, Array.IndexOf(pad[row], code));
}
private long GetShortestSequence(string code, int position = 0)
{
if (_cache.TryGetValue((code, position), out long result))
{
return result;
}
if (position > _numRobots)
{
return code.Length;
}
bool isDirectional = position > 0;
ValueTuple<int, int> start = isDirectional ? DirectionalPadStart : NumericPadStart;
long pathLength = 0;
foreach (char t in code)
{
HashSet<string> sequences = FindShortestSequences(isDirectional, t.ToString(), start);
start = GetEnd(isDirectional, t);
pathLength += sequences.Select(sequence => GetShortestSequence(sequence, position + 1)).Min();
}
_cache.Add((code, position), pathLength);
return pathLength;
}
public override object PartOne()
{
_numRobots = 2;
return _codes.Select(code => GetShortestSequence(code) * long.Parse(code[..^1])).Sum();
}
public override object PartTwo()
{
_numRobots = 25;
return _codes.Select(code => GetShortestSequence(code) * long.Parse(code[..^1])).Sum();
}
public override void Reset()
{
_cache.Clear();
}
}