Refactor range combining logic for future reusability

This commit is contained in:
Sebastian Lindemeier 2025-12-05 10:59:44 +01:00
parent 79541d0dae
commit fd417a6e97
1 changed files with 14 additions and 2 deletions

View File

@ -104,14 +104,26 @@ public class Day05 : IPuzzleSolver<long>
{
optimizedRanges.Add(range);
}
else if (IsIdInRange(range.from, lastRange) && ! IsIdInRange(range.to, lastRange))
else if (TryCombineRanges(lastRange, range, out var combined))
{
optimizedRanges[^1] = (lastRange.from, range.to);
optimizedRanges[^1] = combined;
}
}
return optimizedRanges;
}
private bool TryCombineRanges(Range first, Range second, out Range combined)
{
if (IsIdInRange(second.from, first) && !IsIdInRange(second.to, first))
{
combined = (first.from, second.to);
return true;
}
combined = first;
return false;
}
private static IEnumerable<long> CountIdsInRanges(IEnumerable<Range> combinedRanges)
{