diff --git a/AdventOfCode.Extensions/EnumerableExtensions.cs b/AdventOfCode.Extensions/EnumerableExtensions.cs index 15da20b..119a839 100644 --- a/AdventOfCode.Extensions/EnumerableExtensions.cs +++ b/AdventOfCode.Extensions/EnumerableExtensions.cs @@ -148,6 +148,15 @@ public static class EnumerableExtensions private static TValue[] GetCombination(int[] indices, TValue[] values) => indices.Select(i => values[i]).ToArray(); + /// + /// Enumerates sliding windows over the sequence, yielding one array for each source element. + /// Each returned array contains the most recent values up to , so the initial windows may contain fewer than elements. + /// + /// The type of elements in the sequence. + /// The sequence to enumerate. + /// The maximum number of elements to include in each window. + /// A sequence of arrays representing consecutive windows from . + /// is less than zero. public static IEnumerable SlidingWindow(this IEnumerable values, int count) { var queue = new Queue(count); @@ -156,7 +165,7 @@ public static class EnumerableExtensions queue.Enqueue(value); if(queue.Count > count) queue.Dequeue(); - yield return queue.ToArray(); + yield return [.. queue]; } } } \ No newline at end of file diff --git a/AoC_2026/Day02.cs b/AoC_2026/Day02.cs index ddf4ee3..180a28f 100644 --- a/AoC_2026/Day02.cs +++ b/AoC_2026/Day02.cs @@ -30,13 +30,13 @@ public class Day02 : IPuzzleSolver private static int GetTravelDistance(char waypoint, char[] program) { - var distance = 0; - foreach (var nextWaypoint in program) - { - distance += GetDistance(waypoint, nextWaypoint); - waypoint = nextWaypoint; - } - return distance; + return program + .Prepend(waypoint) + .SlidingWindow(2) + .Aggregate(0, (acc, next) => + next.Length < 2 + ? acc + : acc + GetDistance(next[0], next[1])); } private static int GetDistance(char current, char next)