using System.Collections; namespace AdventOfCode.HelperClasses; public record IntegerRange(long start, long end) : IEnumerable { public long Count => end - start + 1; public bool Contains(long number) => start <= number && end >= number; public bool TryCombine(IntegerRange other, out IntegerRange combined) { if (Contains(other.start) || other.start == end + 1) { var isEndInRange = Contains(other.end); combined = isEndInRange ? this : this with {end = other.end}; return true; } if (other.Contains(start) || start == other.end + 1) { var isEndInRange = other.Contains(end); combined = isEndInRange ? other : other with {end = end}; return true; } combined = this; return false; } public IEnumerator GetEnumerator() => new IntegerRangeEnumerator(start, end); IEnumerator IEnumerable.GetEnumerator() => GetEnumerator(); } public record IntegerRangeEnumerator(long start, long end) : IEnumerator { private long _current = start - 1; public bool MoveNext() { if(_current >= end) return false; _current++; return true; } public void Reset() { _current = start - 1; } long IEnumerator.Current => _current; object? IEnumerator.Current => _current; public void Dispose() { GC.SuppressFinalize(this); } }