diff --git a/AdventOfCode/AdventOfCode.csproj b/AdventOfCode/AdventOfCode.csproj index 19aa473..8beeaf6 100644 --- a/AdventOfCode/AdventOfCode.csproj +++ b/AdventOfCode/AdventOfCode.csproj @@ -55,6 +55,8 @@ + + diff --git a/AdventOfCode/Problems/AOC2023/Day8/HauntedWasteland.cs b/AdventOfCode/Problems/AOC2023/Day8/HauntedWasteland.cs new file mode 100644 index 0000000..000a8e0 --- /dev/null +++ b/AdventOfCode/Problems/AOC2023/Day8/HauntedWasteland.cs @@ -0,0 +1,74 @@ +using AdventOfCode.Runner.Attributes; + +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace AdventOfCode.Problems.AOC2023.Day8; + +[ProblemInfo(2023, 8, "Haunted Wasteland")] +internal class HauntedWasteland : Problem +{ + private string _path = string.Empty; + private Dictionary _nodes = []; + + public override void LoadInput() + { + var data = ReadInputLines(); + _path = data[0]; + + for (int i = 2; i < data.Length; i++) + { + var curLine = data[i].Split('='); + var node = curLine[0].TrimEnd(); + var branches = curLine[1][2..^1].Split(", "); + _nodes.Add(node, (branches[0], branches[1])); + } + } + + public override void CalculatePart1() + { + var curPos = "AAA"; + var i = 0; + var steps = 0; + do + { + curPos = _path[i] switch + { + 'L' => _nodes[curPos].left, + 'R' => _nodes[curPos].right, + _ => throw new Exception("Something went horribly wrong") + }; + i = (i + 1) % _path.Length; + steps++; + } while (curPos != "ZZZ"); + Part1 = steps; + } + + public override void CalculatePart2() + { + var curPos = _nodes.Keys.Where(n => n[^1] == 'A').ToArray(); + var len = new long[curPos.Length]; + var i = 0; + do + { + for (int j = 0; j < curPos.Length; j++) + { + if (curPos[j][^1] == 'Z') + continue; + len[j]++; + curPos[j] = _path[i] switch + { + 'L' => _nodes[curPos[j]].left, + 'R' => _nodes[curPos[j]].right, + _ => throw new Exception("Something went horribly wrong") + }; + } + i = (i + 1) % _path.Length; + } while (curPos.Any(n => n[^1] != 'Z')); + + Part2 = len.LCM(); + } +} \ No newline at end of file diff --git a/AdventOfCode/Runner/Extensions.cs b/AdventOfCode/Runner/Extensions.cs index 4e446aa..e1ee614 100644 --- a/AdventOfCode/Runner/Extensions.cs +++ b/AdventOfCode/Runner/Extensions.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.Linq; +using System.Numerics; using System.Text; using System.Threading.Tasks; @@ -15,4 +16,15 @@ public static class Extensions } return values; } + + public static T LCM(this IEnumerable values) where T : INumber + { + var a = values.First(); + values = values.Skip(1); + foreach (var item in values) + { + a = a.LCM(item); + } + return a; + } } diff --git a/AdventOfCode/Runner/ExtraMath.cs b/AdventOfCode/Runner/ExtraMath.cs new file mode 100644 index 0000000..1c0cc38 --- /dev/null +++ b/AdventOfCode/Runner/ExtraMath.cs @@ -0,0 +1,26 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Numerics; +using System.Text; +using System.Threading.Tasks; + +namespace AdventOfCode.Runner; +public static class ExtraMath +{ + public static T GCF(this T a, T b) where T : INumber + { + while (!b.Equals(T.Zero)) + { + var t = b; + b = a % b; + a = t; + } + return a; + } + + public static T LCM(this T a, T b) where T: INumber + { + return (a / GCF(a, b)) * b; + } +}