-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
46 lines (46 loc) · 2.11 KB
/
Program.cs
File metadata and controls
46 lines (46 loc) · 2.11 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
using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Diagnostics;
class Program // Advend of Code 2023, Day 8
{ // Guenther Meusburger 2023
static void Main()
{
Stopwatch stopwatch = new Stopwatch(); stopwatch.Start();
string path = @"C:\Users\guent\OneDrive\anp_GF07\aoc2023\C#\aoc202308\Input8.txt";
var lines = File.ReadAllLines(path); string navCommands = lines[0];
var dict = new Dictionary<string, string[]>();
for (int i = 2; i < lines.Length; i++)
{
var splitLine = lines[i].Split('=');
string key = splitLine[0].Trim(); string value = splitLine[1].Trim();
value = value.Trim('(', ')');
string[] node = value.Split(',');
for (int j = 0; j < node.Length; j++) node[j] = node[j].Trim();
dict[key] = node;
}
int curNavPos = 0; int steps = 0; string NodeAAA = "AAA"; long part1 = 0;
List<string> curNodesA = dict.Keys.Where(key => key.EndsWith("A")).ToList();
var allSteps = new List<long>();
do
{
for (int i = 0; i < curNodesA.Count; i++)
{
if (curNodesA[i] != "")
{
curNodesA[i] = dict[curNodesA[i]][navCommands[curNavPos] == 'L' ? 0 : 1];
if (curNodesA[i].EndsWith("Z")) { allSteps.Add(steps + 1); curNodesA[i] = ""; }
}
}
if (NodeAAA != "") NodeAAA = dict[NodeAAA][navCommands[curNavPos] == 'L' ? 0 : 1];
if (NodeAAA == "ZZZ") { NodeAAA = ""; part1 = steps + 1; }
steps++; curNavPos++;
if (curNavPos == navCommands.Length) curNavPos = 0;
} while (allSteps.Count != curNodesA.Count);
Console.WriteLine($"Part1: {part1}\nPart2: {LCM(allSteps)}"); ;
stopwatch.Stop(); Console.WriteLine($"Laufzeit:{stopwatch.Elapsed.TotalSeconds} ");
}
public static long LCM(List<long> numbers) { return numbers.Aggregate((a, b) => a * b / GCD(a, b)); }
public static long GCD(long a, long b) { return b == 0 ? a : GCD(b, a % b); }
}