-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay8.cs
91 lines (76 loc) · 2.64 KB
/
Day8.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
namespace AoC2023;
public class Day8 : IDay<IEnumerable<string>, long>
{
public static long SolvePart1(IEnumerable<string> input)
{
var inputArr = input.ToArray();
char[] pattern;
Dictionary<string, Tuple<string, string>> map = [];
BuildMapAndPattern(input, out map, out pattern, out _);
return FindStepsRequired("AAA", node => node == "ZZZ", map, pattern);
}
public static long SolvePart2(IEnumerable<string> input)
{
var inputArr = input.ToArray();
char[] pattern;
Dictionary<string, Tuple<string, string>> map = [];
List<string> nodesEndingWithA = [];
BuildMapAndPattern(input, out map, out pattern, out nodesEndingWithA);
// 🤔 For some reason, the input is cyclic, so this works
return Helpers.LCM(
nodesEndingWithA
.Select(n => FindStepsRequired(n, node => node.EndsWith('Z'), map, pattern))
.ToArray()
);
}
private static void BuildMapAndPattern(
IEnumerable<string> input,
out Dictionary<string, Tuple<string, string>> map,
out char[] pattern,
out List<string> nodesEndingWithA
)
{
var inputArr = input.ToArray();
pattern = inputArr[0].ToArray();
nodesEndingWithA = new List<string>();
map = [];
for (int i = 2; i < inputArr.Length; i++)
{
var lineArr = inputArr[i].Split('=');
var source = lineArr[0].Trim();
var destinations = lineArr[1].Split(',');
var destinationLeft = string.Join("", destinations[0].Skip(2));
var destinationRight = string.Join("", destinations[1].Skip(1).Take(3));
map.Add(source, Tuple.Create(destinationLeft, destinationRight));
if (source.EndsWith('A'))
{
nodesEndingWithA.Add(source);
}
}
}
private static long FindStepsRequired(
string start,
Func<string, bool> hasReachedDestination,
Dictionary<string, Tuple<string, string>> map,
char[] pattern
)
{
char direction;
var currLocation = start;
var patternPointer = 0;
var jumps = 0;
while (!hasReachedDestination(currLocation))
{
direction = pattern[patternPointer];
var (left, right) = map[currLocation];
currLocation = direction switch
{
'L' => left,
_ => right
};
jumps++;
patternPointer = patternPointer + 1 >= pattern.Length ? 0 : patternPointer + 1;
}
return jumps;
}
}