-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathProgram.cs
50 lines (42 loc) · 1.54 KB
/
Program.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
using System.Text.RegularExpressions;
using AdventOfCode.Common;
var lines = Resources.GetInputFileLines();
var instructionCounter = new CircularCounter(lines.First());
var instructionRegex = new Regex("[A-Z0-9]{3}");
var rawInstructions = lines
.Skip(1)
.Select(line => instructionRegex.Matches(line))
.ToDictionary(m => m[0].Value, m => (m[1].Value, m[2].Value));
var instructions = rawInstructions.Keys
.ToDictionary(name => name, name => new Instruction(name));
foreach (var instruction in instructions)
{
var i = rawInstructions[instruction.Key];
instruction.Value.Left = instructions.TryGetValue(i.Item1, out var v1) ? v1 : null!;
instruction.Value.Right = instructions.TryGetValue(i.Item2, out var v2) ? v2 : null!;
}
var part2 = instructions
.Where(p => p.Key.EndsWith('A'))
.Select(p => p.Value)
.Select(i => GetInstructionCount(i.Name))
.FindLowestCommonDenominator();
Console.WriteLine($"Part 1: {GetInstructionCount("AAA")}");
Console.WriteLine($"Part 2: {part2}");
int GetInstructionCount(string startInstruction)
{
var current = instructions[startInstruction];
var counter = instructionCounter!.GetEnumerator();
while (current.Name[2] != 'Z')
{
current = counter.Current == 'L' ? current.Left : current.Right;
counter.MoveNext();
}
var value = instructionCounter.Counter;
instructionCounter.Counter = 0;
return value;
}
file record Instruction(string Name)
{
public Instruction Left { get; set; } = null!;
public Instruction Right { get; set; } = null!;
}