-
Notifications
You must be signed in to change notification settings - Fork 0
/
Day8.cs
63 lines (52 loc) · 1.72 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
using System.Linq.Expressions;
using System.Text.RegularExpressions;
static class Day8 {
static long gcf(long a, long b)
{
while (b != 0)
{
long temp = b;
b = a % b;
a = temp;
}
return a;
}
static long lcm(long a, long b)
{
return (a / gcf(a, b)) * b;
}
internal static void doit() {
Regex dayNoR = new(@"\d*$");
var lines = Helper.getInputAsLines(int.Parse(dayNoR.Match(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.Name).Value),
test:false);
var rl = lines.First().Replace("R","1").Replace("L","0").ToCharArray().Select(a=>int.Parse(a.ToString()));
var rllen=rl.Count();
var inst = new Dictionary<string, string[]>();
foreach (var line in lines.Skip(1).Where(a=>!String.IsNullOrWhiteSpace(a)))
{
var split = line.Split("=", StringSplitOptions.TrimEntries);
var linerl = split[1].Trim("()".ToCharArray()).Split(",",StringSplitOptions.TrimEntries);
inst.Add(split[0], linerl);
}
var next="AAA";
var i = 0;
while (next != "ZZZ"){
var myRL = rl.Skip(i++%rllen).First();
next=inst[next][myRL];
}
var nextB = inst.Keys.Where(a=>a.EndsWith("A")).ToList();
long steps = 1;
for (int k = 0; k < nextB.Count; k++)
{
var j=0;
next=nextB[k];
while(!next.EndsWith("Z")){
var myRL = rl.Skip(j++%rllen).First();
next=inst[next][myRL];
}
steps=lcm(steps,j);
}
Console.WriteLine($"Sum: {i}");
Console.WriteLine($"SumB: {steps}");
}
}