file record Range(int Lower, int Upper)
{
public int Distance => Upper - Lower;
private bool Intersects(Range other) => other.Lower <= Upper && other.Upper >= Lower;
public static IEnumerable<Range> operator -(Range minuend, Range subtrahend)
{
if (!minuend.Intersects(subtrahend))
{
yield return minuend;
yield break;
}
if (minuend.Lower < subtrahend.Lower)
yield return minuend with { Upper = subtrahend.Lower };
if (minuend.Upper > subtrahend.Upper)
yield return minuend with { Lower = subtrahend.Upper };
}
public static IEnumerable<Range> operator -(IEnumerable<Range> minuends, Range subtrahend)
=> minuends.SelectMany(minuend => minuend - subtrahend);
}