Added unit test project for HelperClasses and tests for IntegerRange.cs. Implemented IEnumerable on IntegerRange.

This commit is contained in:
2025-12-14 14:45:13 +01:00
parent bef54ff551
commit 33eb7322a8
5 changed files with 129 additions and 6 deletions
+37 -5
View File
@@ -1,21 +1,23 @@
namespace AdventOfCode.HelperClasses;
using System.Collections;
public record IntegerRange(long start, long end)
namespace AdventOfCode.HelperClasses;
public record IntegerRange(long start, long end) : IEnumerable<long>
{
public long Count() => end - start + 1;
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))
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))
if (other.Contains(start) || start == other.end + 1)
{
var isEndInRange = other.Contains(end);
combined = isEndInRange ? other : other with {end = end};
@@ -25,4 +27,34 @@ public record IntegerRange(long start, long end)
combined = this;
return false;
}
public IEnumerator<long> GetEnumerator() => new IntegerRangeEnumerator(start, end);
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
}
public record IntegerRangeEnumerator(long start, long end) : IEnumerator<long>
{
private long _current = start - 1;
public bool MoveNext()
{
if(_current >= end) return false;
_current++;
return true;
}
public void Reset()
{
_current = start - 1;
}
long IEnumerator<long>.Current => _current;
object? IEnumerator.Current => _current;
public void Dispose()
{
GC.SuppressFinalize(this);
}
}