Refactor Day02 travel distance calculation to use SlidingWindow
This commit is contained in:
1 parent
d22fd3cacb
commit
fefdc59bd8
2 files changed
+17
-8
No files matched your search
+7
-7
@@ -30,13 +30,13 @@ public class Day02 : IPuzzleSolver<long>
|
||||
|
||||
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)
|
||||
|
||||
Reference in new issue
Block a user